Example Apps for Businesses, Schools & Developers

Version 1.1                 Approx 1 MB (zipped)                       First Published 25 Jan 2025


My thanks to fellow Access MVP, Richard Rost, for his recent ActiveForm video which was the prompt for this article.

It is often important to know which form opened the currently active form so you can return to the calling form easily.
This is particularly important when you have multiple ways of opening a form from different places.

Using OpenArgs

Tracking the calling form is usually done using the OpenArgs property. It works well and requires almost no code.

The syntax to open a form is: DoCmd.OpenForm (FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs)

For example, you can track opening Form3 from Form1, by using this code e.g. on a button click in Form1: DoCmd.OpenForm "Form3",   ,   ,   ,   ,   ,"Form1"

This can also be written explicitly as: DoCmd.OpenForm "Form3", OpenArgs:="Form1"

Similar code can be used to open Form3 from Form2: DoCmd.OpenForm "Form3",   ,   ,   ,   ,   ,"Form2"

When Form3 opens, it stores that OpenArgs property allowing you to easily return to the calling form using code such as: DoCmd.OpenForm Me.OpenArgs
The approach works well and I use it in many of my Access apps.



An Alternative Approach using Screen.ActiveForm

An alternative method is to use the Screen.ActiveForm property and it needs even less code!
If you write the line Debug.Print Screen.ActiveForm in an event such as Form_Current, the name of the currently active form will be printed to the VBE Immediate window.

NOTE:
This doesn't work for a popup form as that is placed 'on top of' the main Access screen. As a result, popup forms are never treated as the currently active form by Access.
Using that code in the Form_Current event of a popup form, will instead return the name of the last opened standard form.

Using that code in the Immediate window when just a popup form is open will trigger error 2475:

Error 2475
Unfortunately, Access doesn't have a Screen.PreviousForm property to keep track of the previous (or calling) form

However, if we use Screen.ActiveForm in the Form_Load event, it will instead return the name of the previous (calling) form.
This happens as the new form isn't fully loaded and is therefore not yet active!

This simplifies the code required to track the calling form and then return to it

So in Form1, we just need to use DoCmd.OpenForm "Form3" (without the OpenArgs).

Then in the Form_Load event of the opened form, Form3, we write code such as:

Private Sub Form_Load()

      Dim strName As String
      'get the name of the previous (calling) form
      strName = Screen.ActiveForm

End Sub


NOTE:
The above code also works even in the case where the previous form was a popup! You can then return to the calling form from any other event using DoCmd.OpenForm strName

If you want a visual clue, you can write the name of the calling form e.g. in the form caption

Private Sub Form_Load()

      Dim strName As String
      'get the name of the previous (calling) form
      strName = Screen.ActiveForm

      'get the name of the previous (calling) form
      Me.Caption = Me.Name & " called from " & strName & "

End Sub


For example, if Form3 is opened from Form1, the Form3 caption will read: Form3 called from Form1

Form3 - opened from Form1
Similar code throughout the app will allow you to easily track calling forms in your app and revert to the previous form.
Generic code can be used throughout the app without needing to specify the calling form name in each case.



Download

Click to download an example app:  Active Form Tests      2 x ACCDB files     Approx 1 MB (zipped)

As with all files downloaded from the internet, you will need to first Unblock downloaded files by removing the Mark of the Web

Then unzip the file and either make it a trusted document or (preferably) save it in a trusted location

NOTE:
Both example apps contain identical code including code to check when ech form is activated / deactivated.
The code only runs in the non popup version. If you find it annoying, it can be commented out or deleted without affecting form functionality.

Form1 Activated Form2 Deactivated


The Form_Activate and Form_Deactivate event code NEVER runs in a popup form as it can never become active.



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 25 Jan 2025



Return to Example Databases Page




Return to Top