By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,476 Members | 1,390 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,476 IT Pros & Developers. It's quick & easy.

Using fields and or form controls in a module

P: n/a
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
Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
If the field/control names are the same, but the form is different, you can
pass a reference to the into your generic procedure.

The function in the general module would be declared like this:
Public Function MyFunc(frm As Form)
Debug.Print frm![SomeControl]
End Function

That works just like:
Debug.Print Me.SomeControl
in the module of the form.

To call the function from your form's module:
Call MyFunc(Me)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"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

Nov 12 '05 #2

P: n/a
Dixie

You could put a sub or function in the form's module, or write the
code in a module in such a way that you can pass the value of a
control to the function.
i.e.
Function myFunction(myControlValue)

The latter method is the way that I would do it, as I tend to need
these things in more than one form.

neil
"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

Nov 12 '05 #3

P: n/a
"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

p.s. If you're still having trouble, provide some sample code and I'd
be glad to help.
Nov 12 '05 #4

P: n/a
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
Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.