Results 1 to 7 of 7
  1. #1
    rescobar is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    30

    How to close a form

    I have a Form called "Search" which runs a query. The query result opens a form called "Search Result"
    When I open form "Search" again I want "Search Result" to close. Currently unless I close the form "Search Result" it will display the previous result unless I close it.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It depends on when and where you want this to happen. If it is in the form load event, you will have to check if the form is open or have error handling code to deal with trying to close a from that is not open.

    The command I use is:

    generic:
    Code:
    DoCmd.Close acForm, Me.Name

    in your case, I might use:
    Code:
    DoCmd.Close acForm, "SearchResult"

  3. #3
    rescobar is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    30
    I tried DoCmd.Close acForm, "SearchResult" but getting it wrong with how to place it. Here's my Search Form code

    Option Compare Database
    Private Sub Form_Load()
    DoCmd.Maximize
    End Sub

    Private Sub clear_Click()
    Me.qProduct.Value = ""
    Me.qModel.Value = ""
    Me.qSummary.Value = ""
    Me.qDetails.Value = ""
    Me.qSolution.Value = ""
    Me.cboProduct.Value = Null
    Me.cboModel.Value = Null
    End Sub

    ' This code opens the Search Result Form and comments out mainquery
    Private Sub Command33_Click()
    'DoCmd.OpenQuery "mainquery", acViewNormal
    DoCmd.OpenForm "Search Result"
    'This will Maximize window
    DoCmd.Maximize
    End Sub

    Private Sub cboModel_Click()
    Me.qModel = Me.cboModel
    End Sub

    Private Sub cboProduct_Click()
    Me.qProduct = Me.cboProduct
    End Sub

    Private Sub cboProduct_AfterUpdate()
    Me.cboModel = vbNullString
    Me.cboModel.Requery
    End Sub

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try
    Code:
    Option Compare Database  '<<= should be at the top of every code page
    Option Explicit     '<<= should be at the top of every code page
    
    Private Sub Form_Load()
        DoCmd.Maximize
        
        'handles error if form in not loaded
        On Error Resume Next
    
        DoCmd.Close acForm, "Search Result"
    
    End Sub

  5. #5
    rescobar is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    30
    No that's not working. Maybe this will be more useful.

    I open "Search" Form and do a query.
    Result: "Search Result" Form Opens.

    Now I open "Search" Form again and do a different query.
    Result: The previous result is displayed in "Search Result" Form.
    NOTE: If I close the "Search Result" Form and Search again it works as expected.

  6. #6
    rescobar is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    30
    I fixed it. Tried this code before but had a typo. This worked.
    ' This code opens the Search Result Form inand comments out mainquery
    Private Sub Command33_Click()
    'This code will close Search Result form on new query
    DoCmd.Close acForm, "Search Result"
    'DoCmd.OpenQuery "mainquery", acViewNormal
    DoCmd.OpenForm "Search Result"
    'This will Maximize window
    DoCmd.Maximize
    End Sub



    Thanks

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I understood that you were closing form "Search", then re-opening it.

    Your code is exactly right! Glad you found the typo and fixed it.

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

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2012, 11:52 AM
  2. Replies: 11
    Last Post: 01-26-2012, 01:22 PM
  3. Replies: 2
    Last Post: 06-20-2011, 03:10 PM
  4. Close All but One Form
    By Bruce in forum Forms
    Replies: 4
    Last Post: 04-06-2010, 09:31 AM
  5. close form
    By taylorosso in forum Forms
    Replies: 5
    Last Post: 08-25-2009, 12:18 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