I don’t believe Access forms support Show or Hide like forms in Excel. Why are you attempting to use this rather than simply setting the Visible property?
Issue with MS Access 365 not releasing Forms from Forms collection
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.
6 answers
Sort by: Most helpful
-
-
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.
-
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
-
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 OutputFormsEnd 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 sFormsListEnd 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?
-
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.