다음을 통해 공유


Application.Run Method (Access)

You can 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, Microsoft Access looks for the procedure first in the library database, 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 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 by 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 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

See Also

Concepts

Application Object

Application Object Members