jia103
jia103

Reputation: 1134

Run a VBA sub from command button in Access

I'm having trouble attaching a VBA macro to a form button in MS Access. I have my public sub Demo defined in a Module1 under VBA. In Access, I ensured macros are enabled because I can click in the VBA window and run my macro by pressing F5.

In Access, I created a new blank form (Ribbon > Create > Forms > Blank Form). On the form, I went into Design View. Then Ribbon > Design > Controls > Ensure "Use Control Wizards" is pressed.

Then I pressed Button and dropped a new command button onto my form. In the Command Button Wizard, I clicked Miscellaneous under Categories, and then Run Macro under Actions, and pressed Next.

I expected to see a list of modules or macros listed in the list on the following screen that prompts, "What macro would you like the command button to run?"

Instead, the list is empty. Where do I find the macro to add to the command button?

Upvotes: 1

Views: 21913

Answers (1)

Johnny Bones
Johnny Bones

Reputation: 8414

  1. Go into the Design View of your form.
  2. Press the ALT key and the F11 key simultaneously, and you will see the VBA Code window.
  3. Find your form and double-click it, which will bring up the code behind your form.

    Find your button, and put this piece of code in it:

    DoCmd.RunMacro "YourMacroName"

Obviously, replace "YourMacroName" with the name of your macro, but keep it inside the double quotes.

Upvotes: 2

Related Questions