Application.Run method (Access)

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

Syntax

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 A variable that represents an Application object.

Parameters

Name Required/Optional Data type Description
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, Access looks for the procedure first in the library database, and then in the current database.
Arg1, Arg2, ...Arg30 Optional Variant The arguments that should be passed to the Function or Sub specified in the Procedure argument.

Return value

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 an Access database.

You can set a reference to the 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 Access database from any application other than 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 Access database from another database.

Example

The following example runs a user-defined Sub procedure in a module in an 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 by selecting Tools > WizCode Properties from the VBE main menu.

Public Sub Greeting(ByVal strName As String) 
 MsgBox ("Hello, " & strName & "!"), vbInformation, "Greetings" 
End Sub

After you have completed this step, run the following code from Microsoft Excel or Visual Basic. Make sure that you have added a reference to the Access type library by choosing References on the Tools menu and choosing Microsoft Access 12.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

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.