O'Reilly logo

Access™ 2007 VBA Programmer's Reference by Armen Stein, Geoffrey Griffith, Rob Cooper, Teresa Hennig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.6. Forms as Objects

By now you should have a fair grasp on how to create classes and class objects in Access 2007. Something you might not be aware of is the fact that because form and report modules are also class modules, you can instantiate and use them in exactly the same way as any other class object. The greatest benefits of this are that you can create and operate on more than one instance of the object at any one time, and you can use its events by declaring their object variables using the WithEvents keyword.

Let's say you have a form called Form1. You would, of course, be familiar with the tried and true method of displaying a standard form.

DoCmd.OpenForm "Form1"
DoCmd.Close acForm, "Form1"

Copy the following code into a standard module and try stepping through it using the F8 key.

Public Sub TestFormClass()
    Dim frm As Form_Form1
    Set frm = New Form_Form1

    frm.Visible = True
    Set frm = Nothing
End Sub

Then try the same thing with a report.

Public Sub TestReportClass()
Dim rpt As Report_Report1
    Set rpt = New Report_Report1

    rpt.Visible = True
    Set rpt = Nothing
End Sub

Often, you may want to display a data selection dialog box while editing data in a form, and to return the selected value from the dialog box to the original form. For example, in Microsoft Word, you select Date and Time from the Insert menu. This displays the Date and Time dialog box, from which you select the format of the date you want to insert into your text. You're about to see a mechanism for returning ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required