VBA Code To Global VBA

vdanelia

Registered User.
Local time
Today, 08:47
Joined
Jan 29, 2011
Messages
215
Hello Dear friends i have a question about saving VBA As Global Module...

While working with Access and VBA There are many VBA codes which we use very frequently for example:
Private Sub close_Click()
DoCmd.Close
End Sub

How to save this as Module and then call to a button?

Thank you In Advanced
 
Hopefully you wish to do this for more than the basic things like close.. which there is no value in changing to a global macro because it's one line of code, not to mention the wizard can make you a close button in about 15 seconds no coding required.

But for more complex operations, you have your code in a Public Sub
and you name it approptiatly.
When in your button you will simply call that subroutine or function.

Button1_click
My_subroutine <== This line here moves to your subroutine called My_subroutine
End sub
 
Access_guy49 Thank You for the reply...
Of course I will not use this just for a close function, I just wanted to know, because i have more complex codes and needed to save and then to use (Call them)
 
So the key is to making them Public.
When you create a custom subroutine in a module Private subs are local to that module, while Public subs are accessable throughout the application.

Public Sub My_first_Subroutine (Any Variables to be passed)

'Code

end sub

If you wish to create variables to pass into your subroutine, use the ByVal designation if you wish to use the value as a static,
and the Byref designation if you wish for the variable to change and maintain whichever new value it gets.
 
if you code this as a GLOBAL FUNCTION IN A MODULE

so it looks something like this

Code:
FUNCTION myclose()
with screen.activeform
   DoCmd.Close
end with
End function

THEN you can have a button, with a button click event of

=myclose()

and no code is needed in the forms module

You can now just copy the button from form to form, with no more code needed at all


-----
if you have a standard ms switchboard - look at the way the button clicks are rpogrammed. it's a similar idea.
 

Users who are viewing this thread

Back
Top Bottom