Loop problem?

L

Leslie Isaacs

Hello All

I have the following code behind a button on "frm main":

Dim counter As Integer
For counter = Me.CurrentRecord To Me.RecordsetClone.RecordCount
OpenFormOrReport ("frm_P45_submission")
Do Until Not FormIsOpen("frm_P45_submission")
DoEvents: Sleep 100
Me.SetFocus
Loop
DoCmd.GoToRecord , , acNext
Next counter

This essentially opens "frm_P45_submission": this form then continuously
polls an external government computer, and once the communication is
acknowledged by the government computer, my PC makes a data submission to
it. Then the form is closed, the next record is selected on "frm main", and
the process is repeated. A number of tests need to be made before
"frm_P45_submission" is opened, and these are made as part of the
OpenFormOrReport procedure: if any of the tests fail "frm_P45_submission"
does not open - which is what I want - but the 'test failure message' is
constantly repeated - which is not what I want! It seems as if the code gets
stuck inside the loop, whereas, to my mind, by having "Do Until Not
FormIsOpen", the loop should be exited as soon as "frm_P45_submission" is
found not to be open.

Where have I gone wrong?

Hope soneone can help.
Many thanks
Leslie Isaacs
 
D

Dorian

Maybe I'm not understanding something but you have a loop which commences
based on a condition, then inside the loop there is nothing to change that
condition and so your loop once it starts will never exit.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 
J

John W. Vinson

Hello All

I have the following code behind a button on "frm main":

Dim counter As Integer
For counter = Me.CurrentRecord To Me.RecordsetClone.RecordCount
OpenFormOrReport ("frm_P45_submission")
Do Until Not FormIsOpen("frm_P45_submission")
DoEvents: Sleep 100
Me.SetFocus
Loop
DoCmd.GoToRecord , , acNext
Next counter

If the intention is to open frm_P45_Submission, and let your code pause until
it's closed... there's a far simpler solution! Open the form in Dialog mode,
and your code will stop until the form is either closed or its Visible
property is set to false:

DoCmd.OpenForm "frm_P45_submission", WindowMode:=acDialog

See the Help for the OpenForm method for more details.
 
D

Dale Fye

Does the OpenFormOrReport code run all of the same tests for each record on
your main form? If so, is there a particular reason for doing so numerous
times? If not, you could open the form, run the code, then loop through the
records on your main form from within the OpenFormOrReport code, or in some
code in the Load event of "frm_P45 submission".
 
L

Leslie Isaacs

Hello Dorian

Many thanks for your reply.
The onOpen event of "frm_P45_submission" includes (at the end) the command
to close "frm_P45_submission" (once a particular response has ben received
from the external government computer.
Hope that makes sense!

Thanks again
Les
 
L

Leslie Isaacs

Hello John

Many thanks for your reply.
The code pauses to allow the polling of the external government computer to
take place. I note you suggest I use
DoCmd.OpenForm "frm_P45_submission", WindowMode:=acDialog
.... but it is essential that the tests within the OpenFormOrReport function
are carried out, before "frm_P45_submission" is opened - and in fact these
same test are used when the form is opened elsewhere, hence the need for the
OpenFormOrReport function.
I am not familiar with Dialog mode, and will certainly read up un this -
many thanks for the suggestion.

Thanks again
Les
 
L

Leslie Isaacs

Hello Dale

The tests in the OpenFormOrReport code are only applied once for each record
on the main form, as the OpenFormOrReport function is outside the loop (I
think!)

Hope that makes sense.
Thanks again
Les
 
J

John W. Vinson

Hello John

Many thanks for your reply.
The code pauses to allow the polling of the external government computer to
take place. I note you suggest I use
DoCmd.OpenForm "frm_P45_submission", WindowMode:=acDialog
... but it is essential that the tests within the OpenFormOrReport function
are carried out, before "frm_P45_submission" is opened - and in fact these
same test are used when the form is opened elsewhere, hence the need for the
OpenFormOrReport function.

You may want to modify the code in OpenFormOrReport to include a (perhaps
optional?) additional argument to specify dialog mode.
I am not familiar with Dialog mode, and will certainly read up un this -
many thanks for the suggestion.

It's going to be a LOT simpler than a waste-time-and-check loop!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top