Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2012
    Posts
    70

    Unanswered: How can I make a button where one form closes as another opens?

    I am making a start page to my DB with multiple buttons, I want a button to switch between different forms but I do not want there to be lots of them open after the user has selected a few different buttons.

    Can I have a button to switch the current form (i.e. close one whilst opening another)?


    Please can it be without this code I see all around the forum? If it needs code, can someone please tell me how to use it? Thanks

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    As far as I know, it cannot be done without some VBA code. one solution would consist in:
    a) In a new Module (or an existing one), create the following procedure:
    Code:
    Public Sub CloseAllForms(Optional ByVal Except As String)
    
        Dim obj As AccessObject
        Dim dbs As Object
        Set dbs = Application.CurrentProject
        For Each obj In dbs.AllForms
            If obj.IsLoaded = True Then
                If obj.Name <> Except Then DoCmd.Close acForm, obj.Name
            End If
        Next obj
    
    End Sub
    b) In the module of every form that must close any other open ones when it opens, add the following procedure:
    Code:
    Private Sub Form_Open(Cancel As Integer)
    
        CloseAllForms Except:=Me.Name
        
    End Sub
    Have a nice day!

  3. #3
    Join Date
    Aug 2012
    Posts
    70
    Thanks, I've just found out how to implement that onto one of the forms.

    However, how can I place the b) part in other forms?

    I can't find any of the other forms in the editor (only the one form appears in the navigation pane on the left.

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Are there several forms in the database?
    Have a nice day!

  5. #5
    Join Date
    Aug 2012
    Posts
    70
    yes, there are

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14

    Question

    Quote Originally Posted by Sinndho View Post
    Are there several forms in the database?
    Answer:
    Quote Originally Posted by kkid View Post
    yes, there are
    This seems contradictory with:
    Quote Originally Posted by kkid View Post
    I can't find any of the other forms in the editor (only the one form appears in the navigation pane on the left.
    Have a nice day!

  7. #7
    Join Date
    Aug 2012
    Posts
    70
    I know, I can see them all in Access but when in Visual Basic only the current form shows up

  8. #8
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    If the other forms have a module already, open the Project Explorer from the VBA Editor (Ctrl+R) and navigate from Form Module to Form Module with it. If the other forms do not have a module, open every one of them in Design mode and create a module for every one of them.
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •