CONTROLLING CODE WITH CONDITIONAL COMPILATION COMMANDS

Although Accessdoesn't create true executables, it does compile the VBA code to a level lower than the Visual Basic language the developer works with. This compiled code is called pseudocode (also known as P-code). When compiling P-code, certain commands, such as comments, are stripped out to optimize the code.

By using conditional compilation directives, you can include or exclude code sections in the final compiled code. This is different from using just If..Endif to skip over parts of code while it's running. Access literally strips the section of code that's surrounded by the directives. One of the things this means is that you must know which lines of code you won't need at runtime.

One way to use the conditional compilation directives would be to follow the example in the sample application. World Wide Video could have three versions of the system:

  • A video kiosk, so customers can view current and upcoming movies

  • A turn-key retail system, so salespeople can rent movies and sell merchandise

  • An administrative system for management, with accounting and reporting capabilities

By having the conditional compilation directives in the code, you can use the same code base but include only the code used for the particular system, thus saving memory.

Although this last example could work, it's not necessarily practical because the number of forms, reports, and other objects necessary to run each version, and the number of places to put the compiler directives, would be enormous. A more practical example—and how most developers use conditional compilation directives—is to use them for debugging purposes. There's only one conditional compilation directive: #If..#ElseIf..#Else..#End If.

Figure A.15 shows code that prints three variables to the Immediate window if a variable called ccDebug is set to True.

Figure A.15. Conditional compilation commands are useful for excluding debugging commands from distributed applications.


ccDebug is what's called a conditional compiler constant. This special kind of constant can be used only with the conditional compiler directive. You can declare conditional compiler constants in two different ways:

  • Through the user interface on the Project_Name Properties sheet (choose Project_Name Properties from the Tools menu in the VBE). You can see ccDebug in the Conditional Compilation Arguments text box in Figure A.16.

    Figure A.16. You can specify a conditional compiler constant through the Project Properties sheet.

Note

Conditional compiler constants declared through the UI are scoped globally. This type of constant can only be of data type Integer, and can't be a variable, standard constant, or even another conditional compiler constant.


  • Through the #Const command. Conditional compiler constants are visible only at the module level and—unlike those declared through the UI—may be different data types. The #Const statement looks a lot like the standard Const statement. Here's an example using the ccDebug constant:

    #Const ccDebug = –1
    

Note

A conditional compiler constant declared with #Const must be a literal, any data type, or another conditional compiler constant. It can't be a variable or standard constant.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.119.17