Introduction.
There are several ways to close an active Access form, and with a few simple techniques, you can enhance the appearance and behavior of this action for a better user experience.
The simplest method to close an active Access form is by clicking the standard Close (X) button on the window, which most users are familiar with. This action also triggers the Form's 'On Close' event, allowing the programmer to handle any necessary final tasks, such as closing related forms or saving changes.
However, when multiple forms are open simultaneously, there's a risk that the user might accidentally close the wrong form by clicking the incorrect X button. This can disrupt workflow or cause confusion, especially in multi-form applications.
Command Button with Macro Hyperlink.
Instead of relying on the standard Close (X) button, I prefer to create a dedicated Command Button placed conveniently on the form, usually in the Header or Footer section. This provides a clearer and more user-friendly method to close the form.
To implement this, I create a macro (I usually name it Hlink_Close
) that contains only the Close action. I leave the Object Type and Object Name parameters blank, so it simply closes the active form. Then, I set the Command Button’s Hyperlink SubAddress property to point to this macro.
This approach not only avoids accidental form closure but also provides a more controlled and visually consistent user experience.
To link your Command Button to the Hlink_Close
macro using the Hyperlink SubAddress property, follow these steps:
-
Select the Command Button you’ve added to your form.
-
In the Property Sheet, go to the Other tab and locate the Hyperlink SubAddress property.
-
Click the build (…) button next to this property to open the Edit Hyperlink dialog box.
-
In the dialog box, under Link to, select Object in this Database.
-
A list of database objects (Tables, Queries, Forms, Macros, etc.) will appear.
-
Expand the Macros group and locate the
Hlink_Close
macro. -
Select it, and click OK to apply the link.
Now, when the user clicks the Command Button, the macro will run and close the form.
An additional advantage of using the Hyperlink SubAddress method is its visual feedback.
When the user moves the mouse pointer over the Command Button linked to a macro (like Hlink_Close)
the cursor changes to a hand with the index finger pointing, just like it does when hovering over a hyperlink on a webpage.
This subtle cue improves usability by clearly indicating that the control is clickable, making your form more intuitive and interactive.
Reusing the Hlink_Close
Macro Across Multiple Forms.
The same Hlink_Close
Macro can be reused across any number of forms. You can assign it to Command Buttons, Labels, Images, or any control that supports the Hyperlink SubAddress property.
By linking these controls to the Hlink_Close
Macro users can simply click the object to close the form, without writing additional code for each instance. This method provides a consistent and user-friendly way to close forms across your application.
Important Note on Popup Forms.
'Hlink_Close'
The macro method does not work on forms where the Popup property is set to Yes. In such cases, clicking a control with a 'Hlink_Close'
Hyperlink may instead close another open form with Popup = No, leaving the intended form open.
If the pop-up form is the only form open, Access will throw an error:
“Can’t follow the Hyperlink Macro Hlink_Close.”
Recommended Alternative.
For popup forms, use a Command Button with an On Click event procedure in VBA:
This reliably closes the active pop-up form without relying on the Hlink_Close
macro.
If you need to change only the style of the Mouse Pointer to a hand while pointing the cursor on the control, then insert a # symbol in the Hyperlink SubAddress Property of the Command Button.
Tip: You can Open External Files Like Word, Excel, etc. by setting the Hyperlink Address Property (not Hyperlink SubAddress) of the Command Button with an external File's location address. Select the Link to Option: Existing File or Webpage rather than Object in this Database from the Edit Hyperlink Dialog Control.
Click Next to see Opening Access Forms
[...] Solved: Amended macro action to 'Close' leaving the ObjectType and ObjectName parameters blank and deleted all the sub-macros. changed the form's Popup property from Yes to No Made the Hyperlink SubAddress property link to the new Close macro which now has no sub-macros. Found this article helpful: http://msaccesstips.com/2007/05/closing-forms/ [...]
ReplyDelete