unload all forms

  • Thread starter Thread starter Newbie
  • Start date Start date
N

Newbie

Hi,
I have a procedure that is supposed to close all open forms and then open
the mainform
If I step through procedure it works ok but if I let it run the whole
database gets closed

Why is this and what do I need to do to fix it?

Here is my procedure that is called from the Click event on a form

************code starts*****************
Public Function unloaded()

Dim fNum As Integer
If Forms.Count = 0 Then
Exit Function
End If
For fNum = Forms.Count - 1 To 0 Step -1
DoCmd.Close
Next fNum
DoEvents
DoCmd.OpenForm "frmMainMenu", acNormal, , , acFormEdit, acWindowNormal
DoEvents

End Function

*************code ends***************

Here is the OnClick event code that calls the above:

************code starts***************
Private Sub btnClose_Click()
Call modDeclarations.unloaded
DoCmd.Close acForm, "frmError", acSaveYes
End Sub
*********code ends*****************
 
Thanks for that but no I wasn't closing the db from the frmMainMenu

I have changed the code to the following and this seems to work - for some
reason it is the DoEvents after the Close from line that made it work
otherwise the db closed all by itself

Any idea why that would be?

Public Function unloaded()

Dim fNum As Integer
If Forms.Count = 0 Then
Exit Function
End If
For fNum = Forms.Count - 1 To 0 Step -1
If Forms(fNum).Caption <> "MIS" Then
DoCmd.Close acForm, Forms(fNum).FormName
DoEvents
End If
Next fNum
DoEvents
End Function
 
I started writing this before lunch so it seems like you
already answered your own question, but here it is anyway.

Docmd.close closes the current object. Your problem seems
to be a matter of the order in which the database is
finding & closing objects. If it finds the database
window prior to any other open forms, it will close it
(and the entire database) even though the database window
is not counted as a form in your form count.

When you ran it step-by-step it is probably just a
coincidence (or the order in which you opened forms) that
it is finding all open forms prior to hitting the
database window.

After looking at your second set of coding, it appears
that your database name is "MIS", if it is; you are now
testing to make sure you're not closing the database form
which is why it is no longer happening. You should not
need the "Do Event" lines at all to accomplish closing
the forms. But if it works now, there's no reason to go
taking that line of code out.
 
Back
Top