Understanding and Using Conditional Compilation

When you run a procedure for the first time, Visual Basic converts your VBA statements into the machine code understood by the computer. This process is called compiling. You can also perform the compilation of your entire VBA project before you run the procedure by choosing Debug | Compile (name of the current VBA project). You can tell Visual Basic to include or ignore certain blocks of code when compiling and running by using conditional compilation. Your procedure may behave differently depending on the condition you set. For example, the conditional compilation is used to compile an application that will be run on different platforms (Windows or Macintosh, Win16 or Win32 bit). The conditional compilation is also useful in localizing an application for different languages. The program code excluded during the conditional compilation is omitted from the final file, thus it has no effect on the size or performance of the program.

Conditional compilation is enabled via special expressions called directives. First, you need to declare a Boolean (True, False) constant by using the #Const directive. Next, you check this constant inside the #If...Then... #Else directive. The portion of code that you want to compile conditionally must be surrounded by these directives. Notice that the If and Else keywords are preceded by a pound sign (#). If a portion of code is to be run, the value of the conditional constant has to be set to True (-1). Otherwise, the value of this constant should be set to False (0). You declare the conditional constant in the declaration section of the module. For example,

#Const User = True declares the conditional constant named User.

Part I

In the hands-on that follows, data is displayed in the Spanish language when the conditional constant named verSpanish is True. The WhatDay procedure calls the DayOfWeek function, which returns the name of the day based on the supplied date. To compile the program in the English language, all you have to do is change the conditional constant to False, and Visual Basic will jump to the block of instructions located after the #Else directive.

© Hands-On 9-6: Using Conditional Compilation

1. Insert a new module and name it Conditional.

2. Enter the WhatDay and DayOfWeek function procedures as shown below.

' declare a conditional compiler constant #Const verSpanish = True

Sub WhatDay()

Dim dayNr As Integer #If verSpanish = True Then dayNr = Weekday(InputBox("Entre la fecha, por ejemplo 01/01/2001")) MsgBox "Sera " & DayOfWeek(dayNr) & "." #Else

WeekdayName #End If End Sub

Function DayOfWeek(dayNr As Integer) As String

DayOfWeek = Choose(dayNr, "Domingo", "Lunes", "Martes", _ "Miercoles", "Jueves", "Viernes", "Sabado") End Function

Function WeekdayName() As String

Select Case Weekday(InputBox("Enter date, e.g., 01/01/2000"))

Case 1

WeekdayName =


Case 2

WeekdayName =


Case 3

WeekdayName =


Case 4

WeekdayName =


Case 5

WeekdayName =


Case 6

WeekdayName =


Case 7

WeekdayName =


End Select

MsgBox "It will be " & WeekdayName & "." End Function

MsgBox "It will be " & WeekdayName & "." End Function

3. Run the WhatDay procedure.

Because the conditional compilation constant (verSpanish) is set to True at the top of the module, Visual Basic runs the Spanish version of the

Introduction to Access 2003 VBA Programming

WhatDay procedure. It asks for the user's input in Spanish and displays the result in Spanish. To run the English version of the code, change the verSpanish constant to False, and rerun the procedure.

Instead of declaring the conditional compiler constants at the top of a module, you can choose Tools | (Debugging) Properties (see Figure 9-15). When you use the Properties window, use the following syntax in the Conditional Compilation Arguments text box to enable the English version of the WhatDay procedure:

verSpanish = 0

If there are more conditional compilation constants, each of the constants must be separated by a colon.

Figure 9-15: The conditional compilation constant can be declared either at the top of the module or in the Properties window, but never in both places.

CustomP reject ÍM - Project Properties


General | Protection [

Project Name:

Project Description: |

Help File Name: 1

Project Help Context ID:

J 1»

Conditional Compilation Argurisnhs:

| verSpanish = 0

i °K |

[true Ibb

4. Comment out the #Const verSpanish directive at the top of the module, and enter the conditional compilation constant in the Properties window as shown in Figure 9-15. Then run the WhatDay procedure to see how the Else branch of your program is now executed for English-speaking users.

0 0

Post a comment