< Day Day Up > |
Enter code the same way you would enter data in a text editor just start entering the appropriate declarations, variables, and statements into a module. Once you're proficient with VBA code, that's probably exactly how you'll do it. Until then, you might want a little help. That's what the Insert Procedures dialog box is for.
Follow these steps to create a new procedure:
Don't worry if you don't know which options to choose yet. Figure 2.5 shows us creating a public sub procedure named OpenClientForm. Figure 2.5. Set these options to define a new procedure.
When you click OK, the VBE enters what's known as a stub, as shown in Figure 2.6. A stub identifies the procedure as either a sub or function procedure, contains the procedure's name, and provides the procedure's required End statement. The stub doesn't contain any executable code that's your job. Figure 2.6. The VBE enters the new procedure's stub.![]() Whether you start with a VBE generated stub or enter it yourself, it's your job to add the code. That's what you'll be learning to do throughout this book. For now, you're probably itching to actually accomplish something, so complete the procedure by inserting the additional code between the two provided statements as follows: Public Sub OpenClientForm() ' Open the client form DoCmd.OpenForm "Clients" Debug.Print "The form is open" End Sub If the Immediate Window isn't already open at this point, open it by pressing Ctrl+G. Now, run the procedure by inserting the mouse pointer anywhere inside the actual procedure (in the module) and pressing F5. Two things will happen. First, Access will open the Clients form shown in Figure 2.7 in the Access window, just as if you'd opened it by hand even though you won't readily see it. (Switch to the Access window to view the opened form if you like, but it isn't necessary.) Second, the procedure will print the message shown in Figure 2.8 in the Immediate window. Figure 2.7. Running the procedure opens an Access form.Figure 2.8. The procedure displays a message in the Immediate window.![]() Normally, you won't display values in the Immediate window. Instead, you'll use them in your code somehow. For now, it's enough to see that you can display results in the Immediate window, which is a great way to debug your code. Saving the CodeTo save a new module or to save changes made to an existing module, click the Save tool on the Standard toolbar or choose File, Save project. When saving a new module, Access 2003 displays the Save As dialog box. Accept the default name or enter a new one, as shown in Figure 2.9, and choose OK. Figure 2.9. Be sure to save a new module.CAUTION You can close the VBE without losing unsaved code, although it isn't advisable. However, once you close a module any unsaved code is lost. Notice that the new module is listed in the Project Explorer as shown in Figure 2.10. (It was there as Module1.) It's also worth noting that highlighting the new module (Chapter2) in the Project Explorer updates the contents of the Properties window. Currently, there are no properties to display other than the name of the module. You'll become accustomed to working with and even depending upon these windows. Figure 2.10. Saving the module updates its name in the Project Explorer. |
< Day Day Up > |