PDA

View Full Version : Loop acting Wierd



Movian
05-29-2009, 02:27 PM
I have a table, it has a single record in it (Although it can have more so i need the loop to search through it)

I have the following loop

Do Until PatRS.Fields("ReportPhys") = CStr(InterpPhysRs.Fields("InterpPhys")) Or InterpPhysRs.EOF
InterpPhysRs.MoveNext
Loop
Now as there is one record i would expect it to either match or atempt to go to the next record hit EOF and exit the loop.

In either case it should exit the loop

however instead i get a no current record error .... its really annoying as it appears this loop now works Sporadically at best.

Need to get this resolved ASAP

~Edit here is some more info to put this loop in prespective

Dim sTempList As String
Dim mydb As DAO.Database
Dim PatRS As DAO.Recordset, RefPhysRS As DAO.Recordset, LedgerRs As DAO.Recordset, InterpPhysRs As DAO.Recordset

Set mydb = CurrentDb()
Set PatRS = mydb.OpenRecordset("tblPatient")
Set RefPhysRS = mydb.OpenRecordset("tblPhys")
Set LedgerRs = mydb.OpenRecordset("Ledger")
Set InterpPhysRs = mydb.OpenRecordset("tblIntPhys")
sTempList = ""

'Find Correct Records in each recordset (exept the Ledger Table as this can contain multiple applicable records)
While Not PatRS.EOF And Not CStr(MedID) = CStr(PatRS.Fields("MedicalID#"))
PatRS.MoveNext
Wend

While Not RefPhysRS.EOF And Not PatRS.Fields("RefPhys") = CStr(RefPhysRS.Fields("Referring Phys"))
RefPhysRS.MoveNext
Wend
Do Until PatRS.Fields("ReportPhys") = CStr(InterpPhysRs.Fields("InterpPhys")) Or InterpPhysRs.EOF
InterpPhysRs.MoveNext
Loop

Oorang
05-29-2009, 03:23 PM
VBA does not have short circuit evaluation (http://en.wikipedia.org/wiki/Short-circuit_evaluation) so when you are on the the EOF record you are still trying to get the value as part of your exit condition. Since it's EOF you get an error.
Try this instead:
Do Until InterpPhysRs.EOF
If PatRS.Fields("ReportPhys") = CStr(InterpPhysRs.Fields("InterpPhys")) Then
Exit Do
End if
InterpPhysRs.MoveNext
Loop