Results 1 to 11 of 11
  1. #1
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237

    Closing multiple forms at once with vba

    is there an issue with closing 2 or more forms at once and returning to a login form with this code....



    Private Sub LogOut1_Click()
    DoCmd.Close acForm, "JobProgress", acSaveYes
    DoCmd.Close acForm, "CabinetTech", acSaveYes
    DoCmd.OpenForm "LoginForm"
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,825
    You tell us if there is an issue - error message, wrong results, nothing happens?

    The acSaveYes has nothing to do with saving data, it is to save design changes.
    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.

  3. #3
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Im still getting used to how code is written it just seems to easy.... just making sure i dont build the database and end up with awhole lot of errors...
    It works fine, code ive seen to open forms is generally a little bigger...

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,101
    I use the custom function below (goes into a standard module) when I want to close multiple forms and leave only one open:

    Code:
    Function fCloseAll(Optional strForm As String)Dim icount As Integer, intX As Integer
    'strform is the form/report required to be left open while all
    'else are closed.
    icount = Application.Forms.Count - 1
        'close all forms
        For intX = icount To 0 Step -1
            If Forms(intX).Name <> strForm Then
                DoCmd.Close acForm, Forms(intX).Name
            End If
        Next
        
    End Function
    Use it like this fCloseAll(Me.Name) 'from the form you want to leave open

    Cheers,
    Vlad

  5. #5
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by Gicu View Post
    I use the custom function below (goes into a standard module) when I want to close multiple forms and leave only one open:

    Code:
    Function fCloseAll(Optional strForm As String)Dim icount As Integer, intX As Integer
    'strform is the form/report required to be left open while all
    'else are closed.
    icount = Application.Forms.Count - 1
        'close all forms
        For intX = icount To 0 Step -1
            If Forms(intX).Name <> strForm Then
                DoCmd.Close acForm, Forms(intX).Name
            End If
        Next
        
    End Function
    Use it like this fCloseAll(Me.Name) 'from the form you want to leave open

    Cheers,
    Vlad
    not sure if I understand....so insert the code into a module as you've written it?

    then fcloseAll(Me.LoginForm) in the LoginForm?

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,101
    almost, yes add the function to any standard modules and the call is
    Code:
    Call fCloseAll(Me.Name)  ' or you can also use Call fCloseAll("LoginForm") if that is the name of your form
    Cheers,
    Vlad

  7. #7
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by Gicu View Post
    almost, yes add the function to any standard modules and the call is
    Code:
    Call fCloseAll(Me.Name)  ' or you can also use Call fCloseAll("LoginForm") if that is the name of your form
    Cheers,
    Vlad
    ok cool.... im not familiar with modules how do they work.. does every form have its own module?

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,101
    yes they do, but you want that function in a standard module (one that appears in the Modules collection of you database). Here are few links:
    https://access-programmers.com/modules
    https://msdn.microsoft.com/en-us/vba...-object-access
    https://access-programmers.co.uk/for...d.php?t=140489
    Cheers,
    Vlad

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    Quote Originally Posted by ShaunG View Post

    ...just making sure i dont build the database and end up with awhole lot of errors...
    That's why you test as you go...and why you make regular backup copies of your file! If you really scrooch the pooch, you can always go back to your last 'good' copy.

    Quote Originally Posted by ShaunG View Post

    ...code ive seen to open forms is generally a little bigger...
    That depends on what you need to do when you open a file...whether you need to be able to add Records, edit Records...whether you want the Form to open in Datasheet View (even if its Default View is Datasheet View, if you're opening it thru code, you have to specify this) whether you need to go to a particular Record, when opening the Form, and so forth.

    A login Form probably requires none of this, but simply needs to be opened, hence

    DoCmd.OpenForm "LoginForm"

    is probably all that is needed.

    Welcome to AFN!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  10. #10
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by Gicu View Post
    yes they do, but you want that function in a standard module (one that appears in the Modules collection of you database). Here are few links:
    https://access-programmers.com/modules
    https://msdn.microsoft.com/en-us/vba...-object-access
    https://access-programmers.co.uk/for...d.php?t=140489
    Cheers,
    Vlad
    Cheers ill have a sus

  11. #11
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by Missinglinq View Post
    That's why you test as you go...and why you make regular backup copies of your file! If you really scrooch the pooch, you can always go back to your last 'good' copy.



    That depends on what you need to do when you open a file...whether you need to be able to add Records, edit Records...whether you want the Form to open in Datasheet View (even if its Default View is Datasheet View, if you're opening it thru code, you have to specify this) whether you need to go to a particular Record, when opening the Form, and so forth.

    A login Form probably requires none of this, but simply needs to be opened, hence

    DoCmd.OpenForm "LoginForm"

    is probably all that is needed.

    Welcome to AFN!

    Linq ;0)>
    Yeah im backing up as I go... I learnt the hard way!

    Yeah ok cool I will need to add and edit records as well so good to know thanks

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

Similar Threads

  1. closing forms if they are currently hidden
    By jdashm in forum Access
    Replies: 2
    Last Post: 04-11-2016, 02:32 PM
  2. Replies: 5
    Last Post: 01-27-2016, 07:05 AM
  3. Code for closing all opened forms
    By gstylianou in forum Access
    Replies: 9
    Last Post: 06-30-2014, 01:35 AM
  4. Closing Datasheet Forms Within Each Other
    By jfriedm in forum Access
    Replies: 11
    Last Post: 11-23-2012, 03:31 PM
  5. 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

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