UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Someone Please Show Me An Easier Way.....Please....    
 
   
sredworb
post Dec 30 2006, 07:28 PM
Post #1

UtterAccess Guru
Posts: 918
From: Chicago Suburb



Im Using This To Close forms When Other forms Open.

CODE
Private Sub Form_Open(Cancel As Integer)
DoCmd.RunCommand acCmdSizeToFitForm
DoCmd.Close acForm, "frmPlateCheck"
DoCmd.Close acForm, "frmMainMenu"
DoCmd.Close acForm, "frmMasterName"
DoCmd.Close acForm, "frmMaxSearch"
DoCmd.Close acForm, "frmAssociatedVehicleToPerson"
DoCmd.Close acForm, "frmVehicleRelatedToGang"
DoCmd.Close acForm, "frmVehicleRelatedToLocation"
DoCmd.Close acForm, "frmGangs"
DoCmd.Close acForm, "frmSwitchBoard"
End Sub


Please Tell Me Their Is An Easier Way.....

Jerry
Go to the top of the page
 
+
Alan_G
post Dec 30 2006, 08:27 PM
Post #2

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,885
From: Devon UK



Hi

I guess the question has to be why so many forms open in the first place, but yes there is an easier way. The following code will close all open forms apart from any form name you explicitly add to the If statement.......

CODE
Dim intX As Integer
   Dim intCount As Integer
   intCount = Forms.Count - 1
   For intX = intCount To 0 Step -1
        If Forms(intX).Name <> "NameOfFormNotToClose" Then
            DoCmd.Close acForm, Forms(intX).Name
        End If
   Next
Go to the top of the page
 
+
sredworb
post Dec 30 2006, 08:31 PM
Post #3

UtterAccess Guru
Posts: 918
From: Chicago Suburb



CODE
I guess the question has to be why so many forms open in the first place,

Not the case, some forms open / associate with other ones...Hard to explain.

Thanks For the answer though..

Jerry
Go to the top of the page
 
+
sredworb
post Dec 30 2006, 08:35 PM
Post #4

UtterAccess Guru
Posts: 918
From: Chicago Suburb



Worked like a charm...Thanks.
Go to the top of the page
 
+
vtd
post Dec 30 2006, 08:35 PM
Post #5

Retired Moderator
Posts: 19,667



Try the following:
CODE
Public Sub CloseLeftOverForms(strFormsToRemain)

' Usage: call the Sub with argument being a semicolon-separated list of

'   Form Names to remain open.

' Example:

'   Call CloseLeftOverForms("frmMenu_Main;frmLogIn")

'   to close all Forms except frmLogIn & frmMenu_Main

  

  Dim intCountFormsToRemain As Integer

  Dim intCount As Integer

  Dim intIndex As Integer

  Dim frm As Access.Form

  

On Error GoTo CloseLeftOverForms_Err

  intCountFormsToRemain = UBound(Split(strFormsToRemain, ";")) + 1

  intCount = Forms.Count

  If (intCount > intCountFormsToRemain) Then

    For intIndex = intCount - 1 To 0 Step -1

      If (InStr(1, strFormsToRemain, Forms.Item(intIndex).Name) = 0) Then

        DoCmd.Close acForm, Forms.Item(intIndex).Name, acSaveNo

      End If

    Next intIndex

  End If

  

CloseLeftOverForms_Exit:

  On Error Resume Next

  Exit Sub

  

CloseLeftOverForms_Err:

  Select Case Err.Number

    Case 0

    Case Else

      MsgBox "Error " & Err.Number & ": " & Err.Description & vbCrLf & vbCrLf & _

        "(Programmer's note: Sub CloseLeftOverForms)", _

        vbOKOnly + vbCritical, "Run-time Error!"

  End Select

  Resume CloseLeftOverForms_Exit

End Sub


Edited by: VanThienDinh on Sat Dec 30 20:36:52 EST 2006.
Go to the top of the page
 
+
Alan_G
post Dec 30 2006, 08:55 PM
Post #6

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,885
From: Devon UK



Van's code is much more portable and complete, but either way we're glad to help........ (IMG:http://www.utteraccess.com/forum/style_emoticons/default/sad.gif)
Go to the top of the page
 
+
mishej
post Dec 30 2006, 09:25 PM
Post #7

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



I'd modify Van's code just a little to prevent the possibility of leaving open a form with a matching (but longer name), e.g. "frmInventory" and "frmInventorySearch". If you wanted to keep frmInventory open then frmInventorySearch (if open) would also remain open. So I did a small change:
CODE
' from UtterAccess - written by VanThienDinh
'
' I modified slightly to prevent a problem with the InStr() finding a match
' on partial form name, for example, if you have frmInventory and frmInventorySearch
' and pass in "frmInventory" then both would remain open
'
' Usage: call the Sub with argument being a semicolon-separated list of
'   Form Names to remain open.
' Example:
'   Call CloseLeftOverForms("frmMenu_Main;frmLogIn")
'   to close all Forms except frmLogIn & frmMenu_Main
'
Public Sub CloseLeftOverForms(strFormsToRemain As String)
    On Error GoTo CloseLeftOverForms_Err
    
    Dim nCountFormsToRemain As Long
    Dim nCount As Long
    Dim nIndex As Long
    Dim frm As Access.Form
    Dim arrFormsToRemain() As String
    Dim bClose As Boolean, x As Long
    
    arrFormsToRemain = Split(strFormsToRemain, ";")
    nCountFormsToRemain = UBound(arrFormsToRemain) + 1
    nCount = Forms.Count
    
    If (nCount > nCountFormsToRemain) Then
        For nIndex = (nCount - 1) To 0 Step -1
            bClose = True
            For x = 0 To (nCountFormsToRemain - 1)
                If (StrComp(arrFormsToRemain(x), Forms.Item(nIndex).name, vbTextCompare) = 0) Then
                    ' have a match, leave this form open
                    bClose = False
                    Exit For
                End If
            Next x
            
            If bClose = True Then
                DoCmd.Close acForm, Forms.Item(nIndex).name, acSaveNo
            End If
        Next nIndex
    End If
    
CloseLeftOverForms_Exit:
    On Error Resume Next
    Exit Sub
    
CloseLeftOverForms_Err:
    Select Case Err.Number
      Case 0
      Case Else
        MsgBox "Error " & Err.Number & ": " & Err.Description & vbCrLf & vbCrLf & _
          "(from Sub CloseLeftOverForms)", _
          vbOKOnly Or vbCritical, "Run-time Error!"
    End Select
    Resume CloseLeftOverForms_Exit
End Sub
Go to the top of the page
 
+
vtd
post Dec 31 2006, 01:25 AM
Post #8

Retired Moderator
Posts: 19,667



Excellent mods, John ...

I didn't think about finding a match on partial form name.

Thanks & cheers
Go to the top of the page
 
+
mishej
post Dec 31 2006, 02:03 AM
Post #9

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



Thanks Van. Fresh set of eyes have the advantage.
Go to the top of the page
 
+
sredworb
post Jan 1 2007, 01:55 PM
Post #10

UtterAccess Guru
Posts: 918
From: Chicago Suburb



Thanks Everyone, I really appreciate the help.

Jerry
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 24th May 2013 - 09:38 PM