Results 1 to 10 of 10
  1. #1
    gstylianou is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    167

    Code for closing all opened forms

    Hello to all forum friends,


    I would like to ask if there is a way so that using vba code to be able to closed any opened form in my database. I would appreciate if someone could enlighten me.




    Thank you in advance

  2. #2
    atuhacek is offline New
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Location
    Michigan
    Posts
    61
    This function will close all forms with save.

    Code:
    Function CloseAllForms()
    
    
        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
                DoCmd.Close acForm, obj.Name, acSaveYes
                ' Print name of obj.
                Debug.Print obj.Name
            End If
        Next obj
    End Function

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The acSaveYes parameter has nothing to do with saving records, it is to save design changes.

    Record edits are committed to table when form closes, move to another record, or run acCmdSaveRecord command.

    Alternatively, try: DoCmd.RunCommand acCmdCloseAll
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    atuhacek is offline New
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Location
    Michigan
    Posts
    61
    Its not used for records it used to prevent save changes dialog from popping up. Which can be omitted if you choose. What is the relavence of move to another record or acCmdSaveRecord to the above code. You could use

    DoCmd.RunCommand acCmdCloseAll

    it you desire to close all open objects.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Good point, but it will only popup if there were changes to properties. acSaveNo would accomplish the same and is more reassuring to me than acSaveYes, even though I know changes can only be saved from design view.

    The suggested code will close all objects, not the db itself.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    atuhacek is offline New
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Location
    Michigan
    Posts
    61
    I know that but the question was to close forms I had to assume that was all they wanted closed. The best way I know to accomplish this is iterating through the database object.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Just offering user options to consider.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    gstylianou is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    167
    Dear all,

    I would like to thank you very much for your help. One thing more if you can suggest a solution. How can write the code in order to close all active forms excepts the switchboard?

    Thanks again all of you

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Include another nested If. Is Switchboard the name of form?

    If obj.Name <> "Switchboard" Then
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    gstylianou is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    167
    June7,

    The name of my switchboard form is "frmSwitchboard". I' ll try the suggested code line to see whats happened.

    Thanks a lot

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 07-24-2013, 11:49 AM
  2. Closing Datasheet Forms Within Each Other
    By jfriedm in forum Access
    Replies: 11
    Last Post: 11-23-2012, 03:31 PM
  3. Issue w/ VBA code for Opening/Closing Forms
    By need_help12 in forum Forms
    Replies: 5
    Last Post: 04-20-2012, 11:09 AM
  4. Don't want to save forms on Refresh or closing
    By accesscoder in forum Forms
    Replies: 5
    Last Post: 09-25-2010, 12:03 AM
  5. Code needed when Database is Opened
    By Access_Headaches in forum Access
    Replies: 7
    Last Post: 08-13-2010, 01:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums