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?
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.
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 ()
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