Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



Harnessing the Power of the Immediate Window


The Immediate window serves several purposes. It provides you with a great way to test VBA and user-defined functions, it enables you to inquire about and change the value of variables while your code is running, and it enables you to view the results of Debug.Print statements. To open the Immediate window while in the Visual Basic Editor, do one of three things:

  • Click the Immediate window tool on the Debug toolbar.

  • Choose View, Immediate window.

  • Press Ctrl+G.


NOTE

An advantage of pressing Ctrl+G is that it invokes the Immediate window without a Code window being active. You can click the Immediate window toolbar button or choose View, Immediate window only from within the VBE.

Figure 15.1 shows the Immediate window.

Figure 15.1. The Immediate window enables you to test functions and to inquire about and change the value of variables.


NOTE

The Debug tools are available on a separate toolbar. To show the Debug toolbar, right-click any toolbar or menu bar and select Debug from the list of available toolbars.

Testing Values of Variables and Properties


The Immediate window enables you to test the values of variables and properties as your code executes. This can be quite enlightening as to what is actually happening within your code.

To practice with the Immediate window, you do not even need to be executing code. To invoke the Immediate window while in a form, report, or module, press Ctrl+G. To see how this works, follow these steps:


  • Run the frmClients form from the CHAP15EX.MDB database on the accompanying CD-ROM.

  • Press Ctrl+G to open and activate the Immediate window. Access places you in the VBE within the Immediate window.

  • Type

    ?Forms!frmClients.txtClientID.Value and press Enter. The client ID of the current client appears on the next line.

  • Type

    ?Forms!frmClients.txtCompanyName.Visible and press Enter. The word True appears on the next line, indicating that the control is visible.

  • Type

    ?Forms!frmClients.txtContactTitle.BackColor and press Enter. The number associated with the BackColor of the Contact Title text box appears on the next line.


  • Your screen should look like the one shown in Figure 15.2. You can continue to request the values of properties or variables within your VBA code.

    Figure 15.2. Use the Immediate window to test the values of properties.


    Setting Values of Variables and Properties


    Not only can you display things in the Immediate window, you also can use the Immediate window to modify the values of variables and controls as your code executes. This feature becomes even more valuable when you realize that you can re-execute code within a procedure after changing the value of a variable. Here's how this process works:


  • Invoke the Immediate window, if necessary. Remember that you can do this by pressing Ctrl+G.

  • Type

    Forms!frmClients.txtContactTitle.Value = "Hello" in the immediate pane. Press Enter. The contact title of the current record changes to Hello.

  • Type

    Forms!frmClients.txtIntroDate.Visible = False . Press Enter. Access hides the txtIntroDate control on the frmClients form.

  • Type

    Forms!frmClients.txtClientID.BackColor = 123456 . Press Enter. The background color of the txtClientID control on the frmClients form turns green. The Immediate window and your form now look like those shown in Figures 15.3 and 15.4, respectively.

    Figure 15.3. Set the values of properties using the Immediate window.


    Figure 15.4. The results of using the Immediate window to set the values of properties are shown here.


  • The Immediate window is an extremely valuable testing and debugging tool. The examples here barely begin to illustrate its power and flexibility.

    CAUTION

    Changes you make to data while working in the Immediate window are permanent. On the other hand, Access does not save changes you make to the properties of controls or the values of variables with the form or report.

    Some people think that data changes made in the Immediate window are not permanent. In other words, if you modify the last name of a customer, they believe that the change will not be permanent (but, of course, it is). Other people think that if they change the BackColor property of a control, the change will persist in the design environment (but, of course, it won't).

    Clearing the Immediate Window


    The Immediate window displays the last 200 lines of output. As you add additional lines of code to the Immediate window, older lines disappear. When you exit completely from Access, it clears the Immediate window. If you want to clear the Immediate window at any other time, follow these three steps:


  • With the Immediate window active, press Ctrl+Home to go to the top of the Immediate window.

  • Hold down your Shift key and press Ctrl+End to go to the last statement in the Immediate window.

  • Press Delete.


  • Practicing with the Built-In Functions


    In addition to being able to test and set the values of properties and variables using the Immediate window, you can test any VBA function. To do so, type the function and its arguments in the Immediate window, preceded by a question mark. This code returns the month of the current date, for example:

    ?datepart("m",date)

    This tells you the date one month after today's date:

    ?dateadd("m",1,date)

    This tells you how many days exist between the current date and the end of the millennium:

    ?datediff("d",date(),#12/31/2999#)

    Executing Subroutines, Functions, and Methods


    In addition to enabling you to test any VBA function, the Immediate window lets you test any user-defined subroutine, function, or method. This is a great way to debug your user-defined procedures. To see how this works, follow these steps:


  • Open the basExamples module found in the CHAP15EX.MDB database on the accompanying CD-ROM.

  • Invoke the Immediate window if it is not already visible.

  • Type

    ?ReturnInitsFunc("Bill","Gates") . This calls the user-defined function ReturnInitsFunc, sending "Bill" as the first parameter and "Gates" as the second parameter. The value B.G. appears in the Immediate window. This is the return value from the function.

  • Type

    Call ReturnInitsSub("Bill","Gates") . This calls the user-defined subroutine ReturnInitsSub, sending "Bill" as the first parameter and "Gates" as the second parameter. The value B.G. appears in a message box.


  • Notice the difference between how you call a function and how you call a subroutine. Because the function returns a value, you must call it using a question mark. On the other hand, when calling a subroutine, you use the Call keyword.

    NOTE

    You also can call a subroutine from the Immediate window using this syntax:

    RoutineName Parameter1, Parameter2, ....

    Notice that, when you omit the Call keyword, you do not need to enclose the parameters in parentheses.

    Printing to the Immediate Window at Runtime


    The capability to print to the Immediate window is useful because you can test what is happening as your code executes without having to suspend code execution. It also is valuable to be able to print something to a window when you are testing, without interfering with the user-interface aspect of your code. You can test a form without being interrupted and then go back and view the values of variables and so on. Here's how the process works:


  • Type

    Call LoopThroughCollection in the Immediate window. This calls the user-defined subroutine LoopThroughCollection. The values Skating, Basketball, Hockey, and Skiing appear. The routine prints these values to the Immediate window.

  • Open the form frmDebugPrint in Form view.

  • Press Tab to move from the First Name field to the Last Name field.

  • Press Tab to move back to the First Name field.

  • Type your first name.

  • Open the Immediate window. Notice that the routine sent all the statements to the Immediate window (see Figure 15.5). I have coded these Debug.Print statements in all the appropriate form and control events.

    Figure 15.5. Use Debug.Print statements to print values to the Immediate window.


  • NOTE

    Although it is good practice to remove Debug.Print statements when you have completed the debugging process, you can safely deploy your applications without removing them. Your users will never know that the statements are in your code unless they view the Immediate window. The Debug.Print statements result in only a minor degradation in performance.


    / 544