Need to Parm Form Name in VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a module that I need to update a form field on one of several forms.
I need a way to parm in the form name. I don't want to have to write and
maintain seperate modules to perform the same load logic for each of the
forms.
 
Since you didn't post the code you are using, specific advice is impossible.

I assume that you want to pass the form or formname as an argument to a
subroutine (a sub or function).

Public Sub ChangeTheValue (FormRef as Form)

With FormRef
'Do something here
End With

End Sub

It might make more sense to pass in the actual control on the form and
modify the value there.
 
Here is an example of the code I am working with.
I have two forms that use the same module to set values on the form. What I
am trying to do is be able to use the code for any form and not hardcode the
form name. This even more pressing when assigning values to fields on a
subforms.

If (Len(Me.TicketSelection) = 0) Then
Forms![EMail Automation1]![CPkg] = ""
Forms![EMail Automation1]![CPkg].Visible = False
Forms![EMail Automation1]![PackageLbl].Visible = False
Forms![EMail Automation1]![MessageArea].Visible = False
Forms![EMail Automation1]![MessageArea] = ""
Else
Me.TicketSelection = Translate(Me.TicketSelection, Me.TicketNumber &
",", "")
End If
 
From your example, you might be using code that looks the same in two or
more forms. You are not using the same module.

So, if you want the code to run, I would assume you have the same controls
on the two forms. You would need the following in a VBA module (not one
associated with a form)

Sub SetAutomation (frmAny as Form)

With frmAny
if Len(.TicketSelection) = 0 then
Forms![EMail Automation1]![CPkg] = ""
...
Else
.TicketSelection = Translate(.TicketSelection, .TicketNumber & ",",
"")
End If
End With 'frmAny

End Sub

You could then call the above from a form or a subform that has a control
named TicketSelection. You could do this as simply as

SetAutomation Me (works from a form or a subform)
or
SetAutomation Forms!YourFormName
or
SetAutomation Forms!YourFormName!SubFormControlName.Form


BCP said:
Here is an example of the code I am working with.
I have two forms that use the same module to set values on the form. What
I
am trying to do is be able to use the code for any form and not hardcode
the
form name. This even more pressing when assigning values to fields on a
subforms.

If (Len(Me.TicketSelection) = 0) Then
Forms![EMail Automation1]![CPkg] = ""
Forms![EMail Automation1]![CPkg].Visible = False
Forms![EMail Automation1]![PackageLbl].Visible = False
Forms![EMail Automation1]![MessageArea].Visible = False
Forms![EMail Automation1]![MessageArea] = ""
Else
Me.TicketSelection = Translate(Me.TicketSelection, Me.TicketNumber &
",", "")
End If

John Spencer said:
Since you didn't post the code you are using, specific advice is
impossible.

I assume that you want to pass the form or formname as an argument to a
subroutine (a sub or function).

Public Sub ChangeTheValue (FormRef as Form)

With FormRef
'Do something here
End With

End Sub

It might make more sense to pass in the actual control on the form and
modify the value there.
 
Back
Top