In this tutorial, we use Ms Access VBA Close Form and Workbook topic. VBA code is the feature in Ms Access for programming to adding more function to Access database, using Visual Basic.To open the VBA window, press the Alt + F11 buttons in your keyboard. In Project toolbar, double click the desired form. The VBA window for that form will appeared. The Close Form is the function to close the current active form when a specific action occurred (E.g. Click exit button, image or anything else). Sometimes, it’s combined with other f unction, such as open or load new form. Even through the feature can be achieved with command button wizard, sometimes you may need to use it in VBA for more complex or specified result.
Here is the most popular syntax for Close form:
DoCmd.Close (ObjectType, ObjectName, acSavePrompt)
ObjectType is the type of the object that wanted to be closed. In this case, the form type. Mostly, it started with “Ac”. Example: AcForm because the object type is form. ObjectName is the name of the object that wanted to be closed. This is string argument, so you have to use quotation marks. E.g. “Order Form”.
acSavePrompt is the argument to specify whether to save the changes of the object (form) or not. The acSavePrompt can be replaced with other save syntax.
Note: All of these 3 parameters are optional. Depends on the situation, it is possible to only using DoCmd.Close without all the parameters. However, be careful because in some case it may cause an error because there is no detailed specification or sometimes it may exit the database without saving the changes.
Example:
DoCmd.Close acForm, “Order Form”, acSaveYes
By looking the example above, if the button in Order Form is hit, it will close the form with saving the record.
In the picture below is the example to save record before closing form.
The close syntax in the picture is combined with error handling for the saving prompt. If the required field value is null, it will cause run-time error and display message to fill the fields.
After you write the code, don’t forget to save and test it first. Lastly, good luck with the programming!