Running the same code

ECEK

Registered User.
Local time
Today, 22:53
Joined
Dec 19, 2012
Messages
717
I have a command button [Me.cmdUpdFE] on Form1 that runs several lines of code.

What I want to do is call this code under different circumstances.

Obviously I can copy and paste my code but would always prefer to have just one place to edit my code.

Is this possible?

ie when I open Form2 then run the same code as Form1.cmdUpdFE_onclick()
 
put the code in separate Module, ie:
if there is Form reference on your code,
add parameter as Form to the Public Sub.
insteand of using Me., use the Form reference:

Public Sub UpdateFE(ByRef thisForm As Access.Form)
' your code here...
'
...
...
' now instead of Me to reference the form, use
' thisForm
'
' Example:
' Dim strStatus
' strStatus = thisForm.Status (not Me.Status)
End Sub


then you call this Public sub in your button's
Click event.

Private Sub cmdUpdFE_Click()
Call UpdateFE(Me)
End Sub
 
Of course !!!
Thanks Arnie
 
in addition, if there are codes in the Sub
which is specific to a particular Form,
test for the Name of the form, before executing
the code. otherwise, make the sub more generic.
eg:

Public Sub UpdateFE(ByRef thisForm As Access.Form)
' specific code for frmEntry
If thisForm.Name = "frmEntry" Then
' code goes here
ElseIf thisForm.Name = "someOtherFormName" Then
' another code goes here
End If
'generic code for all forms or sub
'that is consuming this Sub will
'go here..
End Sub
 

Users who are viewing this thread

Back
Top Bottom