Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272

    I want to vba code to close all forms except two forms


    I have a database with several forms.

    I wanted a vba code that could close all the forms except two forms: “frm_login” and “HIDE”

    Searching around, I came across these codes;


    Dim F As Access.Form
    Dim i As Long

    ' Loop all open forms, from last to first, to avoid problems due to closing forms
    ' (removing them from the Forms collection) in the loop
    For i = Forms.Count - 1 To 0 Step -1
    Set F = Forms(i)
    ' Close all forms except the login form
    If F.Name <> "frm_login" Then
    DoCmd.Close acForm, F.Name
    End If
    Next i

    End Sub


    Unfortunately, this code will close all forms except only the “frm_login”.

    Would be glad if I could get help to modify the code so it will close all forms except the “frm_login” and the “HIDE”

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,368
    Because you've only excluded 1 form name in your IF block? Add the other one(s):
    If F.Name <> "frm_login" Or F.Name <> "HIDE" Then
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Micron View Post
    Because you've only excluded 1 form name in your IF block? Add the other one(s):
    If F.Name <> "frm_login" Or F.Name <> "HIDE" Then
    Great
    Will give it a try and see wat happens

  4. #4
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Micron View Post
    Because you've only excluded 1 form name in your IF block? Add the other one(s):
    If F.Name <> "frm_login" Or F.Name <> "HIDE" Then
    That means I can use this same process to add more excluded forms using the “Or F.Name option I think

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,368
    There might be better ways - would depend on how many I guess. I might use a Select Case block or (maybe even better) Form.Tag property (see Other tab in property sheet). Close (or don't) those with/without a tag property value.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Micron View Post
    There might be better ways - would depend on how many I guess. I might use a Select Case block or (maybe even better) Form.Tag property (see Other tab in property sheet). Close (or don't) those with/without a tag property value.
    For now I only need two
    The highest may be 3 forms
    Hope it will work for a 3rd form

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,368
    Should not be an issue if written correctly. You could add another Or condition in the code line I guess, but I think I'd stop at 3 when it comes to doing that.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Micron View Post
    Should not be an issue if written correctly. You could add another Or condition in the code line I guess, but I think I'd stop at 3 when it comes to doing that.
    Alrite
    Thanks for your time
    I do appreciate

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,368
    Yer welcome. Good luck with it!
    You could mark this thread as solved if you're done with it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Micron View Post
    Yer welcome. Good luck with it!
    You could mark this thread as solved if you're done with it.
    This option didn’t work for me.
    Can you demonstrate how to use the select case so I can copy the codes and modify them to suit my needs?

  11. #11
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,368
    Doesn't work doesn't help, I often say. Can you post what you tried? Please enclose code in code tags (use # button on posting toolbar).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Micron View Post
    Doesn't work doesn't help, I often say. Can you post what you tried? Please enclose code in code tags (use # button on posting toolbar).
    #
    Dim F As Access.Form
    Dim i As Long

    ' Loop all open forms, from last to first, to avoid problems due to closing forms
    ' (removing them from the Forms collection) in the loop
    For i = Forms.Count - 1 To 0 Step -1
    Set F = Forms(i)
    ' Close all forms except the login form
    If F.Name <> "frm_login" Or F.Name <> “HIDE”Cmd.Close acForm, F.Name
    End If
    Next i

    End Sub #

  13. #13
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,368
    putting #'s in your code is not using code tags. Maybe
    Code:
    Dim F As Access.Form
    Dim i As Long
    
    ' Loop all open forms, from last to first, to avoid problems due to closing forms
    ' (removing them from the Forms collection) in the loop
    For i = Forms.Count - 1 To 0 Step -1
       Set F = Forms(i)
       'Close all forms except the login form
         If F.Name <> "frm_login" Or F.Name <> "HIDE"  Then
           DoCmd.Close acForm, F.Name
         End If
    Next i
    
    End Sub
    Those funny quote characters around HIDE in your code are not allowed. At least, I've never seen them not cause problems.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Micron View Post
    putting #'s in your code is not using code tags. Maybe
    Code:
    Dim F As Access.Form
    Dim i As Long
    
    ' Loop all open forms, from last to first, to avoid problems due to closing forms
    ' (removing them from the Forms collection) in the loop
    For i = Forms.Count - 1 To 0 Step -1
       Set F = Forms(i)
       'Close all forms except the login form
         If F.Name <> "frm_login" Or F.Name <> "HIDE"  Then
           DoCmd.Close acForm, F.Name
         End If
    Next i
    
    End Sub
    Those funny quote characters around HIDE in your code are not allowed. At least, I've never seen them not cause problems.
    So what is the way forward?

  15. #15
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,368
    Don't understand your question. I corrected your code - did you try it?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with VBA to have combobox open/close forms
    By templeowls in forum Programming
    Replies: 4
    Last Post: 03-10-2022, 10:29 AM
  2. Use of code to close forms
    By Roncc in forum Access
    Replies: 2
    Last Post: 01-21-2018, 08:24 PM
  3. Replies: 2
    Last Post: 09-02-2014, 06:21 AM
  4. Replies: 8
    Last Post: 05-11-2014, 08:52 AM
  5. should i close inactive forms
    By Mclaren in forum Forms
    Replies: 1
    Last Post: 07-07-2010, 02:39 PM

Tags for this Thread

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