Translate

Total Pageviews

Showing posts with label Office. Show all posts
Showing posts with label Office. Show all posts

Conditional Compilation

 Conditional Compilation

In contrast to a normal If statement this condition to executed at compile time and only the appropriate branch is included.
As with other languages it is possible to create "debug" and "release" versions of your code.
You can also use the #Const directive to create other compiler variables.

#Const g_Conditional = 1 

You can define a constant in (Project Properties)(General tab) in the text field Conditional Compilation Arguments

In the program code you can then make use of the value of this constant with #IF statements.
There is no loss in speed of execution.

#If g_Conditional = 1 Then 

#Else

#EndIf

For example you may want to include extra message boxes, or use Debug.Print or Debug.Assert statements while you are developing and testing your code but don't want these incorporated when you distribute the code to the users.
Use the "#Const" directive to create a compiler variable. (#IF .THEN #ELSE #ELSEIF, #CONST).
Then use delimiter blocks using the compiler directive to include the extra blocks of code.
These are global in scope, ie they apply to the whole project.
The Option Compare statement does not affect expression in a #IF and #ELSEIF statements and these are always evaluated using Option Compare Text.


Keywords

Certain keywords cannot be used for your constants.
For example "debug" cannot be used.

#Const debug = 1 

With - End With

 With - End With

If you are going to perform several different actions on the same object use the With ....... End With.
This enables you to perform multiple operations on a single object.
After an object is assigned to a variable VBA can access it more quickly than it can a lengthy reference that has to be resolved.
The fewer the dots, the faster the processing time.
Another way to improve the speed is to use the With - End construct.

With Selection.Font 
   .Name = "Arial"
   .Size = 12
End With

link - docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement


Important

Using With-End can speed things up but it does make the code harder to debug as you can't quickly drag expressions into the Watch Window.

Comment in Access VBA

 

Rem Keyword

The apostrophe (') is the preferred comment indicator, although you can use the "Rem" keyword if you want.
This is a rollover from the BASIC days.
Until the apostrophe, the Rem keyword can only be written at the start of the line and not on the same line (after an instruction).

REM my comment