Access VBA Macro Programming

(Joao Candeias) #1
Of course, many other properties exist, but this gives you an idea of what some properties
look like.
Examples of object methods on theCurrentDbobject include the following:

Method Description
CreateQueryDef Creates a new query based on tables within the database
CreateTableDef Creates a new table in the database
Execute Executes a SQL statement on the database—an extremely useful method
NewPassword Changes the password for the database or sets a new one if there is no previous
password

Properties can be either read-only or read/write. Read-only means you can access the
property’s value (setting) but not change it. Read/write means you can both access and
change a property’s value. This can have an important effect on your program because you
may find your code writing to properties that can only be read.
To preserve the integrity of the other property’s object model, it may be important to keep
the property read-only.
For example, theCountproperty of any collection of objects is always read-only. Altering
theCountofTableDefswithin aTableDefscollection could lead to very unpredictable results.
For instance, there might be ten tables within the database. If you could change it to five tables,
you would lose tables because they would be deleted!
Methods are effectively like subroutines or shortcuts to actions that you can call from your
code to perform certain actions, such as setting a password on the database or opening a recordset
based on a table. To try writing the code to set a password would be impossible because you
do not know the finer points of the encryption system used for the password and where it is
stored in the Access file structure. Without VBA, you’d need to know the intricacies of C and
have the source code to set the password. However, Microsoft did all the hard work for us.
They know the answers to all these questions, so all you need is one line of code calling the
NewPasswordmethod. With methods, you usually pass parameters as well—for example,
theNewPasswordmethod has to be given parameters for the old password and the new
password.
Some parameters are optional when calling a method. They are indicated as being optional
by being shown in square brackets ([ ]).

Manipulating Properties


If a property is read/write, it can be manipulated. This means you can substitute other values
into it to provide different effects, depending on the object and the property. For example,
you may want to use code to alter the SQL in a query. You can do this by writing your new
SQL to theSQLproperty in theQueryDefobject for that query.
Properties are generally manipulated by using code at runtime, when your program is
executing. However, some properties are available at design time and can be changed using
the Properties window within VBE. Design time is when you are viewing the code window
and designing and making changes to your code.

178 Microsoft Access 2010 VBA Macro Programming

Free download pdf