Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Closing Forms

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:

  1. Select the Command Button you’ve added to your form.

  2. In the Property Sheet, go to the Other tab and locate the Hyperlink SubAddress property.

  3. Click the build (…) button next to this property to open the Edit Hyperlink dialog box.

  4. In the dialog box, under Link to, select Object in this Database.

  5. A list of database objects (Tables, Queries, Forms, Macros, etc.) will appear.

  6. Expand the Macros group and locate the Hlink_Close macro.

  7. 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:

vba
Private Sub cmdClose_Click() DoCmd.Close End Sub

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

Share:

1 comment:

  1. [...] 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

Comments subject to moderation before publishing.

PRESENTATION: ACCESS USER GROUPS (EUROPE)

Translate

PageRank

Post Feed


Search

Popular Posts

Blog Archive

Powered by Blogger.

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Objects Menus and Toolbars Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Combo Boxes Dictionary Object ListView Control Query VBA msaccessQuery Calculation Event Graph Charts ImageList Control List Boxes TreeView Control Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email progressmeter Access2007 Copy Excel Export Expression Fields Join Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility WScript Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Desktop Shortcuts Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Message Box Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code