Results 1 to 7 of 7
  1. #1
    JayGee1969 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    17

    Question Automatically close form following other action in VBA


    I have created a button on my form that opens another form showing linked data. Having opened the second form I would like the first form to close, but cannot seem to get it to work. I have tried various different bits of code but none seem to work.

    The code I have for the button at the moment (which does work insofar as opening the second form showing the correct record) is:
    Code:
    Private Sub Proceed_Click()
    On Error GoTo Err_Proceed_Click
    
    DoCmd.RunCommand acCmdSaveRecord
    
        Dim stDocName As String
        Dim stLinkCriteria As String
    
        stDocName = "frmCustOrd1"
        
        stLinkCriteria = "[OrderID]=" & Me![OrderID]
        DoCmd.OpenForm stDocName, , , stLinkCriteria
        
      
    Exit_Proceed_Click:
        Exit Sub
    
    Err_Proceed_Click:
        MsgBox Err.Description
        Resume Exit_Proceed_Click
        If Me.Dirty Then Me.Dirty = False
        
    End Sub
    Can anyone tell me what I need to add in and at what point to make the first form close?

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    DoCmd.Close

    with the appropriate arguments. You'd put it after opening the other form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    In the ON OPEN property of the form you open you can use code to check and see if the form you want to close is open as well, if it is, close it.

    here's the code to check to see if a form is open

    Code:
    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
              If Forms(strFormName).CurrentView <> conDesignView Then
             'perform your close form operation here
             'this checks to see if the form is open and if it's in anything but design view
              End If
    End If

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    The Form the OP wants to Close is the one the code appears in and is being executed from, so it is obviously Open at the time! There is no need to check its state!

    The most important of the 'appropriate arguments' Paul mentions is the actual name of the Form being Closed!

    Code:
    Private Sub Proceed_Click()
    On Error GoTo Err_Proceed_Click
    
    DoCmd.RunCommand acCmdSaveRecord
    
        Dim stDocName As String
        Dim stLinkCriteria As String
    
        stDocName = "frmCustOrd1"
        
        stLinkCriteria = "[OrderID]=" & Me![OrderID]
        DoCmd.OpenForm stDocName, , , stLinkCriteria
        
       DoCmd.Close acForm, "NameOfFirstForm"
      
    Exit_Proceed_Click:
        Exit Sub
    
    Err_Proceed_Click:
        MsgBox Err.Description
        Resume Exit_Proceed_Click
        If Me.Dirty Then Me.Dirty = False
        
    End Sub

    Replacing NameOfFirstForm with the actual name of the first Form.

    DoCmd.Close, by itself would work without problems, if there was only one Form involved here, but in order to not confuse the Access Gnomes, because you have two Forms, here, you need to specifically state the Form to be Closed! And to be honest, it's a good practice to get into the habit of always explicitly naming the Form in question.

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

    All posts/responses based on Access 2003/2007

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    God forbid someone might learn how to fish by having to look up the arguments themselves.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I actually included the check on purpose in case the form can be opened from more than one source which I have had occasion to use a few times. So rather than trapping an unnecessary error it performs a quick check, does nothing if the form is open but closes it if it is.

  7. #7
    JayGee1969 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    17

    Talking

    Sorry to have started an argument here but I really do appreciate this having been spelled out for me because I am very much a novice when it comes to VBA, and my next question would most certainly have been 'what are the appropriate arguments?'. I had already tried including DoCmd.Close into the coding and all I had succeeded in doing was closing the form the I wanted to open!

    Thanks Missinglinq and rpeare, this has solved it for me.

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

Similar Threads

  1. "Close form" action does not release table
    By TFisher in forum Programming
    Replies: 8
    Last Post: 10-23-2022, 11:54 AM
  2. Replies: 7
    Last Post: 07-18-2012, 07:53 AM
  3. Replies: 2
    Last Post: 05-11-2012, 11:52 AM
  4. Replies: 2
    Last Post: 06-20-2011, 03:10 PM
  5. Automatically close form in 5 seconds
    By alaric01 in forum Forms
    Replies: 4
    Last Post: 09-21-2010, 04:25 AM

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