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?