1
votes

I am very new to vba and I am trying to get my first bit of VBA code to run, I have added a button to a form. I then opened the on click event and placed

Private Sub run_Click()
MsgBox "hello World"
End Sub`

into the module but when I go back into the form and click the button in form view nothing happens, I am doing something wrong, i have tryed to run lots of other sniptes of code but nothing is working, I am kinda unsure how I actaully "run" the code.

enter image description here

I Click run then I get this

enter image description here

I have gone to the trust centre and Enabled Macros and still I get this error ??

// This issue has been solved my marcos were not enabled in the trust centre

3
Make sure your button is called run and your are not using a reserved name to name it. Try Private Sub CommandButton1_Click() MsgBox "hello World" End Subuser2140173
Yes im sure the button name and vba code match, I have uploaded a photo to show you! i have followed a few differnt tutorials and still nothing worksBawn

3 Answers

6
votes

If this is a brand new database then it may not be "trusted" yet. Exit Access completely, then try opening the database again. if you see a warning like...

Security Warning: Some active content has been disabled.

...along with an "Enable Content" button then click that button (to enable macros and VBA code) and then try your form again.

1
votes

In your VBA editor do this:

  1. Create a userform and rename it to yourFormName Rename your form
  2. Create a button on the form and rename it to yourButton Rename your button
  3. Double-click your button and paste the below
    Private Sub yourButton_Click()
    MsgBox "hello world"
    Me.Hide
    End Sub

  4. Insert a module and paste this code in
    Sub RunMain()
    With yourFormName
    yourFormName.Show
    Unload yourFormName
    End With
    End Sub

  5. Run the RunMain macro and then click the button!
1
votes

When MS Access first opens on your desktop and it has VBA within, you'll need to enable that code. In MS Access 2010, File --> Options --> Trust Center --> Macro Settings as well as ActiveX Settings. The security nanny-statists have set the default so VBA will not run unless you specifically allow it.