Issue with MS Access 365 not releasing Forms from Forms collection

Anonymous
2025-04-02T12:15:56+00:00

I'm running MS Access Version 2502 (Build 18526.20168 Click-to-Run), Current Channel and I've noticed that in certain scenario Forms are not being removed from Forms collection.
If I create a new database with a single form called 'Form1' and add 2 public methods to it:

Public Sub Show()

Me.Visible = True

End Sub

Public Sub Hide()

Me.Visible = False

End Sub

then I add a Standard Module with the following code:

Public frm As Form_Form1


Public Sub Test()

Set frm = New Form_Form1

'frm.Show

Debug.Print Forms.Count

'frm.Hide

Set frm = Nothing

Debug.Print Forms.Count

End Sub

and slowly step through the code, first Debug.Print will show the count of 1 and the second goes back to 0 as expected.
But if I uncomment Show & Hide methods and step through the code again, the counter never clears.

Has that been already acknowledged, has anybody else experienced this issue?

Thanks

Microsoft 365 and Office | Access | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Newest
  1. Anonymous
    2025-04-11T12:51:41+00:00

    It's not so much hiding the Form but clearing the reference (set frm = nothing) that is the key here. In the latest code snippet, I've removed the hiding part.
    In my tiny brain this is how it should work:
    If we're dealing with the default form instance, we need to start it up with DoCmd.OpenForm and because that is a Sub, it doesn't return form object reference back and it need to be closed with DoCmd.Close.
    If we're dealing with creating instances of the Form, we need to assign it to the variable, that increases a counter for that object in VBA/COM layer but Access must be tapping into that to pass that reference to it's internal Forms collection. When that variable gets cleared (or goes out of scope) VBA/COM layer reference counter gets decreased, Access should clear that reference in the Forms collection and object gets released.
    So if you step through the code when we set frm = nothing (and not hiding form first), form should disappear from screen because in debug mode Access internal processes can still run. At runtime can add DoEvents to lett Access do it's thing.
    In our weird environments, Forms collection reference to that Form doesn't get cleared though, leading to Forms occupying memory and stacking up in forms collection.

    Some very basic info regarding this in the following article (can't find anything more in-depth):
    Create multiple instances of a form | Microsoft Learn

    Initially when this was reported to us, we couldn't replicate it. We suspected that this will be related to Click-To-Run but at the time my machine was still on Win11 22H2. I think that after upgrading to 23H2 I've started seeing the issue but probably Office was upgraded at the same time.
    We were hoping that this would be easier to reproduce by the community but sadly it is not the case.
    We may need to create clean VM with just Office installation to see if we can reproduce it.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2025-04-11T10:44:50+00:00

    I’m using Windows 10 and a slow PC, both of which may be relevant.

    In my tests, using DoEvents was enough to clear the form from the collection after hiding it, without explicitly closing it.

    To be honest, I was surprised that worked as I wouldn’t have expected just hiding the form to clear it from the Forms collection.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2025-04-11T08:35:27+00:00

    Thank you for you reply,

    Unfortunately this is not a fix in some environments where we're experiencing the problem.
    When everything runs as expected, DoEvents should release some Access internal process that would clean all non-default instances of the Form, that have no references held in code, from Forms collection but in some of our environments, and also reported by some of our customers, this is not happening.

    In my tests, it doesn't really matter if the form's variable is procedure or module level scope, it makes no difference if DoEvents is added, variable is set to Nothing or goes out of scope. Forms collection simply doesn't get cleared unless DoCmd.Close is invoked on that Form.

    I've also tested this on Access 2016 msi/Windows 10 and that behaves as expected so at this point we think it's just Click-to-Run in some environments.

    So with the following, slightly modified code, with form variable being procedure level scope, modifying caption to make form unique and with outputting forms from collection along with that caption:

    Public Sub ReferenceTest()

    Static swCounter As Integer 
    
    Set frm = New Form\_Form1 
    
    swCounter = swCounter + 1   
    
    frm.Visible = True
    
    frm.Caption = "Caption " & swCounter 
    
    Debug.Print "Count before var release: " & Forms.Count 
    
    Set frm = Nothing 
    
    DoEvents 
    
    Debug.Print "Count after var release: " & Forms.Count 
    
    OutputForms 
    

    End Sub

    Private Sub OutputForms()

    Dim frm As Form 
    
    Dim sFormsList As String 
    
    For Each frm In Forms 
    
        sFormsList = sFormsList & IIf(sFormsList = vbNullString, frm.Name, ", " & frm.Name) & " (" & frm.Caption & ")" 
    
    Next frm 
    
    Debug.Print sFormsList 
    

    End Sub

    If I run the Test method few times, in a normally working environment I would get the following output:

    but what we see in some other environments is this:

    Just trying to figure out where that inconsistent behaviour comes from but I understand that you can't replicate it on your end?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2025-04-09T13:38:05+00:00

    Whilst I agree with Duane's comments, the reason for your code failing is very simple. You just need to give it time to finish processing before the second Debug.Print

    Add the DoEvents line as below and it works correctly with the two lines uncommented. For me, it then works correctly both when stepping through and when run normally

    Public Sub Test()

         Set frm = New Form_Form1

        frm.Show '- uncomment for second test

        Debug.Print Forms.Count

        frm.Hide  '- uncomment for second test

         Set frm = Nothing

         DoEvents

         Debug.Print Forms.Count

    End Sub

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2025-04-02T14:04:17+00:00

    Thanks for the reply,
    Show & Hide are custom methods on the Form that are simply changing the Visible property of that Form. I have put code for both at the top of my post.
    Even if I modify the code to call Visible property from outside of the Form e.g.

    • instead of frm.Show, use frm.Visible = True
    • and instead of frm.Hide use frm.Visible = False
      form is not cleared from the Forms collection.

    Was this answer helpful?

    0 comments No comments