Access VBA Macro Programming

(Joao Candeias) #1
CurrentDb
CurrentDbis a top-level DAO (Data Access Objects) object. It allows you to create recordsets
and use your VBA code to interact with them. Because it is one of the main objects, it is dealt
with in more detail in this chapter.

CurrentProject
TheCurrentProjectobject represents the actual Access project and contains the details of
all forms, reports, macros, and modules.
These are included in the following collections:

AllForms
AllReports
AllMacros
AllModules

These collections are very useful since they will provide details of all the forms and
reports within the project, not just the ones that are actually loaded at that point in time. For
example, there is also a Forms collection under the Application object, but this only refers to
forms that are open, which can be restrictive.
You can refer to an AllForms collection as follows:

MsgBox CurrentProject.AllForms("MyForm").DateCreated

This will display the date and time that the form MyForm was created. Chapter 32 (Create
an Objects Inventory) shows how to use these collections further.
A very useful property of theCurrentProjectobject is the Path property. This can be used
to supply the path that the database was loaded in from:

MsgBox CurrentProject.Path

When you distribute an Access application, users may load it from completely different
pathnames, depending on how their network drives are mapped. You may wish to interact
with other files within your Access folder, such as importing or exporting to text or Excel
files.
When the user loads your application you do not know exactly what the path is back to the
folder where the application was loaded from, but the Path property will tell you.
This is very useful also if the application is moved from one server to another, such as
from a test environment to a live environment. If you hardcoded the pathnames, you would
need to make changes within the code and this can easily lead to errors. Using the Path
property gets around this.

DAvg
This methodDAvgallows you to extract the average value for a numeric field from a specified
table. An example is:

MsgBox DAvg("MyField", "MyTable")

194 Microsoft Access 2010 VBA Macro Programming

Free download pdf