Create functions within a form

benc

Registered User.
Local time
Today, 12:42
Joined
Dec 31, 2001
Messages
57
Hi on one form i have lots reasons to call the same bit code i.e. from different buttons or key presses. Rather than write the code over and over again i would like to know how to write the code once and then call it when needed. Thanks in advance
 
You can do this by creating a public function in a module , then in your code you refer to the name of the function to call it.

the main thing to remember is that if you want a function in a module to be available elsewhere in your database you must declare it as public.

This is a simple example which will display and message box saying "hello world"

public Function fTest ()

msgBox "Hello World"

End Function

To use it with a command button:

Private Sub cmdButton_Click()

fTest

End Sub
 
All functions and sub's are public by default. You can optionally put the Public keyword in front of it, or use the Private keyword to keep it local to the particular form or module it is in.

Just wanted to note also that to call a public function from code, instead of putting this code into the Click event of the command button:
Code:
Private Sub cmdButton_Click()
    fTest
End Sub
and then putting the code into the Click event of the command button, it's just as easy to simply put:
Code:
=fTest()
.
 

Users who are viewing this thread

Back
Top Bottom