Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using fields and or form controls in a module

9 views
Skip to first unread message

dixie

unread,
May 5, 2004, 2:30:50 AM5/5/04
to
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


Allen Browne

unread,
May 5, 2004, 5:31:18 AM5/5/04
to
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" <dix...@dogmail.com> wrote in message
news:UT%lc.125$UL2....@nnrp1.ozemail.com.au...

NeilAnderson

unread,
May 5, 2004, 5:42:53 AM5/5/04
to
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" <dix...@dogmail.com> wrote in message news:<UT%lc.125$UL2....@nnrp1.ozemail.com.au>...

Dave Perry

unread,
May 5, 2004, 10:43:22 AM5/5/04
to
"dixie" <dix...@dogmail.com> wrote in message news:<UT%lc.125$UL2....@nnrp1.ozemail.com.au>...

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.

James Fortune

unread,
May 5, 2004, 4:29:29 PM5/5/04
to
dpe...@app-tech.com (Dave Perry) wrote in message news:<a0ca0e55.04050...@posting.google.com>...

> "dixie" <dix...@dogmail.com> wrote in message news:<UT%lc.125$UL2....@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

0 new messages