Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling sub from form 2

Status
Not open for further replies.

nat1967

Technical User
Feb 13, 2001
287
US
Hi everyone,

I use the following code to clear controls on my forms.

**********
Sub ClearAll()
Dim ctl As Control

For Each ctl In Me.Controls
If ctl.Tag = "Clear" Then
ctl.Value = Null
End If
Next ctl
End Sub
****************

Easy enough and works great. I would like to put this code into a module or class module so I can call it from other forms without having to re-type the code on every form.

The problem comes form "Me.Controls". "Me" is limited to the name of the form the code is in. However, it wont work in a module (makes sense to me) since the module is outside a form and is generic. So,... how can I call the procedure from a module with the name of the form being a variable?

I hope my question makes sense. Thanks in advance.

Have A Great Day!!!, [bigglasses]

Nathan
Senior Test Lead
 
Here are two VBA module-based approaches:

1. Pass a reference to the current form:
Code:
   'In your form's OnLoad event:

   Call ClearAllByForm(Me)

   'In a global module somewhere...
   Public Sub ClearAllByForm(frm as Form)
      Dim ctl As Control

      'Is Form Open?
      If SysCmd(acSysCmdGetObjectState, acForm, frm.Name) <> 0 Then
         For Each ctl In frm.Controls
            If ctl.Tag = &quot;Clear&quot; Then
               ctl.Value = Null
            End If
         Next ctl
      End If
   End Sub

2. Pass a formname:
Code:
   'In your form's OnLoad event:

   Call ClearAllByFormName(Me.Name)

   'OR, in some other code (assuming form frmFormName is open):

   Call ClearAllByFormName(&quot;frmFormName&quot;)

   'In a global module somewhere...
   Public Sub ClearAllByFormName(strForm as String)
      Dim ctl As Control

      'Is Form Open?
      If SysCmd(acSysCmdGetObjectState, acForm, frm.Name) <> 0 Then
         For Each ctl In Forms(strForm).Controls
            If ctl.Tag = &quot;Clear&quot; Then
               ctl.Value = Null
            End If
         Next ctl
      End If
   End Sub

You could make this type of routine part of a new base class form and have it automatically execute on load. Jim Kraxberger
Developing Access solutions since 1995
 
Jim,

Works perfect!!

Thanks a bunch! Have A Great Day!!!, [bigglasses]

Nathan
Senior Test Lead
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top