About this lesson
Create a form to use as a Main Menu and make a command button to open a form using a macro.
Download this lesson’s related exercise files.Services_35_Start Main Menu Command Buttons to Open Forms, Macro, VBA.accdb
1.7 MB Services_35_Main Menu Command Buttons to Open Forms, Macro, VBA.accdb
Command Buttons to Open Forms using a Macro or VBA
Macro to Open a Form
VBA Procedure to Open a Form on a Click event
This VBA procedure opens a form called "frm_SERVICE_TYPES". The name of the control is cmd_Services. The event is Click.
If Intellisense doesn't come on, press to trigger it
Help for OpenForm
This is the Help from Microsoft for the OpenForm Macro Action.
The OpenForm action has the following arguments.
This action is similar to double-clicking a form in the Navigation Pane, or right-clicking the form in the Navigation Pane and then selecting a view.
A form can be modal (it must be closed or hidden before the user can perform any other action) or modeless (the user can move to other windows while the form is open). It can also be a pop-up form (a form used to collect or display information that remains on top of all other Access windows). You set the Modal and PopUp properties when you design the form. If you use Normal for the Window Mode argument, the form opens in the mode specified by these property settings. If you use Dialog for the Window Mode argument, these properties are both set to Yes. A form opened as hidden or as an icon returns to the mode specified by its property settings when you show or restore it.
When you open a form with the Window Mode argument set to Dialog, Access suspends the macro until the form is closed or hidden. You can hide a form by setting its Visible property to No by using the SetValue action.
The filter and WHERE condition you apply become the setting of the form's Filter property.
Create a Main Menu Form
- To create a form to use as a Main Menu, choose Form Design from the CREATE ribbon.
- Go to the Design View and click in the upper left, where the Rulers intersect, to select the form.
- If the Property Sheet is not showing, turn it on by pressing (ALT-ENTER).
- Set the form Caption property: MAIN MENU
- Set the form Record Selectors property: No
- Set the form Navigation Buttons property: No
- Save the form and call it f_MAINMENU.
Command Button to Open a Form Using a Macro
- Click on the Button tool in the Controls group of the FORM DESIGN TOOLS, DESIGN ribbon.
- Click on the form where you want to put a button.
- When the Command Button Wizard comes up, choose Form Operations from Categories on the left.
- Choose Open Form from Actions.
- Click Next.
- From the list of forms, choose the form name you wish to open.
- Click Next.
- Choose the default, "Open the form and show all the records", and click Next.
- Change the option to Text, instead of Picture, specify the Caption for the button (i.e., "Work").
- Click Next.
- For the name of the button, type cmd_something (i.e., cmd_OpenWork).
- Click Finish.
- Resize the button and change other properties, if desired.
Command Button to Open a Form Using VBA
- Click on the Button tool in the Controls group of the FORM DESIGN TOOLS, DESIGN ribbon to pick up the button tool.
- Click on the form where you want to put a button. If the Command Button Wizard comes up, click Cancel.
- Choose the Event tab on the Property Sheet. In the Click event for the button, type [ and choose [Event Procedure]
- Click the Builder button (…) or press (Ctrl-F2)
- You will be placed in VBA between the procedure declaration statement and the End Sub statement. Press (Tab) to indent the line.
- Type 'date to create a comment with the date
- Press (Enter) to make a new line
WHERE FormName is the name of the form you want to open.
- From the menu in VBA, choose Debug, Compile and then Save.
Animate Command Button
- To animate a command button so when the mouse hovers over it, the colors will change, select the command button in Design View.
- On the Format tab of the Property Sheet, set Back Color and Border Color to the same color.
- Set Fore Color to a contrasting color
- For the Hover colors, reverse the settings for Back Color and Fore Color. Hover Color = Fore Color
- Hover Fore Color = Back Color
- For the Pressed color, which you probably won't see because a form will open, set Pressed Color = Hover Color (or maybe slightly different)
- Set Pressed Fore Color = a color similar but different than Hover Fore Color.
Naturally, you may choose any colors you want! This is what I generally do.
Lesson notes are only available for subscribers.