command button to do multiple actions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I am a complete beginner and trying to make a command button that closes one
form and opens another form within the same database. I understand that a
command button can only have one action assigned to it (?) - and for multiple
actions I need to code VBA. Therefore i found this code modified it and
saved it as Module1

----------------------------------------------------------------------------------

Private Sub cmdExitForm_Click()
On Error GoTo Err_cmdExitForm_Click

'Close the customers form
DoCmd.Close acForm, "Customers", acSaveYes

'Open the contacts form view mode
DoCmd.OpenForm "Contacts", acNormal, , , acFormEdit, acWindowNormal

Exit_cmdExitForm_Click:
Exit Sub

Err_cmdExitForm_Click:
MsgBox Err.Description, vbExclamation, "Oops, something went wrong with
the command()"
Resume Exit_cmdExitForm_Click
End Su
------------------------------------------------------------------------------------

the code works as when i double click Module1 it runs and perfoms the
action. NOW i want this action to be performed OnClick of a command button
in the Customers form.

HOW DO I DO THIS?

create a macro with the RunCode? I've tried. Can some body give me step by
step instructions how to do this please?
 
JayJay,

Regarding "a command button can only have one action assigned to it",
this is absolutely not true. There is a limit of 999 actions in a
macro, which is probably more than you will need. You can simply make a
macro with the 2 actions you need:
OpenForm
Close
.... and then assign this macro on the On Click event property of the
command buttton.

If you want to use VBA instead, then you can go to the On Click property
of the command button, in design view of your form, enter [event
procedure] in there, and then click the little ellispsis [...] button to
the right. This will open the Visual Basic Editor window, with the
cursor already located between the Private Sub... and End Sub lines. So
just copy/paste the remainder of the code in there. Miind you, some of
the code is incorrect or superfluous. For example, the acSaveYes
applies to design changes to the form, which will not be applicable in
your case. So I would say this is the code:
------------
On Error GoTo Err_cmdExitForm_Click
'Close the customers form
DoCmd.Close acForm, Me.Name

'Open the contacts form view mode
DoCmd.OpenForm "Contacts"

Exit_cmdExitForm_Click:
Exit Sub

Err_cmdExitForm_Click:
MsgBox Err.Description, vbExclamation, "Oops, something went wrong
with the command()"
Resume Exit_cmdExitForm_Click
 
Back
Top