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

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
2 Pages V  1 2 >  (Go to first unread post)
   Reply to this topicStart new topic
> vba to close all open forms    
 
   
Richard70
post Dec 15 2009, 10:44 PM
Post#1



Posts: 456
Joined: 19-September 08



Hi. Can any one tell me how to close all open forms in the database with the exception of one specific form? Thanks.
Go to the top of the page
 
theDBguy
post Dec 15 2009, 11:01 PM
Post#2


Access Wiki and Forums Moderator
Posts: 75,754
Joined: 19-June 07
From: SunnySandyEggo


Hi,
One possible way may be:
Dim frm As Form
For Each frm in CurrentProject.AllForms
If frm.Name <> "SpecificFormName" Then
DoCmd.Close acForm, frm.Name
End If
Next frm
(untested)
Hope that helps...
Go to the top of the page
 
Richard70
post Dec 15 2009, 11:25 PM
Post#3



Posts: 456
Joined: 19-September 08



Thank you. I sincerely appreciate your help.
Go to the top of the page
 
theDBguy
post Dec 16 2009, 12:24 AM
Post#4


Access Wiki and Forums Moderator
Posts: 75,754
Joined: 19-June 07
From: SunnySandyEggo


You're welcome. Good luck!
Go to the top of the page
 
datAdrenaline
post Dec 16 2009, 12:40 AM
Post#5


UtterAccess Editor
Posts: 18,000
Joined: 4-December 03
From: Northern Virginia, USA


I would suggest the use of the Forms() collection since it is populated with all the Open forms, where as the .AllForms collection is all the forms in the database....
!--c1-->
CODE
Public Function CloseAllForms(ParamArray Exclusions()) As Byte
'This procedure will close all the forms open except for the ones passed through
'The argument list.  Only declared as a function for flexibility.  No meaningful
'data is returned. Note that "Join" is used, which is not available in A97.
'
    Dim strExcluded As String
    Dim intX As Integer, intY As Integer
    Const cDelimiter As String = "!" 'chose ! simply because it is not allowed in
                                     'a form name
    
    'Concatenate all the excluded form names
    strExcluded = cDelimiter & Join(Exclusions, cDelimiter) & cDelimiter
    
    'Loop all the forms and close them as appropriate
    For intX = Forms.Count - 1 To 0 Step -1
        If InStr(1, strExcluded, cDelimiter & Forms(intX).Name & cDelimiter, vbTextCompare) = 0 Then
            DoCmd.Close acForm, Forms(intX).Name, acSaveNo
        End If
    Next intX
    
End Function

Note: The reason for creating this as a Function is that it allows you to set the OnClick event of a button to: =CloseAllForms()
And thus avoid a "one-liner" of [Event Procedure] VBA code to call the procedure.
Go to the top of the page
 
theDBguy
post Dec 16 2009, 10:53 AM
Post#6


Access Wiki and Forums Moderator
Posts: 75,754
Joined: 19-June 07
From: SunnySandyEggo


Pretty slick, Brent. Thank you! thumbup.gif
Go to the top of the page
 
Richard70
post Dec 16 2009, 11:03 AM
Post#7



Posts: 456
Joined: 19-September 08



Thank you Brent!
Go to the top of the page
 
LPurvis
post Dec 16 2009, 01:15 PM
Post#8


UtterAccess Editor
Posts: 16,335
Joined: 27-June 06
From: England (North East / South Yorks)


Just FWIW the somewhat related subject of determining if a form is open - and the relative merits of each.
http://www.utteraccess.com/forums/showflat...;Number=1645207
Cheers.
Go to the top of the page
 
theDBguy
post Dec 16 2009, 02:44 PM
Post#9


Access Wiki and Forums Moderator
Posts: 75,754
Joined: 19-June 07
From: SunnySandyEggo


Hi Leigh,
believe I have that thread in my bookmarks. Really good info. thumbup.gif
Go to the top of the page
 
ronymaxwell
post Jun 16 2017, 04:53 AM
Post#10



Posts: 19
Joined: 12-February 11



Thank you for this. Simply realising that the loop for closing the forms had to be from the highest to the lowest solved my problem.
Go to the top of the page
 
jimbofoxman
post Jun 18 2019, 07:30 AM
Post#11



Posts: 379
Joined: 4-April 08



Sorry for the silly question.........maybe it's too early in the morning.

So from where ever that I am calling this from I am doing it like so???

CloseAllForms(Form1!Form2)
Go to the top of the page
 
Phil_cattivocara...
post Jun 18 2019, 07:36 AM
Post#12



Posts: 307
Joined: 2-April 18



QUOTE (jimbofoxman)
maybe it's too early in the morning
On UtterAccess the sun never sets. It is never night or day, it is always the right moment for somebody.
But I did not understand your question. Can you explain better, please?

--------------------
Please forgive in advance my horrible English.
Go to the top of the page
 
jimbofoxman
post Jun 18 2019, 08:09 AM
Post#13



Posts: 379
Joined: 4-April 08



Bascially I was trying to figure out how to specify which Form or Forms to exclude.
Go to the top of the page
 
isladogs
post Jun 18 2019, 08:28 AM
Post#14


UtterAccess VIP
Posts: 1,459
Joined: 4-June 18
From: Somerset, UK


The link provided by Leigh in post #8 appears to be broken

--------------------
Go to the top of the page
 
Phil_cattivocara...
post Jun 18 2019, 08:32 AM
Post#15



Posts: 307
Joined: 2-April 18



CODE
CloseAllForms("Form1!Form2")

This post has been edited by Phil_cattivocarattere: Jun 18 2019, 08:32 AM

--------------------
Please forgive in advance my horrible English.
Go to the top of the page
 
ADezii
post Jun 18 2019, 08:35 AM
Post#16



Posts: 2,463
Joined: 4-February 07
From: USA, Florida, Delray Beach


And still another approach:
CODE
If SysCmd(acSysCmdGetObjectState, acForm, "<Form Name>") <> 0 Then
  'Form is Open, Dirty, or New
End If

This post has been edited by ADezii: Jun 18 2019, 08:35 AM
Go to the top of the page
 
jimbofoxman
post Jun 18 2019, 08:45 AM
Post#17



Posts: 379
Joined: 4-April 08



Thanks Phil and ADezii!
Go to the top of the page
 
gemmathehusky
post Jun 18 2019, 09:08 AM
Post#18


UtterAccess VIP
Posts: 4,725
Joined: 5-June 07
From: UK


I use this to close all forms open in design mode. Easily amended to close all forms.
frm.currentview tests the openmode of the form.

isopen is a small function to test whether the form is open




CODE
Function IsOpen(strName As String, Optional objtype As Integer = acForm)
    IsOpen = (SysCmd(acSysCmdGetObjectState, objtype, strName) <> 0)
End Function

Function closedesignforms()
Dim frm As Object
Dim count As Long

count = 0
For Each frm In CurrentProject.AllForms
    
    If IsOpen(frm.Name, acForm) Then
        If frm.CurrentView = 0 Then
            DoCmd.close acForm, frm.Name
            count = count + 1
        End If
    End If
Next
        
    If count > 0 Then
        MsgBox (count & " forms closed")
    Else
        MsgBox ("No forms to close")
    End If

End Function

--------------------
Dave (Male)

(Gemma was my dog)
Go to the top of the page
 
isladogs
post Jun 18 2019, 10:38 AM
Post#19


UtterAccess VIP
Posts: 1,459
Joined: 4-June 18
From: Somerset, UK


Another variation on a theme
IsLoaded checks whether a form is open

CODE
Public Function IsLoaded(MyFormName)

On Error Resume Next

' Accepts: a form name
' Purpose: determines if a form is loaded
' Returns: True if specified the form is loaded;
'          False if the specified form is not loaded.
    
    Dim I As Integer
    IsLoaded = False
    For I = 0 To Forms.Count - 1
        If Forms(I).FormName = MyFormName Then
            IsLoaded = True
            Exit Function
        End If
    Next
    
End Function


Public Function CloseAllOpenFormsExceptOne()

Dim frm As Form
For Each frm in CurrentProject.AllForms
If IsLoaded(frm.Name) And frm.Name <> "SpecificFormName" Then
    DoCmd.Close acForm, frm.Name
End If
Next frm

End Function


@PhilC
Your CloseAllForms function isn't built in to Access so you would need to supply the code

--------------------
Go to the top of the page
 
Phil_cattivocara...
post Jun 18 2019, 10:49 AM
Post#20



Posts: 307
Joined: 2-April 18



QUOTE (isladog)
@PhilC
Your CloseAllForms function isn't built in to Access so you would need to supply the code

It is in this thread at #5.
I saw jimbofoxman wrote CloseAllForms and I thought he wanted to know how to use it

--------------------
Please forgive in advance my horrible English.
Go to the top of the page
 
2 Pages V  1 2 >


Custom Search


RSSSearch   Top   Lo-Fi    22nd July 2019 - 02:01 PM