Developers sometimes feel nervous about switching to code because they fear getting themselves into a complex world without all the commands they have available with macros. This is far from true.
Although getting into code can be confusing at first, switching to VBA not only gives you more flexibility, it gives you more control. Most macro commands are covered with the DoCmd object, described in the following section; others either have equivalent commands or aren't needed in code.
In code, most of the macro actions can be performed using the DoCmd object. The actions are used as methods that can be called with arguments. Most of the arguments have intrinsic constants, which can be used in place of numbers. An example of this is using the acCmdSaveRecord constant with the RunCommand method to save the current record in a form:
DoCmd.RunCommand acCmdSaveRecord
Note
In versions of Access before Office 97, the DoMenuItem command performs the same commands as RunCommand. Access automatically changes DoMenuItem commands to RunCommand when converting applications.
acCmdSaveRecord is what's called an intrinsic constant, and is used to represent a value needed for a parameter. Access uses intrinsic constants quite a bit. This saves you development time; you don't have to remember numeric numbers because you have the intrinsic constants listed.
Another is the OpenForm method:
DoCmd.OpenForm "SplashScreen", acNormal, , , acFormEdit, acWindowNormal
Table 1.1 lists the macro actions that are used as methods off the DoCmd object.
Note
In addition to the DoCmd object, many macro actions have alternative methods and statements available. In most cases, these commands are more efficient to use than the DoCmd method. This generally isn't the case when DAO or ADO alternatives are used.
Instead of being simply a straight port over from macro action to VBA command, some of the commands are much more flexible in the action they perform. It's generally better to use a code command over a macro whenever possible. The macro actions that have code equivalents are as follows:
MsgBox In place of the MsgBox macro action, you use the MsgBox() function either to retrieve an answer from the user or simply to display a message box.
Tip
One enhancement to the MsgBox() function is that the function can include other buttons in the message box, such as Yes, No, and Cancel. The other enhancement is that the function returns what the user pressed or clicked. For information about the MsgBox() function, look up MsgBox Function in the Answer Wizard.
RunApp The Shell()function is used to run another application.
RunCode This action allows you to use the string name of a function or subprocedure to run it.
StopAllMacros This statement isn't necessary because macros aren't used. When a macro is converted to VBA, the End statement is used.
StopMacro This statement isn't necessary because macros aren't used. When a macro is converted to VBA, the Exit Sub|Function statement is used.
13.59.130.130