How can I unload all used forms in Access using VBA?

L

Laphan

Hi All

I'm struggling with how I can unload all used forms when the user closes my
Access 'app'.

Basically I have around 9 forms in my Access DB and I want to unload all of
the used forms when the user clicks any of the 'X's on the visible forms.
In essence if they click the 'X' on any form then the app has ended and I
need to close down all forms.

In each Form_Close event I have a call to a public sub that does the
following:

Dim intChoice As Integer

intChoice = MsgBox("Are you sure you wish to quit the WebLog program?",
vbOKCancel)

If intChoice = 1 Then
Dim f As Form
' close db objects
Call CloseConnections
' unload all forms
For Each f In Forms
DoCmd.Close acForm, f, acSaveNo <<< its this part that I'm stuck on
<< I was using unload f, but that it didn't work either its as if I'm
using a completely different syntax
<< to what Access is expecting.
Next
End If

I won't know which forms the user will have opened on their travels so I
need to go through the active forms/collection.

Any ideas?

Thanks
 
D

Dennis

Dim i As Integer

For i = 1 To CurrentProject.AllForms.Count
If CurrentProject.AllForms(i - 1).IsLoaded Then
DoCmd.Close acForm, CurrentProject.AllForms(i - 1).Name, acSaveNo
End If
Next i
 
L

Larry Daugherty

Why do that? It would surely be unexpected behavior and therefore
unfriendly to the user.

If you don't want your users closing the forms using the "xx" you can
disable it in the form's properties. But then be sure to give them a
command button to close the form or they'll simply crash the system to
get out of your application.

Since a well designed application requires the use of Forms for the
user to do anything at all, just put the line
Application.Quit
into the Close event of each form. That will quit the entire
application which closes all forms for you. :)

HTH
 
L

Laphan

Hi Larry

Thanks for this, but I was told that the quit/end command was like the brute
force approach and I should always gracefully unload all of the forms
myself.

Is this not the case?

Rgds

Why do that? It would surely be unexpected behavior and therefore
unfriendly to the user.

If you don't want your users closing the forms using the "xx" you can
disable it in the form's properties. But then be sure to give them a
command button to close the form or they'll simply crash the system to
get out of your application.

Since a well designed application requires the use of Forms for the
user to do anything at all, just put the line
Application.Quit
into the Close event of each form. That will quit the entire
application which closes all forms for you. :)

HTH
 
L

Laphan

Thanks Dennis

This seems to be spot on!!

Dim i As Integer

For i = 1 To CurrentProject.AllForms.Count
If CurrentProject.AllForms(i - 1).IsLoaded Then
DoCmd.Close acForm, CurrentProject.AllForms(i - 1).Name,
acSaveNo
End If
Next i
 
L

Larry Daugherty

No. Quitting the application closes all forms in the inverse order of
their opening with the same grace as if you'd clicked the X box.

But.... I'm definitely on the side of giving the User all of the
information and predictability that I can. I almost always put large
navigation buttons on forms and always have a "Done" button which will
close the current form and return to the prior form or switchboard..
On switchboards I offer to Return to Higher menu or Quit the
Application.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top