Using Visual Basic for Applications Efficiently in Access 2013
Part of the Access 2013 All-In-One For Dummies Cheat Sheet
When you’re automating an Access 2013 database application by using Visual Basic for Applications (VBA), you can get lost with a blank page to begin writing code. Where do you start? Here are a few simple guidelines to follow that will have you writing VBA code like a pro:
Press F1 at any time to start Microsoft Access help, where you can see examples of VBA code.
Use the Object Browser.
The Object Browser lets you explore the properties and methods of the objects in VBA. In the Visual Basic Editor, choose View, then Object Browser or simply press F2.
Get more help.
Sure, the built-in help tools within VBA are wonderful, but you can also use your favorite browser to search the Internet for help on writing VBA code. You can even find examples that you can steal — or borrow — for your own project.
Handle your errors.
Even the most perfect programmer can’t stop errors from occurring, but he or she can stop them from bringing a program to a grinding halt. Use VBA’s built-in error handling with the On Error Goto and Resume statements to trap errors and change the program flow so that your applications don’t frustrate the people who use them.
Use functions and sub procedures.
Employ functions and sub procedures to handle tasks that will be performed by different areas of your program. As a general rule, if you find yourself copying and pasting code from one area of a program to another, you may want to put that code in its own procedure.
Convert macros to VBA code.
The macro designer in Access 2013 lets you choose among lists of predefined tasks to automate your application. Build a macro that does what you want and then convert that macro to VBA code so you can see how it would look if you’d typed it from scratch.