The following example runs a user-defined Sub procedure in a module in a Microsoft Access database from another application that acts as an Active X component.
To try this example, create a new database called WizCode.mdb and set its ProjectName property to WizCode. Open a new module in that database and enter the following code. Save the module, and close the database.
Sub Greeting(strName As String)
MsgBox("Hello, " & strName)
End Sub
Once you have completed this step, run the following code from Microsoft Excel or Microsoft Visual Basic. Make sure that you have added a reference to the Microsoft Access type library by clicking References on the Tools menu and selecting Microsoft Access 9.0 Object Library in the References dialog box.
' Include in Declarations section of module.
Dim appAccess As Access.Application
Sub RunAccessSub()
' Create instance of Access Application object.
Set appAccess = _
CreateObject("Access.Application.9")
' Open WizCode database in Microsoft Access window.
appAccess.OpenCurrentDatabase "C:\My Documents\WizCode.mdb", False
' Run Sub procedure.
appAccess.Run "Greeting", "Joe"
Set appAccess = Nothing
End Sub