From the course: Access: VBA

Run a menu command

From the course: Access: VBA

Start my 1-month free trial

Run a menu command

- [Instructor] The typical Access application limits what users can do, usually by hiding significant chunks of the user interface. If you want to allow your users to take actions they would usually initiate through the user interface, you can attach a run command code segment to a command button. I'll demonstrate how to do that in this movie. I have opened the course database and displayed the product form. I want to add a command button to this form, so that means I need to work with it in design view. On the home tab of the ribbon, I'll go to the view button, click it's down arrow, and click design view. I will resize the detail section and then on the design contextual tab of the ribbon, which displayed for me because I'm looking at the form in design view, I'll go to the controls gallery, click the button item, and then click inside of the detail section on a blank spot. And you can see my command button was created. I don't want to use the command button wizard, so I'll click cancel and I will edit the command button's label to say export and click away. Now that I have the button on the form, I can add code to it. So I'll right click, click build event, click code builder, and click OK. And here you see that I have a command 14 underscore click event handler, so whenever this command button is clicked it will run this command. I'll press enter a couple of times to give myself some space to work. To run a menu command from a command button, you use do command then a period and run command so we're using the do command objects run command method followed by a left parentheses and then you need to give the command that you want to use. And, as you can see, there is a huge list. They start with A-C-C-M-D, and then you have Align Bottom, Align Center, Align Left, and so on. In this case, we will use A-C-C-M-D, and that's common for all the commands you're going to run, ACCMD stands for Access Command, and then Export Excel followed by a left parentheses. So whenever the form is open and someone clicks the button, Access will attempt to export the data to excel. I'll press Alt + F11 to move back to the visual based editor, change back to form view, so I'll go to the view control and click the main body of the button which takes us back to form view, click export and we can select the destination for the export, the file format, and everything else from the usual Access Export functionality. I won't go through with it in this case. It's just a series of wizard steps that you can go through so I'll click cancel, and now we have our code in place to export our data whenever we want to from the products form.

Contents