Access has thousands of places where one or two lines of code added to an "event procedure" can fix a problem or dramatically improve an application.
At certain points in the program flow (when the user enters data, for instance, or runs a query), Access checks to see whether a developer has added any custom code. For example, every time a report is opened, Access sees if any code has been added to that report's Open event. If code has been added, Access runs it before it opens the report. This is powerful!
To cite a very simple example, you can automatically maximize a report window by dropping a single line of code in the report's Open event, such as:
DoCmd.Maximize
Here's how:
Open the report in Design View, and then open its properties sheet (View → Properties).
-
On the Event tab (see Figure A), click in the On Open field, and then click the Build (…) button that appears. If you get a Choose Builder dialog, select "Code Builder" and then click OK. The Visual Basic Editor will open and create the appropriate empty event procedure for you. The empty procedure will look something like this:
Private Sub Report_Open(Cancel As Integer) End Sub
Figure A. The Event tab shows the global report events—click in a particular field, then click the Build (…) button on the right to create an event procedure.
The procedure starts executing at the top and continues line-by-line until it gets to the bottom. Simply add your code on the blank line in between the top and bottom lines, so it looks like this:
Private Sub Report_Open(Cancel As Integer) DoCmd.Maximize End Sub
Indentation and blank lines have no effect on what your code does, but they do help to make code more readable.
That's all there is to it. Of course, the more you learn Visual Basic, the better you'll be able to take advantage of these event hooks.
Learn more about this topic from Fixing Access Annoyances.
Access is one of the easier and more flexible database management solutions for organizing data. You could grab other books for help, but do they solve problems from page one? Meet a book of a different stripe where newcomers start off right and learn valuable methods for building databases while seasoned pros gain efficiency. Fixing Access Annoyances saves time and bouts of hair pulling.

Help


