Close All forms when new one opens

Avick

Registered User.
Local time
Today, 13:19
Joined
Mar 11, 2000
Messages
49
Hi
I am trying to get access to close all open forms and open the selected form when a button is pressed.

I tried this in a macro like so:

open switchboard
open new.client
close (forms)

The switchboard is used as a back drop to the database.
the new client form will open over this (pop up is set to YES)
Any other forms will be closed at this point.

I can only get it to close one form at a time which means I have lots of close commands in each macro.

The problem is, if I add a new form then I have to go through each macro to reset it to take that form into consideration.

There must be an easy way to do this.

Alan
 
Hello:
The below code will do what you ask. Place it under a command button called "cmdCloseOpen"

Private Sub cmdCloseOpen_Click()
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
'Search for open AccessObject objects in AllForms collection.
For Each obj In dbs.AllForms
If obj.IsLoaded = True Then
'Close the form if it is open
DoCmd.Close acForm, obj.Name, acSaveYes
End If
Next obj
'--------------------
'Open new form here
DoCmd.OpenForm "Your Form Name here"
End Sub
 
Hi mhartman

I tried the code and i cant get it to work.

here is the code in the new module I created.

Private Sub cmdCloseOpen_Click()
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
'Search for open AccessObject objects in AllForms collection.
For Each obj In dbs.AllForms
If obj.IsLoaded = True Then
'Close the form if it is open
DoCmd.Close acForm, obj.Name, acSaveYes
End If
Next obj
'--------------------
'Open new form here
DoCmd.OpenForm "frm_switchboard"
DoCmd.OpenForm "frm_new_reps"
End Sub

I then created a link to it and named it cmdCloseOpen as you said.
When I click the link it brings me back to the module window and the code is highlighted on the first form (frm_switchboard)
If I remove this form and try the link it brings me back to the second form (frm_new_reps) I have check and double checked the form names and they are correct.

Could it be something to do with the fact that there is an underscore in the names of the forms???
 
Which form does the code run from? I would think that you should run it from your switchboard. It would then close the switchboard and all other open forms then open your "frm_new_reps".

Before posting this reply, I tested mhartman's code and it worked fine for me.
 
I have just re-tested the code and it worked when I put it in the switchboard code and not in a module on its own.

I have even got it working on a simple text link which was not working at the begining.

Thank you very much for your code and your support.

I now have a list of codes that opperate all the form links in the database and it really works a treat.

There is just one thing. Can I open a form in new record view or read only view useing this code. ????

Thanks again for your help
 
Just a small change required for read-only...

Code:
DoCmd.OpenForm "The Form Name", , , , acFormReadOnly

Regards,
Tim
 
That worked a treat on most of the forms. Thanks.

The only form I can't get it to work on is one that has to open in DS view.

If I set this form like so:
DoCmd.OpenForm "frm_reps", acFormDS, , , acFormReadOnly
It opens in standard form view. This is no good as the form opens a second form that changes as you click a name (record) in the first form.
For example: If you click a name in the first form (Data sheet view) the second form automaticly changes to the data for that name.

The only way I can get it to display in DS view is if I set it like so:
DoCmd.OpenForm "frm_reps", acDesign, , , acFormReadOnly

If I do this it opens in Datasheet view but it is not ReadOnly and users can add or change the names in the first form. My aim is to make the first form just a clickable form and nothing else.

Alan
 

Users who are viewing this thread

Back
Top Bottom