how to put a code in to the on click event of the command button?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nuwan1988
    New Member
    • Apr 2014
    • 3

    how to put a code in to the on click event of the command button?

    i want to print my access form page with its details. i have got the following code and now i want to put this code to the on click event...

    Code:
     Private Sub CommandBtn_Click()
    Dim MyForm As Form
        Set MyForm = Screen.ActiveForm
        DoCmd.SelectObject acForm, MyForm.Name, True
        DoCmd.PrintOut
        DoCmd.SelectObject acForm, MyForm.Name, False
     
    End Sub
    Last edited by Rabbit; Apr 21 '14, 03:34 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    It looks like it is already in the OnClick event based on the "_Click" at the end of the procedure name. If you go into design view on the form, click the button that you want this code to be behind, click on the events tab of the property window (press F4 if not visible) and then click inside the OnClick event box. You will see an ellipsis button (three dots). If you don't see Event Procedure inside the OnClick event box, then when you click the ellipsis button, you will be asked what type of event you want to build. I can't remember off hand if it says Code Module or Event Procedure, but the other options are Macros and Expression Builder and you don't want these. It will then create the event sub and you can then copy the code into it.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      [event procedure]
      will be in the proper event property of the control for VBA.

      Please review this link for some options that should be set along with some basic troubleshooting :[*]> Before Posting (VBA or SQL) Code

      Comment

      Working...