make one form wait for another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #1

    make one form wait for another

    I have a form that opens another form. I want the first form to wait for the form it opened to be filled out before it proceeds with processing what comes after the opening of the other form.

    In this instance, a form used by the warehouse/shipping system is opening a form to get credit card information. After the credit card info is entered (it may be approved or not approved) I want to close the credit card form and update the order's status.

    How can I make the first form wait for the second form's "done" or 'cancel' button to be clicked? I am working with Access 2003.

    Thanks,
    Jim
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hi, Jim.

    Objects (including form objects) have a special mechanism to communicate - events.
    The following code snippet illustrates a general approach. The scenario is the following:
    • [frmParent] has a button to open [frmChild]
    • [frmChild] has button [Done] which when pressed generates event handled in [frmParent]


    [frmParent] module
    [code=vb]
    Dim WithEvents frmChildForm As Form_frmChild

    Private Sub btnOpenForm_Cli ck()
    DoCmd.OpenForm "frmChild"
    Set frmChildForm = Forms!frmChild
    End Sub

    Private Sub frmChildForm_Do ne(strReturn As String)
    MsgBox "Child form has done something" & vbCrLf & "And says: " & strReturn
    End Sub
    [/code]

    [frmChild] module
    [code=vb]
    Event Done(strReturn As String)

    Private Sub btnDone_Click()
    RaiseEvent Done("I have done")
    End Sub
    [/code]

    Enjoy OOP.

    Kind regards,
    Fish

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      I believe the short answer here would be to open the second form in Dialog mode, which does exactly what you want:

      DoCmd.OpenForm YourFormName, , , , , acDialog


      Linq ;0)>

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by missinglinq
        I believe the short answer here would be to open the second form in Dialog mode, which does exactly what you want:

        DoCmd.OpenForm YourFormName, , , , , acDialog


        Linq ;0)>
        I'm with Linq on this one:
        [CODE=vb]
        DoCmd.OpenForm "frmChild", acNormal, , , acFormEdit, acDialog

        'by opening the frmChild Window in Dialog Mode, its Modal and Popup
        'properties are set to True, all procesing from this point on will
        'be suspended until frmChild is closed
        '...
        'will never execute until frmChild is closed
        Msgbox "Done in frmChild"[/CODE]

        Comment

        • FishVal
          Recognized Expert Specialist
          • Jun 2007
          • 2656

          #5
          Sure Linq's suggestion is simpler and doesn't touch programming aspects that may be new or complicated for OP.

          But the child form is expected to return some information (credit card number and user choice as for action type) to the parent form. When opening form in Dialog mode code execution in Parent form proceeds after Child form is closed and thus inaccessible. So the options are the following.
          • global variable - IMHO not the best possible solution
          • child form calls method/property in parents form module
          • child form raises event handled in parent form module
          • something else I cannot figure so far


          Regards,
          Fish

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by FishVal
            Sure Linq's suggestion is simpler and doesn't touch programming aspects that may be new or complicated for OP.

            But the child form is expected to return some information (credit card number and user choice as for action type) to the parent form. When opening form in Dialog mode code execution in Parent form proceeds after Child form is closed and thus inaccessible. So the options are the following.
            • global variable - IMHO not the best possible solution
            • child form calls method/property in parents form module
            • child form raises event handled in parent form module
            • something else I cannot figure so far


            Regards,
            Fish
            Good points, FishVal! I was under the assumption that once the Child Form was closed, the data, if entered, would persist in the form of being written to a Table related to the Order itself. If that were the case, it could be easily retrieved. Don't mind me, just off on another tangent again. (LOL).

            Comment

            • jimatqsi
              Moderator Top Contributor
              • Oct 2006
              • 1293

              #7
              Thanks to everyone for their advice. I'm so glad to have gotten more than one way to solve this.

              However, being under the gun, I came up with another solution while I was waiting for your great ideas. I put an endless loop with a DoEvents in it after opening the child form. Then I changed the "Done" button on the child form to update an invisible checkbox to True. I break out of the endless loop when that checkbox on the child form becomes True and I close the child from from within the parent.

              That seems to have solved the problem. Does it cause any problems I don't know about?

              Thanks very much!

              Jim

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Originally posted by jimatqsi
                Thanks to everyone for their advice. I'm so glad to have gotten more than one way to solve this.

                However, being under the gun, I came up with another solution while I was waiting for your great ideas. I put an endless loop with a DoEvents in it after opening the child form. Then I changed the "Done" button on the child form to update an invisible checkbox to True. I break out of the endless loop when that checkbox on the child form becomes True and I close the child from from within the parent.

                That seems to have solved the problem. Does it cause any problems I don't know about?

                Thanks very much!

                Jim
                DoEvents occupies valuable CPU time and passes control to the Operating System where it processes Events in it Queue. You must be very careful when you use DoEvents within an Event Procedure. If by any chance the procedure is executed again from a different part of your code before the first call returns; this could cause unpredictable results

                Comment

                • FishVal
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2656

                  #9
                  I agree with ADezii.

                  Using DoEvents requires many nuances to be kept in mind.
                  For example you need to prevent frmChild closing by any way except you've implemented.
                  Closing it via CloseWindow button will result in fault in frmParent loop.
                  Moreover closing it with Alt-F4 crashes Access completely. ;)

                  Regards,
                  Fish.

                  P.S. And I never liked that 100% CPU usage indicated in Task Manager when DoEvents loop is running. Looks very suspicious.

                  Comment

                  Working...