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



The information in this article applies to:

  • Microsoft Access 1.0
  • Microsoft Access 1.1
  • Microsoft Access 2.0
  • Microsoft Access for Windows 95 7.0
  • Microsoft Access 97
  • Microsoft Visual Basic Professional Edition for Windows 3.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0

This article was previously published under Q97776
Moderate: Requires basic macro, coding, and interoperability skills.

SUMMARY

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:

96845 How to Use DDE to Display Microsoft Access Data in VB 3.0


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbinterop kbProgramming KB97776