Run Method

Description

You can use the Run method to carry out a specified Microsoft Access or user-defined Function or Sub procedure. This method is useful when you are controlling Microsoft Access from another application through OLE Automation. For example, you can use the Run method from an OLE Automation controller to carry out a Sub procedure that is defined within a Microsoft Access database.

Syntax

application.Run procedure[, arg1, arg2, ..., arg30]

The Run method uses the following arguments.

Argument

Description

application

The Application object.

procedure

The name of the Function or Sub procedure to be run.

arg1, arg2, ...

Optional. The arguments for the specified Function or Sub procedure. You can supply a maximum of thirty arguments.


Remarks

You can set a reference to the Microsoft Access type library from any other OLE Automation controller and use the objects, methods, and properties defined in that library in your code. However, you can’t set a reference to an individual Microsoft Access database from any application other than Microsoft Access. The Run method provides a way to call a procedure you have defined in a Microsoft Access database from another application.

For example, suppose you have defined a procedure NewForm in a database named WizCode.mdb. The NewForm procedure takes a string argument. You can call NewForm in the following manner from Visual Basic 4.0.


Dim appAccess As New Access.Application.OpenCurrentDatabase ("C:\My Documents\WizCode.mdb").Run "WizCode.NewForm", "Some String"

If another procedure with the same name may reside in a different database, qualify procedure with the name of the database in which the desired procedure resides.

You can also use the Run method to call a procedure in a referenced Microsoft Access database from another database. Although you can call a procedure in a referenced database as though it were in the current database, doing so loads the potential call tree of the module in the referenced database. For performance and resource reasons, you may want to use the Run method, which loads only the module in which the procedure resides.

Microsoft Access ignores any value returned by a procedure called by the Run method.

Example

The following example runs a user-defined Sub procedure in a module in a Microsoft Access database from another application that acts as an OLE Automation controller.

To try this example, create a new database called WizCode.mdb and open a new module in that database. Enter the following code, save the module, and close the database.


Sub Greeting(strName As String)
    MsgBox("Hello, " & strName)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 For Windows 95 in the References dialog box.


' Include in Declarations section of module.
' From Microsoft Excel, use "Dim appAccess As Object".appAccess As Access.Application
RunAccessSub()
    ' Create instance of Microsoft Access Application object.
    Set appAccess = CreateObject("Access.Application.7")
    ' Open WizCode database in Microsoft Access window.
    appAccess.OpenCurrentDatabase ("C:\My Documents\WizCode.mdb")
    ' Run Sub procedure.
    appAccess.Run "Greeting", "Joe"Sub