Delete open forms

A

alex

Hello,

I'm trying to write code that deletes all open unhidden forms...

I have this code:
Dim db As Access.Application
Dim frm As Form
Set db = Application

For Each frm In db.Forms
If frm.Visible Then
DoCmd.Close acForm, frm.Name
'Debug.Print frm.Name
End If
Next frm

Although the appropriate forms will print in the immediate window; the
code will not delete them all. Strangely, the form that gets opened
second seems to remain; i.e., if three forms are opened, the one that
was opened first and last will close.

Any thoughts? Thanks,
alex
 
D

Dale Fye

What you are trying to do is not "DELETE" open forms but close them. Here is
what I do to close all of the open forms.

Public Sub FormsCloseAll

While Forms.Count > 0
Docmd.Close acForm, Forms(Forms.count - 1)
Wend

End Sub

Actually, I generally do some additional checking to see whether the form is
Dirty, if so, I offer the user the opportunity to save or undo the current
changes. But this is the basic code.
 
J

John Spencer

The problem is that you are changing the number of forms that are open when
you close Form(0) then Form(1) becomes Form(0) and Form(2) becomes Form(1).

Access has handled Form(0) and moves on to Form(1) - which was Form(2)

The easiest way to handle this is to step through the open forms collection in
reverse order. Something like the following untested code snippet:

Dim iLoop as Long

For iLoop = db.Forms.Count-1 to 0 step -1
Set frm = db.Forms(iLoop)
If frm.Visible then
docmd.close acform, frm.Name
end If
End iLoop

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
A

alex

The problem is that you are changing the number of forms that are open when
you close Form(0) then Form(1) becomes Form(0) and Form(2) becomes Form(1).

Access has handled Form(0) and moves on to Form(1) - which was Form(2)

The easiest way to handle this is to step through the open forms collection in
reverse order.  Something like the following untested code snippet:

Dim iLoop as Long

For iLoop = db.Forms.Count-1 to 0 step -1
    Set frm = db.Forms(iLoop)
    If frm.Visible then
      docmd.close acform, frm.Name
    end If
End iLoop

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County









- Show quoted text -

Thanks guys for the reply...you're right, I meant close the forms!
Dale, when I run your code I get a wrong data type error

John, the VBE didn't like 'End iLoop'; I changed it to Next, thinking
that might help, but I'm now getting 'Object variable or With block
variable not set'
****code
Dim db As Access.Application
Dim frm As Form
Dim iLoop As Long

For iLoop = db.Forms.Count - 1 To 0 Step -1
Set frm = db.Forms(iLoop)
If frm.Visible Then
Debug.Print frm.Name
'DoCmd.Close acForm, frm.Name
End If
Next iLoop

I'll keep working on it.

Thanks,
alex
 
D

Douglas J. Steele

All you should need is"

Dim frm As Form
Dim iLoop As Long

For iLoop = Forms.Count - 1 To 0 Step -1
Set frm = Forms(iLoop)
If frm.Visible Then
Debug.Print frm.Name
'DoCmd.Close acForm, frm.Name
End If
Next iLoop


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)

The problem is that you are changing the number of forms that are open
when
you close Form(0) then Form(1) becomes Form(0) and Form(2) becomes
Form(1).

Access has handled Form(0) and moves on to Form(1) - which was Form(2)

The easiest way to handle this is to step through the open forms
collection in
reverse order. Something like the following untested code snippet:

Dim iLoop as Long

For iLoop = db.Forms.Count-1 to 0 step -1
Set frm = db.Forms(iLoop)
If frm.Visible then
docmd.close acform, frm.Name
end If
End iLoop

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County









- Show quoted text -

Thanks guys for the reply...you're right, I meant close the forms!
Dale, when I run your code I get a wrong data type error

John, the VBE didn't like 'End iLoop'; I changed it to Next, thinking
that might help, but I'm now getting 'Object variable or With block
variable not set'
****code
Dim db As Access.Application
Dim frm As Form
Dim iLoop As Long

For iLoop = db.Forms.Count - 1 To 0 Step -1
Set frm = db.Forms(iLoop)
If frm.Visible Then
Debug.Print frm.Name
'DoCmd.Close acForm, frm.Name
End If
Next iLoop

I'll keep working on it.

Thanks,
alex
 
A

alex

Thanks guys for the reply...you're right, I meant close the forms!
Dale, when I run your code I get a wrong data type error

John, the VBE didn't like 'End iLoop'; I changed it to Next, thinking
that might help, but I'm now getting 'Object variable or With block
variable not set'
****code
Dim db As Access.Application
Dim frm As Form
Dim iLoop As Long

For iLoop = db.Forms.Count - 1 To 0 Step -1
    Set frm = db.Forms(iLoop)
    If frm.Visible Then
        Debug.Print frm.Name
      'DoCmd.Close acForm, frm.Name
    End If
Next iLoop

I'll keep working on it.

Thanks,
alex- Hide quoted text -

- Show quoted text -

I guess I needed to set the db as well...figured it out. Thanks for
the advice!
alex
 

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