Access VBA Macro Programming

(Joao Candeias) #1

Years as Integer
End Type


This creates a new type calledEmployee, which holds information for Name, Salary, and
Years of Service.
You can then use this type in exactly the same way as the built-in variable types. It is even
automatically included in the drop-down lists within the VBA editor. You can use these as
normal variables, as seen in the following:


Dim temp As employee
temp.Name = "Richard Shepherd"
temp.Salary = 10000
temp.Years = 5
MsgBox temp.Name
MsgBox temp.Salary
MsgBox temp.Years


Note that the variable name has a list box showing the properties or fields for this data type as
you type in the variable name within the code.
You can also create an array of this type and effectively use it as an object in its own right.
Notice that after specifying the name of the array and the subscript index that will receive the
data, a dot is used soNameappears as a property:


Dim temp( 10 ) as employee
temp( 0 ).Name = "Richard Shepherd"


Constants


Constantsare, in effect, variables that do not change. They hold values that are used
repeatedly within your code and effectively provide shorthand for that particular constant
value.
You use the same rules to create a constant as you do to create variables, but you cannot
assign a new value to that constant from within your code as you can with a variable.


Const Path_Name = "C:\temp\"


The preceding code sets up a constant calledPath_Name, which here always has the value:
C:\temp. Thus, you can use this in your program every time you want to use that particular
path.
The Access object model also has predefined constants, which you can see by using the
Object Browser (covered in Chapter 12). In the Access object model, all constants begin
with the letters “xl” to denote that they are part of the Access object model—for example,
xlSaveChangesorxlDoNotSaveChanges. The Object Browser also shows the actual value
of the constant at the bottom of the browser window.


Chapter 2: Variables, Arrays, Constants, and Data Types 25

Free download pdf