Add a Button to Run an Update Query in Microsoft Access
Question details
Add a command button to an Access form so users can run an update query without opening the database design environment.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Users need to find values and run an update query from a form, optionally entering a replacement value.
- Observed behavior
- The desired result is a form button that runs the query and safely restores Access warnings if an error occurs.
Create and test the update query on a copy of the data, and note its exact name in the Navigation Pane. Make sure the database is trusted so its VBA code can run.
Run the update query from a form button
Use the button’s On Click event to confirm the action, run the saved query, and restore warnings even if the query fails.
Open the form in Design View, choose Button from the Design tab’s Controls group, and place it on the form. If the Command Button Wizard opens, cancel it.
Select the button and open its Property Sheet. On the Event tab, set On Click to [Event Procedure], then select the ellipsis button to open the VBA editor.
Inside the generated Click procedure, add the following code, replacing qryUpdate with the exact name of your saved update query: On Error GoTo ErrHandler If MsgBox("Run this update?", vbYesNo + vbQuestion, "Confirm update") <> vbYes Then Exit Sub DoCmd.SetWarnings False DoCmd.OpenQuery "qryUpdate" CleanUp: DoCmd.SetWarnings True Exit Sub ErrHandler: MsgBox "The update could not be completed: " & Err.Description, vbExclamation Resume CleanUp
Save the form and VBA code, switch to Form View, and select the button. Confirm the action and check that the intended records changed.

Add form controls for search and replacement values
Use form controls when users need to choose which value to find and what to replace it with.
Work with Office Documents and Spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface and Microsoft Office format compatibility for an easier move between applications. It does not run Access forms, update queries, or Access VBA.

Frequently Asked Questions
Where do I put the code for the Access button?
In the form’s Design View, select the button, open the Property Sheet, and choose [Event Procedure] for On Click on the Event tab. Select the ellipsis button to edit the generated VBA procedure.
How can I find records before entering a replacement value?
Enter the search value in a form text box and open a separate select query whose Criteria references that control, such as Forms![frmUpdate]![txtFind]. Review the results, then enter the replacement value and run the update query.
Why should the code restore Access warnings?
DoCmd.SetWarnings False suppresses action-query warnings beyond the immediate query. Use an error handler that returns to cleanup code containing DoCmd.SetWarnings True so warnings are restored if the query fails.
Why does the button do nothing when I click it?
Check that On Click is set to [Event Procedure], the query name in DoCmd.OpenQuery matches the saved query, and the database is trusted so VBA can run.




