Attach an existing VBA sub to a new button (1 Viewer)

JimL

Registered User.
Local time
Today, 06:56
Joined
Jul 9, 2019
Messages
20
I have looked at various searches on this but have not been able to determine how to create a button and then associate it with an existing VBA sub.
`
Can anyone help, please?

Thanks
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:56
Joined
Aug 30, 2003
Messages
36,118
If the function/sub is in a standard module, you just call it by name:

http://www.baldyweb.com/Function.htm

If it's an existing event procedure (which I personally wouldn't do), you have to make it public and then call it like

Forms!FormName.Command3_Click
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:56
Joined
Oct 29, 2018
Messages
21,358
Hi. If you have a certain procedure that needs to be executed from multiple events or scenarios, you might consider moving it to a Public Sub or Function.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:56
Joined
Feb 19, 2002
Messages
42,981
Using procedures in other forms is generally avoided because it requires using the form name specifically to call them and also requires Access to load the form into memory. Shared procedures should be stored in a standard module which is "lighter" to load than a form with its class module.

Personally, I am old school and for procedures, I ALWAYS use "Call"
Code:
Call myProcName
Although you can just use the name without "call"
Code:
myProcName

If you want to call a function, you would assign its return value to some other field
Code:
Me.somecontrol = myFuncName

Both functions and procedures can take arguments. Again, I am old school and consistant. I ALWAYS enclose the arguments in ()
Code:
Call myProcName(var1, var2, var3)
myProcName(var1, var2, var3)
Me.somecontrol = myFuncname(var1, var2, var3)

Consistency is your FRIEND when you are coding. The fewer decisions you need to make as you are typing, the less errors you will make.

And don't forget, as the others have mentioned, you must make the function/procedure public if you want to use it from outside the module that contains it UNLESS you are willing to always prefix it with its modulename:
Call somemodulename.myProcName
 

Users who are viewing this thread

Top Bottom