Run Method

Microsoft Access Visual Basic

Run Method

       

You can use the Run method to carry out a specified Microsoft Access or user-defined Function or Sub procedure. Variant.

expression.Run(Procedure, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30)

expression   Required. An expression that returns one of the objects in the Applies To list.

Procedure  Required String. The name of the Function or Sub procedure to be run. If you are calling a procedure in another database use the project name and the procedure name separated by a dot in the form: "projectname.procedurename". If you execute Visual Basic code containing the Run method in a library database, Microsoft Access looks for the procedure first in the library database, then in the current database.

Arg1  Optional Variant.

Arg2  Optional Variant.

Arg3  Optional Variant.

Arg4  Optional Variant.

Arg5  Optional Variant.

Arg6  Optional Variant.

Arg7  Optional Variant.

Arg8  Optional Variant.

Arg9  Optional Variant.

Arg10  Optional Variant.

Arg11  Optional Variant.

Arg12  Optional Variant.

Arg13  Optional Variant.

Arg14  Optional Variant.

Arg15  Optional Variant.

Arg16  Optional Variant.

Arg17  Optional Variant.

Arg18  Optional Variant.

Arg19  Optional Variant.

Arg20  Optional Variant.

Arg21  Optional Variant.

Arg22  Optional Variant.

Arg23  Optional Variant.

Arg24  Optional Variant.

Arg25  Optional Variant.

Arg26  Optional Variant.

Arg27  Optional Variant.

Arg28  Optional Variant.

Arg29  Optional Variant.

Arg30  Optional Variant.

Remarks

This method is useful when you are controlling Microsoft Access from another application through Automation, formerly called OLE Automation. For example, you can use the Run method from an ActiveX component to carry out a Sub procedure that is defined within a Microsoft Access database.

You can set a reference to the Microsoft Access type library from any other ActiveX component 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.

For example, suppose you have defined a procedure named NewForm in a database with its ProjectName property set to "WizCode." The NewForm procedure takes a string argument. You can call NewForm in the following manner from Visual Basic:

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

If another procedure with the same name may reside in a different database, qualify the procedure argument, as shown in the preceding example, 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.

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 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.

Note You set the ProjectName be selecting Tools, WizCode Properties... from the VBE main menu.

Public Sub Greeting(ByVal strName As String)
    MsgBox ("Hello, " & strName & "!"), vbInformation, "Greetings"
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 10.0 Object Library in the References dialog box.

Private Sub RunAccessSub()
    
    Dim appAccess As Access.Application

    ' Create instance of Access Application object.
    Set appAccess = CreateObject("Access.Application")
    
    ' 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