View Full Version : Getting code to run via a button


Jemmo
09-15-2008, 04:57 AM
I have several (VBA) routines that run when a button is clicked on a form. All of these are Private Sub's.

I have a piece of code that I have copied and tweaked from elsewhere, but it is in a separate module as a 'Public Sub'.

How do I get this to run via a button click?

MSAccessRookie
09-15-2008, 05:05 AM
One way is to create a private sub and run it there. For instance, if you have a Public Sub called AddContact, you could do something like this:
Private Sub AddContactButton_Click()
AddContact
End Sub

Jemmo
09-15-2008, 05:18 AM
Thank you!
Thank you!
Thank you!
Thank you!

So stupidly simple when you know.

Thank you!

gemma-the-husky
09-15-2008, 09:33 AM
if you want the button to do that sub/function, and that function only, then declare the common procedure as a function rather than a sub

then on the button event just put the expression

=myfunc() {it has to have the brackets}

no code, it just calls the function

------
one of my clients likes a large close button on every form, instead/as well as the x at the top right

function genericclose
with screen.activeform
docmd.close
end with
end function


then on any button just
=genericclose(), to call it