Do While ignores .eof

  • Thread starter Thread starter marc_donofrio
  • Start date Start date
M

marc_donofrio

Hi

I am trying to get a do while loop to enter data in the relevant txt
boxes - if there are two bookings on a day then to put the details in
one text box - then move on to the next box, fill in the detaisl for
that day etc.

The problem is that when the rs hits end of file it crashes instead of
exiting this code and moving on to the next.

I hope some one can see where i am going wrong.

If Not rsBooking.EOF = True Then

For DayNum = 1 To 7

If rsBooking!Date = (ReneDate + (DayNum - 1)) Then

Do While rsBooking!Date = (ReneDate + (DayNum - 1)) And Not
rsBooking.EOF

Forms![Calendar]("txtRow" & RowNum & DayNum) =
Forms![Calendar]("txtRow" & RowNum & DayNum) &
Left(rsBooking![StartTime], 5) & "-" & Left(rsBooking![EndTime], 5) &
vbCrLf & rsBooking![EventTitle] & vbCrLf
Forms![Calendar]("txtRow" & RowNum & DayNum & "ID") =
rsBooking![RoomID]
Forms![Calendar]("txtRow" & RowNum & DayNum & "D") = rsBooking![Date]
rsBooking.MoveNext
Loop

End If

If rsBooking.EOF = True Then
Exit For
End If

Next DayNum
End If
 
Hi

I am trying to get a do while loop to enter data in the relevant txt
boxes - if there are two bookings on a day then to put the details in
one text box - then move on to the next box, fill in the detaisl for
that day etc.

The problem is that when the rs hits end of file it crashes instead of
exiting this code and moving on to the next.

I hope some one can see where i am going wrong.

If Not rsBooking.EOF = True Then

For DayNum = 1 To 7

If rsBooking!Date = (ReneDate + (DayNum - 1)) Then

Do While rsBooking!Date = (ReneDate + (DayNum - 1)) And Not
rsBooking.EOF

Forms![Calendar]("txtRow" & RowNum & DayNum) =
Forms![Calendar]("txtRow" & RowNum & DayNum) &
Left(rsBooking![StartTime], 5) & "-" & Left(rsBooking![EndTime], 5) &
vbCrLf & rsBooking![EventTitle] & vbCrLf
Forms![Calendar]("txtRow" & RowNum & DayNum & "ID") =
rsBooking![RoomID]
Forms![Calendar]("txtRow" & RowNum & DayNum & "D") = rsBooking![Date]
rsBooking.MoveNext
Loop

End If

If rsBooking.EOF = True Then
Exit For
End If

Next DayNum
End If


This might be because your "Do While" test accesses a record
(rsBooking!Date) when you could potentially be at the end of file.
I believe VBA tests the first condition first so when you are at EOF, it
tries to access a record (rsBooking!Date) for the first test before it
notices that there is no current record (it is at EOF).

Try putting the first condition in an If/Then block and make the main loop
test for EOF only.

While Not rsBooking.EOF
If rsBooking!Date = ReneDate + (DayNum - 1) Then
Forms![Calendar]("txtRow" & RowNum & DayNum) = ...
 

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

Back
Top