How do I close an open form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sueb
    Contributor
    • Apr 2010
    • 379

    How do I close an open form?

    I have a little form that I want to:

    1. accept a new primary key (chart number) (this is the only field on the form),
    2. add a new record with this primary key,
    3. open the main form, and
    4. close itself

    So far, it does 1 through 3, but doesn't do 4, and also gives me an error, saying that there's an operator missing on line 12 of the following code:

    Code:
    Private Sub ChartNum_Exit(Cancel As Integer)
        Dim stDocName As String
        Dim stMainForm As String
        Dim stLinkCriteria As String
    
        stMainForm = "Patient_IUR_Overview"
        stDocName = "Patients: Add"
        
        DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
        'DoCmd.Close stDocName
        
        stLinkCriteria = "[Patients.Patient Index]=" & Me![Patient Index]
        DoCmd.OpenForm stMainForm, , , stLinkCriteria
    
    Exit_ChartNum_Exit:
        Exit Sub
    
    Err_ChartNum_Exit:
        MsgBox Err.Description
        Resume Exit_ChartNum_Exit
    
    End Sub
    I'm pretty sure that it has something to do with the index, but I don't know how to fix that line (I cribbed it from another module that behaves similarly).

    Thanks in advance, because I know someone will have posted the answer almost before I can come back to check for it!
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    It looks like you commented out closing the form on line 10. Was this on purpose?

    Comment

    • sueb
      Contributor
      • Apr 2010
      • 379

      #3
      I got a "type mismatch" on that line, and I wasn't even sure what this "Close" command was going to attempt to close.

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        Here is the MSDN link on the DoCmd.Close method. You got the type mismatch because you are sending a string where it expects an AcObjectType.

        Comment

        • sueb
          Contributor
          • Apr 2010
          • 379

          #5
          Thanks for that link, but here is the line I'm using, and it gives me a run-time error of 2585, saying: "This action can't be carried out while processing a form or report event."

          Code:
          DoCmd.Close acForm, stDocName, acSaveYes
          What's wrong with this line?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Your form is named "Patients: Add"? Bad choice of name. Does "[Patients: Add]" work?

            Comment

            • sueb
              Contributor
              • Apr 2010
              • 379

              #7
              So I changed the two lines of code to read:

              Code:
                  DoCmd.RunCommand acCmdSaveRecord
                  DoCmd.Close , stDocName
              and here's what happens:

              1. I add the new chart number, and the correct window opens.
              2. I close that window manually, and find that the original "add" window is still there.
              3. When I click the window's "X" button, I get: "Syntax error (missing operator) in query expression '[Patients.Patien t Index]='."

              How can it be opening the right window if it's having a problem with that particular line? It's actually adding a record for the chart number I entered, and it opens the correct window to look at all the fields for that record, so what's going on? And why does this error only show up when I manually try to close the "add" window?

              Comment

              • eneyardi
                New Member
                • Jul 2010
                • 180

                #8
                on deactivate event macro,put type close on action

                Comment

                • sueb
                  Contributor
                  • Apr 2010
                  • 379

                  #9
                  Do what, where, now?

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    [Patients.Patien t Index] is the name of the field? If it's not, then that syntax is wrong. Just use [Patient Index] or [Patients].[Patient Index].

                    Comment

                    • sueb
                      Contributor
                      • Apr 2010
                      • 379

                      #11
                      Neither of those syntaxes work. Just to refresh things, what happens is:

                      - the add window pops up
                      - I enter a new number (The only action that causes the patient window to come up is TAB; RETURN just clears the field. However, if I manually open the patient form, and search for the new number, it's been added.
                      - the patient window opens
                      - the add window remains open (behind the patient window), and if I close it using the "X" button in the upper-right-hand corner, I get the error message "Run-time error 3075: Syntax error (missing operator) in query expression '[Patient Index]]='."

                      Here's the module as it stands:

                      Code:
                      Private Sub ChartNum_Exit(Cancel As Integer)
                          Dim stDocName As String
                          Dim stMainForm As String
                          Dim stLinkCriteria As String
                      
                          stMainForm = "Patient_IUR_Overview"
                          stDocName = "Patients: Add"
                          
                          DoCmd.RunCommand acCmdSaveRecord
                          
                          stLinkCriteria = "[Patient Index]=" & Me![Patient Index]
                          'stLinkCriteria = "[Patients].[Patient Index]=" & Me![Patient Index]
                          DoCmd.OpenForm stMainForm, , , stLinkCriteria
                          
                          DoCmd.Close , stDocName
                      
                      Exit_ChartNum_Exit:
                          Exit Sub
                      
                      Err_ChartNum_Exit:
                          MsgBox Err.Description
                          Resume Exit_ChartNum_Exit
                      
                      End Sub

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        Can you upload your database?

                        Comment

                        • john garvey
                          New Member
                          • Jan 2011
                          • 50

                          #13
                          From your code the docmd needs an oject to work with, if your code is attached to the form then try DoCmd.Close acForm, Me.Name

                          Comment

                          • sueb
                            Contributor
                            • Apr 2010
                            • 379

                            #14
                            Rabbit, here's my database.

                            And, john garvey, I think I'm already trying that on line 15. Or is that line wrong? or something?
                            Attached Files

                            Comment

                            • john garvey
                              New Member
                              • Jan 2011
                              • 50

                              #15
                              You need to tell Access what to close and what its name is

                              Comment

                              Working...