ACC: Converting Macros to MS Visual Basic for Applications

Last reviewed: August 29, 1997
Article ID: Q136058
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

With macros, you can accomplish many tasks, such as opening and closing forms, showing and hiding toolbars, and running reports. However, if you want more control over your application's behavior, you can convert your macros to procedures using Visual Basic for Applications. To help you convert existing macros to Visual Basic code, you can use the Microsoft Access for Windows 95 version 7.0 Macro Conversion Wizard. This article explains how to use the Macro Conversion Wizard, and how it converts your macro actions.

MORE INFORMATION

To convert a macro to Visual Basic code, follow these steps:

  1. Open the sample database Northwind.mdb.

  2. In the Database window, click the Macros tab, and select the Customer Labels Dialog macro.

  3. On the File menu, click Save As/Export.

  4. In the SaveAs box, click Save as Visual Basic Module, and then click OK.

  5. In the Convert macro box, select the "Add error handling/trapping to generated functions" option and the "Include macro comments" option.

  6. Click Convert.

The Macro Conversion Wizard performs the following tasks when it converts a macro to Visual Basic code:
  • It creates a new module of procedures using the same names from the Macro Names column in the original macro.
  • It converts macro actions to appropriate DoCmd methods in Visual Basic. For the several macro actions that do not have a DoCmd equivalent, the Macro Conversion Wizard does the following:

        No DoCmd Equivalent   Solution
        -----------------------------------------------------------------------
    
        AddMenu               The wizard comments out these lines in the code
                              and notifies the user.
    
        MsgBox                Uses the MsgBox function.
    
        RunApp                Uses the Shell function to run another
                              application.
    
        RunCode               Uses the Call statement and runs the function
                              directly in Visual Basic.
    
        SendKeys              Uses the SendKeys statement.
    
        SetValue              Sets the value directly in Visual Basic.
    
        StopAllMacros         Uses the End statement.
    
        StopMacro             Uses the Exit Function statement.
    
    
  • Expressions in the macro's Condition column are converted into If...Then statements.
  • If the Condition column contains a full reference to a control in a form or report (for example, Forms!MyForm![LastName]), Microsoft Access uses If...Then statements. If the reference refers to a control name only (for example, [LastName]), then Microsoft Access uses a With...End With or a CodeContextObject statement.

REFERENCES

For more information about converting macros to Visual Basic for Applications code, search in the Help Index for "converting macros," or ask the Microsoft Access 97 Office Assistant.

Keywords          : kbprg PgmHowTo
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbinfo


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.