Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code efficiency: all possible popups close with parent close 3

Status
Not open for further replies.

NewTexican

Technical User
Dec 10, 2004
95
US
Something tells me their might be a way to write this snippet of code better.

'closes all forms that can be opened from this one

Private Sub Command57_Click()

On Error GoTo Err_Command57_Click

Dim stDocName As String
Dim stdocname2 As String
Dim stdocname3 As String
Dim stdocname4 As String
Dim stdocname5 As String
Dim stdocname6 As String

stDocName = "Call Note F"
stdocname2 = "call note sf"
stdocname3 = "Company Mailout F"
stdocname4 = "Person popup from common f"
stdocname5 = "Person-complist f"
stdocname6 = "person address f"

DoCmd.Close acForm, stDocName
DoCmd.Close acForm, stdocname2
DoCmd.Close acForm, stdocname3
DoCmd.Close acForm, stdocname4
DoCmd.Close acForm, stdocname5
DoCmd.Close acForm, stdocname6
DoCmd.Close

Exit_Command57_Click:
Exit Sub

Err_Command57_Click:
MsgBox Err.Description
Resume Exit_Command57_Click

End Sub

What would you do?
 
Code:
Dim frm As Form
' Enumerate Open Forms in collection.
For Each frm In Forms
    DoCmd.Close acForm, frm.Name
Next frm
 
Along those lines, perhaps allow for some forms to stay open?

[tt]dim intX as long
for intX= intCount to 0 step -1
if forms(intX).Name <> "MyFormToKeepOpen" then
docmd.close acform,forms(intX).name
end if
next intx[/tt]

(from Close all open forms and "For Each" syntax)

Roy-Vidar
 
Just for the record Ginger, you'll find that the method you posted, will not close ALL the open forms, only half.

As you close a form, the Forms collection count, goes down by 2,(something like that), & thus, for every one you close, one stays open.
There are a few methods around this, here's one I like a lot, by a member here (who's name I, unfortunately forget).

Do While Forms.Count > 0
DoCmd.Close acForm, Forms(0).Name
Loop

He corrected mine, which was a lot more cumbersome.

Here's one by Dev Asish(I think, or someone from "WebAccess"), which allows for conditions...

Dim intx As Integer
Dim intCount As Integer
intCount = Forms.Count - 1
For intx = intCount To 0 Step -1
If Forms(intx).Name <> "MyFormToKeepOpen" Then
DoCmd.Close acForm, Forms(intx).Name
End If
Next

Either way, good luck to both of you!
 
Yeah, forgot to copy the line getting the count of forms [blush]

Roy-Vidar
 
I got that code long ago from Tek-Tips I think (it was for reports tho) and wondered why it didn't work for me, but figured it was ME for some reason. Thanks for the put. My doc filled with snippets has now been updated.
 
Ok, so their definitely is an "easier" way. Thanks.
 
Hi -

Here's an approach that avoids the Forms.count issue, and allows the user to specify the specific forms to be left open.
Code:
Public Sub FormClose2(ParamArray varMyVals() As Variant)
'Purpose:   Close all open forms except those specified
'Coded by:  raskew
'Inputs:    Call FormClose2("Form1", "frmXXX")
'Output:    All open forms closed except Form1, frmXXX
'*******************************************************

Dim db      As DATABASE
Dim cnt     As Container
Dim doc     As Document
Dim i       As Integer
Dim n       As Integer
Dim strHold As String

Set db = CurrentDb
Set cnt = db.Containers!Forms

For Each doc In cnt.Documents
   strHold = doc.Name
   'ensure the form is not intended to remain open
   n = 0
   For i = LBound(varMyVals) To UBound(varMyVals)
      If strHold = varMyVals(i) Then
         n = 1
         Exit For
      End If
   Next
   If n = 0 Then
      'Is the form open?  If true, close it.
      If SysCmd(acSysCmdGetObjectState, acForm, strHold) Then
         docmd.Close acForm, strHold, acSaveYes
      End If
   End If
Next doc

db.Close
Set db = Nothing

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top