Public Variables

In the previous section, you learned that by declaring a variable with the Dim or Private statement at the top of the module, you can make this variable available to other procedures in that module. But what if you want your variable to be available to all other procedures in all modules in the current Microsoft Access database as well as in other Microsoft Access databases that your procedure code may be referencing? To meet this demand, you need a module-level variable with a Public scope. Module-level variables declared with the Public statement can be used in any Visual Basic for Applications module. When you want to work with a variable in all the procedures in your application, declare it with the Public keyword, like this:

Option Explicit

Public slsTax As Single

Sub CalcCost()

...Instructions of the procedure...

End Sub

The slsTax variable declared at the top of the module with the Public statement will now be available to any other procedure in your application.

Introduction to Access 2003 VBA Programming

0 0

Post a comment