OpenQuery Method

Microsoft Access Visual Basic

OpenQuery Method

       

The OpenQuery method carries out the OpenQuery action in Visual Basic.

expression.OpenQuery(QueryName, View, DataMode)

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

QueryName  Required Variant. A string expression that's the valid name of a query in the current database. If you execute Visual Basic code containing the OpenQuery method in a library database, Microsoft Access looks for the query with this name first in the library database, then in the current database.

View  Optional AcView.

AcView can be one of these AcView constants.
acViewDesign
acViewNormal default
acViewPivotChart
acViewPivotTable
acViewPreview

If the queryname argument is the name of a select, crosstab, union, or pass-through query whose ReturnsRecords property is set to –1, acViewNormal displays the query's result set. If the queryname argument refers to an action, data-definition, or pass-through query whose ReturnsRecords property is set to 0, acViewNormal runs the query.

If you leave this argument blank, the default constant (acViewNormal) is assumed.

DataMode  Optional AcOpenDataMode.

AcOpenDataMode can be one of these AcOpenDataMode constants.
acAdd
acEdit default
acReadOnly

If you leave this argument blank, the default constant (acEdit) is assumed.

Remarks

For more information on how the action and its arguments work, see the action topic.

Note   This method is only available in the Microsoft Access database environment (.mdb). See the OpenView or OpenStoredProcedure methods if using the Microsoft Access Project environment (.adp).

If you specify the datamode argument and leave the view argument blank, you must include the view argument's comma. If you leave a trailing argument blank, don't use a comma following the last argument you specify.

Example

The following example opens Sales Totals Query in Datasheet view and enables the user to view but not to edit or add records:

DoCmd.OpenQuery "Sales Totals Query", , acReadOnly