Entering and Running VBA Code

 < 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.

A


Follow these steps to create a new procedure:

  1. If you don't already have a new open module (refer to Figure 2.4) in which to enter a procedure, choose Insert, Module to create one.

  2. Choose Insert, Procedure to display the Insert Procedure dialog box.

  3. Enter a descriptive but short name for the new procedure. In this case, use the name OpenClientForm.

  4. Click a Type and a Scope option. When creating static variables, click the All Local Variables as Statics option. For this example, choose Sub and Public and ignore the static variable option.

  5. Click OK when you're ready to create the 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.

graphics/02fig05.jpg


Youll learn more about scope in "Declaring Procedures as Public or Private," p. 48.


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.

graphics/02fig06.jpg


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.

graphics/02fig07.jpg


Figure 2.8. The procedure displays a message in the Immediate window.

graphics/02fig08.jpg


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 Code

To 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.

graphics/02fig09.jpg


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.

graphics/02fig10.jpg


     < Day Day Up > 


    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net