How to call sub procedure inside a function

masoud_sedighy

Registered User.
Local time
Today, 15:14
Joined
Dec 10, 2011
Messages
132
I have a form, its name is "form3" there is a button on the form and click event of this button is like below:

Option Compare Database

Public Sub Command0_Click()

'DO SOME THING

End Sub



Now i wanted to call this sub from a macro, i have created a function like below and try to call sub, and defined function in macro but when i run macro i get error "sub, function, not defined"


Option Compare Database

Public Function Update_Trend_Line_by_Month()

Call Command0_Click()

End Function
 
this is very tricky and make sure to call it wisely, since it will make new instance of the form when it is not already open.

Option Compare Database
Public Function Update_Trend_Line_by_Month()
Call Form_FormName.Command0_Click
End Function
 
It's works better in this sort of case if you put the code in a module. So in the module you would have

Code:
Option Compare Database

Public Sub DoSomething()

'DO SOME THING

End Sub

Then in your form module:

Option Compare Database

Code:
Public Sub Command0_Click()

DoSomething

End Sub

And then in Update_Trend_Line_by_Month()

Code:
Option Compare Database

Public Function Update_Trend_Line_by_Month()

DoSomething

End Function
 

Users who are viewing this thread

Back
Top Bottom