CBF vs. Modules

D

David S.

My question is about the best place to put code. I am
thinking of placing everything in modules instead of CBF
unless absolutely necessary. It seems to me that it is
much easier to manage instead of chasing down form names
to find subs. I'd like to hear your thoughts on the pros
and cons. Thanks I appreciate it.

David
 
M

Marshall Barton

David said:
My question is about the best place to put code. I am
thinking of placing everything in modules instead of CBF
unless absolutely necessary. It seems to me that it is
much easier to manage instead of chasing down form names
to find subs. I'd like to hear your thoughts on the pros
and cons.


If the code is going to be called from more than one
form/report, then definitely place it in a standard module.

If it's only going to be used in a single form/report, then
(except possiblly to make the form/reports light weight) the
code should be in the form/report to keep it as close to its
use as possible.
 
J

Joe Fallon

I agree.
But you can still try to make many procedures as genric as possible and put
them in Modules.
=======================================================
e.g.
This is one way to use a general block of code and place it behind multiple
controls.
It changes the date based on the key pressed while the control has the
focus.
=======================================================
In the OnKeyPress event of a Date field on a form I have this:
Call ChangeDate(KeyAscii, Me![UpdateDate])
=======================================================
I have this code in a standard module:
=======================================================
Public Sub ChangeDate(KeyAscii As Integer, ctl As Control)
On Error GoTo Err_ChangeDate
Const KEY_PLUS = 43, KEY_EQUALS = 61
Const KEY_MINUS = 45, KEY_UNDERSCORE = 95
Const KEY_UM = 77, KEY_LM = 109
Const KEY_UW = 87, KEY_LW = 119
Const KEY_UY = 89, KEY_LY = 121
Const KEY_UT = 84, KEY_LT = 116
Dim Msg As String
Select Case KeyAscii
Case KEY_EQUALS, KEY_PLUS
ctl = DateAdd("d", 1, ctl)
KeyAscii = 0
Case KEY_MINUS, KEY_UNDERSCORE
ctl = DateAdd("d", -1, ctl)
KeyAscii = 0
Case KEY_UT
ctl = DateAdd("d", 10, ctl)
KeyAscii = 0
Case KEY_LT
ctl = DateAdd("d", -10, ctl)
KeyAscii = 0
Case KEY_UW
ctl = DateAdd("ww", 1, ctl)
KeyAscii = 0
Case KEY_LW
ctl = DateAdd("ww", -1, ctl)
KeyAscii = 0
Case KEY_UY
ctl = DateAdd("yyyy", 1, ctl)
KeyAscii = 0
Case KEY_LY
ctl = DateAdd("yyyy", -1, ctl)
KeyAscii = 0
Case KEY_UM
ctl = DateAdd("m", 1, ctl)
KeyAscii = 0
Case KEY_LM
ctl = DateAdd("m", -1, ctl)
KeyAscii = 0
End Select

Exit_ChangeDate:
Exit Sub

Err_ChangeDate:
MsgBox ("Error # " & Str(Err.Number) & " was generated by " & Err.Source
& Chr(13) & Err.Description)
Resume Exit_ChangeDate

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top