Run Microsoft Access Queries in VBA Without Datasheet View
Question details
Run a Microsoft Access query from VBA without displaying its results in Datasheet view.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A VBA procedure needs to process a saved query in the background.
- Observed behavior
- DoCmd.OpenQuery opens a SELECT query in Datasheet view, while action queries can be executed directly.
Identify whether the saved query is an action query, such as UPDATE or DELETE, or a SELECT query. Confirm its name in the Navigation Pane and check whether it requires parameters.
Execute an action query without opening Datasheet view
Use CurrentDb.Execute for saved queries that add, update, delete, or make tables.
In Access, open the database and choose Database Tools > Visual Basic. Open the module containing your procedure.
Add `CurrentDb.Execute "queryname", dbFailOnError` to the procedure, replacing `queryname` with the saved action query's name.
Run the procedure from the VBA editor or its assigned button. Handle any error raised by the Execute statement in your VBA code.

Read a SELECT query through a DAO recordset
Use a recordset when VBA needs to process SELECT query results without displaying a datasheet.
Work with Office documents in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with documents, spreadsheets, and presentations. Its Microsoft Office format support helps you move existing files into your workflow. It does not run Microsoft Access queries or replace Access VBA.

Frequently Asked Questions
Why can't CurrentDb.Execute run my SELECT query?
CurrentDb.Execute is intended for action queries, not queries that return a result set. Use CurrentDb.OpenRecordset to read a SELECT query's results in VBA.
How can I view a SELECT query's results instead?
Use `DoCmd.OpenQuery "queryname"` in VBA, replacing the name with your saved query. This opens the query for viewing in Datasheet view.
What does dbFailOnError do for an action query?
When supplied to CurrentDb.Execute, dbFailOnError raises an error if affected records cannot be updated. Add VBA error handling to respond to that error.
How do I run a SELECT query that requires parameters?
Create or reference a DAO QueryDef for the saved query, assign values to its Parameters collection, and then call its OpenRecordset method.




