Can't out of DO...Loop! Help please.

  • Thread starter Thread starter TA
  • Start date Start date
T

TA

Hi ,
I have a piece of code here I don't know where is wrong
but it can not exit Do while loop, please help, Thank you

If Rs_Cum.RecordCount > 0 Then
MsgBox "Do you want to run the Error Checking
Report ?", vbOKCancel
If vbOKCancel = 2 Then
DoCmd.Close
DoCmd.OpenForm "FrmMainMenu"
ElseIf vbOKCancel = 1 Then
Rs_Cum.MoveFirst
Do Until Rs_Cum.EOF
tempID = Trim(Rs_Cum.Fields("ident").Value)
Rs_Cum.MoveNext
If (Rs_Cum.Fields("ident").Value) = tempID Then
Rs3.AddNew
Rs3!Ident = tempID
Rs3!batch = Rs_Cum.Fields("batch").Value
Rs3!seqnum = Rs_Cum.Fields("seqnum").Value
Rs3!Code = "100"
Rs3.Update
Rs_Cum.MovePrevious
Else
Rs_Cum.MovePrevious
End If
Rs_Cum.MoveNext
'MsgBox "end of if and update" OK here
Loop
'MsgBox "Out of loop" NOT OK here
End If ' of vbOKCancel
End If ' of Rs_cum >0
Docmd.close
Docmd.Openform "FrmMainMenu"

It can not close this form and can not open a new form
call FrmMainMenu. Please help Thank you for any suggestion.
TA
 
If this code is within the form module then you need to open the main form
before you close the form that the code is written

i.e. Docmd.openform "FrmMainMenu"
docmd.close acform, "otherform"

NOT Docmd.close
Docmd.openform "FrmMainMenu"


HTH

Al
 
Yeah you right, but the prob. is not here. It ran until
EOF then stop right there and do not thing. I have tried
your ideal but still...?
Thanks for fast reply.
Best Regards,
TA
 
TA,

I think that the following lines are moving you back to the
previous record just before you MoveNext, in effect keeping
you on the same record for the next loop. I am not sure why
they are there, but I don't think they should be.

Rs_Cum.MovePrevious
Else
Rs_Cum.MovePrevious
End If
Rs_Cum.MoveNext
--

Gary Miller
Gary Miller Computer Services
Sisters, OR
________________________
 
Well, it certainly won't until you do, even if you have more
problems than that. Take them out and then see what happens
afterward.

--

Gary Miller
Gary Miller Computer Services
Sisters, OR
________________________
 
Review the Help file for AddNew "The record that was current BEFORE you use
AddNew remains the current record.". Also note that WHERE the new record is
added depends on the type of recordset it is and its indexes. Don't assume
the records will be added to the end of the recordset.

if the statement
(Rs_Cum.Fields("ident").Value) = tempID
is true for both of the last 2 records and if records are being added to the
end of the recordset, you will be adding records forever and never get to
the "last" record because you are continuosly adding it.

If you *did* ever get to the last record then
.MoveNext
If (Rs_Cum.Fields("ident").Value) = tempID
should generate an error because MoveNext has just made EOF = True (meaning
there is no current record) and then you try to get the ident value from
that non-existent record. (You don't have an "On Error Resume Next"
statement lurking atound that you didn't tell us about, do you?)
So I figure the only reason you get an endless loop rather than an error is
because the If statement is true for BOTH the last and next-to-last records
and it will continue to be true for each record you add.

A structure like the following may work better for you to avoid the error on
the last record, but it doesn't address the endless loop/endless copying
issue.

Rs_Cum.MoveFirst
'Note: tempID = "" by default, which is OK for the 1st record.
Do Until Rs_Cum.EOF
If (Rs_Cum.Fields("ident").Value) = tempID Then
Rs3.AddNew
Rs3!Ident = tempID
Rs3!batch = Rs_Cum.Fields("batch").Value
Rs3!seqnum = Rs_Cum.Fields("seqnum").Value
Rs3!Code = "100"
Rs3.Update
End If
tempID = Trim(Rs_Cum.Fields("ident").Value)
Rs_Cum.MoveNext
Loop

Hope this helps,
--
George Nicholson

Remove 'Junk' from return address.
 
Hi Gary,
So, I removed these line but ...the problem is still.?
I keep testing now.
Best Regards,
TA
 
Hi George,
I want to looking in Rs_cum if there is a duplicate IDENT
then insert into Rs3. That why I do a loop there.
First go top recordset then
So, what is your recomendation?
Best
 
TA,

Why don't you post the latest version of what you have so we
can take a fresh look after the changes?

--

Gary Miller
Gary Miller Computer Services
Sisters, OR
________________________
 
I apologize. For some reason I didn't see that you were working with 2
recordsets. It is quite clear in what you provided but I overlooked it.
Therefore, please forget everything I said about endless copying.

However, my structure recommendation remains the same in order to avoid
getting an error when you reach the last record. But I now have no
explaination as to why you aren't reaching the last record.

In order to track down what is happening you may have to step through the
code and see exactly what it is doing (but address the structure issue
first). Maybe set up a temporary RS_Cum record counter for which you can
set up a Watch? Increment the counter every time you MoveNext, then try to
figure out why it either stops advancing or increases beyond the
rs_cum.Recordcount.

Hope this helps,
 
Back
Top