ACC: Running a MS Access Macro from Visual Basic Using DDE

ID: Q97776

The information in this article applies to:

SUMMARY

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

The following example uses Microsoft Visual Basic 3.0 to run macros in Microsoft Access using dynamic data exchange.

MORE INFORMATION

The following code demonstrates how to run a macro called "MyMacro" in Microsoft Access from a Visual Basic application.

   Sub Command1_Click()
      ' The character between MSACCESS and System is the pipe symbol.
      Text1.LinkTopic = "MSACCESS|System"

      ' Establish a manual DDE link to Microsoft Access.
      Text1.LinkMode = 2

      ' Run the macro called "MyMacro"
      Text1.LinkExecute "MyMacro"

      ' Terminate the DDE link to Microsoft Access.
      Text1.LinkMode = 0
   End Sub

To use this code, create a new Visual Basic program with a text box and a command button with the following properties:

   Text box
   --------
   Name: Text1

   Command Button
   --------------
   Name: Command1

Double-click the command button and enter the code example listed above between the "Sub Command1_Click ()" and "End Sub" lines.

When you run this example, be sure that Microsoft Access is running and that a database containing a macro called "MyMacro" is open. For testing purposes, you may want to create a macro called "MyMacro" that contains a single MsgBox action.

NOTE: While the above example uses the Microsoft Access system Topic listed in the LinkTopic line, Microsoft Access is capable of running a macro with virtually every valid topic it supports. For a list of topics that Microsoft Access supports, please see the following article here in the Microsoft Knowledge Base:

  ARTICLE-ID: Q96845
  TITLE:      How to Use DDE to Display Microsoft Access Data in VB 3.0

Additional query words:
Keywords          : kbinterop
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 20, 1998