LOOKING AT MACRO-TO-CODE CHANGES

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.

Using the DoCmd Object

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.

Table 1.1. DoCmd Object Method Alternatives to Macro Actions
Method Visual Basic Alternative
AddMenu  
ApplyFilter  
Beep Beep statement
CancelEvent Use the Cancel argument in event procedures
Close  
CopyObject  
DeleteObject Use the Delete method off the object collection
Echo Application.Echo
FindNext  
FindRecord  
GoToControl ControlName.SetFocus
GoToPage Form.GoToPage
GoToRecord  
Hourglass  
Maximize  
Minimize  
MoveSize  
OpenDataAccessPage  
OpenDiagram  
OpenForm  
OpenModule  
OpenQuery DatabaseVariable.Execute for action queries
OpenReport  
OpenStoredProcedure  
OpenTable  
OpenView  
OutputTo  
PrintOut  
Quit Application.Quit
Rename Rename the object by using the DAO object.Name property
RepaintObject  
Requery ObjectName.Requery
Restore  
RunCommand  
RunMacro  
RunSQL DatabaseVariable.Execute "SqlString"
Save  
SelectObject  
SendObject MAPI commands (SendObject is more efficient)
SetMenuItem  
SetWarnings  
ShowAllRecords  
ShowToolbar  
TransferDatabase  
TransferSpreadsheet  
TransferText  

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.


Code Equivalents of Macro Commands

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.

  • SendKeys You can use a SendKeys statement in code.

  • SetValue You can set the value directly in Visual Basic.

  • 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.130.130