469,963 Members | 1,314 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

make one form wait for another

1,263 Expert 1GB
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
Mar 21 '08 #1
8 7698
FishVal
2,653 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. Dim WithEvents frmChildForm As Form_frmChild
  2.  
  3. Private Sub btnOpenForm_Click()
  4.     DoCmd.OpenForm "frmChild"
  5.     Set frmChildForm = Forms!frmChild
  6. End Sub
  7.  
  8. Private Sub frmChildForm_Done(strReturn As String)
  9.     MsgBox "Child form has done something" & vbCrLf & "And says: " & strReturn
  10. End Sub
  11.  
[frmChild] module
Expand|Select|Wrap|Line Numbers
  1. Event Done(strReturn As String)
  2.  
  3. Private Sub btnDone_Click()
  4.     RaiseEvent Done("I have done")
  5. End Sub
  6.  
Enjoy OOP.

Kind regards,
Fish
Mar 21 '08 #2
missinglinq
3,532 Expert 2GB
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)>
Mar 21 '08 #3
ADezii
8,800 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmChild", acNormal, , , acFormEdit, acDialog
  2.  
  3. 'by opening the frmChild Window in Dialog Mode, its Modal and Popup
  4. 'properties are set to True, all procesing from this point on will
  5. 'be suspended until frmChild is closed
  6. '...
  7. 'will never execute until frmChild is closed
  8. Msgbox "Done in frmChild"
Mar 22 '08 #4
FishVal
2,653 Expert 2GB
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
Mar 22 '08 #5
ADezii
8,800 Expert 8TB
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).
Mar 22 '08 #6
jimatqsi
1,263 Expert 1GB
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
Mar 23 '08 #7
ADezii
8,800 Expert 8TB
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
Mar 23 '08 #8
FishVal
2,653 Expert 2GB
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.
Mar 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by David Gagné | last post: by
5 posts views Thread by Tamir Khason | last post: by
4 posts views Thread by Mike | last post: by
4 posts views Thread by jaYPee | last post: by
6 posts views Thread by Leszek | last post: by
8 posts views Thread by C.Joseph Drayton | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.