dp****@app-tech.com (Dave Perry) wrote in message news:<a0**************************@posting.google. com>...
"dixie" <di****@dogmail.com> wrote in message news:<UT****************@nnrp1.ozemail.com.au>... I came across a lot of repetitious code in a form module that I would like
to put in a module that I can call multiple times from a form. The problem
I have run into is that when I put the code into a module, the Control Names
and the Fields do not work as the module does not recognise them. Is there
a way around this, or do I just live with the repetition of code in the
form's module?
dixie
dixie,
Have you tried passing your controls as arguments to the methods in
you module?
-dp
Here are a couple of module functions I use to keep users from typing
in more characters in the control than the table field is designed to
hold:
Public Function LimitField(KeyAscii As Integer, txtBox As TextBox,
intMax As Integer) As Integer
'If the field has reached its maximum then return 0
LimitField = KeyAscii
txtBox.SetFocus 'I forgot why I put this line here
If Len(txtBox.Text) = intMax Then
If KeyAscii <> 8 Then LimitField = 0
End If
End Function
Example call:
Private Sub txtCity_KeyPress(KeyAscii As Integer)
KeyAscii = LimitField(KeyAscii, txtCity, MaxAFieldWidth(iCity))
End Sub
Public Function LimitCombo(KeyAscii As Integer, cbxCombo As ComboBox,
intMax As Integer) As Integer
'If the field has reached its maximum then return 0
LimitCombo = KeyAscii
cbxCombo.SetFocus
If Len(cbxCombo.Text) = intMax Then
If KeyAscii <> 8 Then LimitCombo = 0
End If
End Function
Example call:
Private Sub cbxContact_KeyPress(KeyAscii As Integer)
KeyAscii = LimitCombo(KeyAscii, cbxContact, 50)
End Sub
The '8' is so that they're not in a cul-de-sac if they hit the limit.
I probably got the idea for these functions from code on this NG.
James A. Fortune