santastefano
Programmer
I have a loop that performs three functions in each loop. I want it to stop when it reaches a variable integer but it continues to the end of the loop and so (in this example) adds two steps before completing. I have tried initiating the variable -1 and this results in one short of the required steps.
In this code I have left out the allocation of variables and a couple of subsidiary steps to reduce the post but all variables set correctly (at the end of the final loop the variable is -2), in the real code all If and End If match OK and posting to the table is OK.
Any solution/suggestions gratefully received.
In this code I have left out the allocation of variables and a couple of subsidiary steps to reduce the post but all variables set correctly (at the end of the final loop the variable is -2), in the real code all If and End If match OK and posting to the table is OK.
Code:
Private Sub Time6_Exit(Cancel As Integer)
If varSessionsPerWeek = 3 Then
Do While varSessionsTotal > 0
With rsTimetable
.AddNew
!Code = varCode
!StartDate = varDate1
!From = varTime1
!To = varTime2
.Update
End With
varSessionsTotal = varSessionsTotal - 1
varDate1 = varDate1 + 7
Dim rsHolidays As DAO.Recordset
Set db = CurrentDb
Set rsHolidays = db.OpenRecordset("tblHolidays", dbopendynaset)
With rsHolidays
.FindFirst "Close1 = #" & Format(varDate1, "yyyy-mm-dd") & "#"
Do While Not .NoMatch
varDate1 = varDate1 + 7
.FindNext "Close1 = #" & Format(varDate1, "yyyy-mm-dd") & "#"
Loop
End With
rsHolidays.Close
Set rsHolidays = Nothing nothing.
With rsTimetable
.AddNew
!Code = varCode
!StartDate = varDate2
!From = varTime3
!To = varTime4
.Update
End With
varSessionsTotal = varSessionsTotal - 1
varDate2 = varDate2 + 7
Set db = CurrentDb
Set rsHolidays = db.OpenRecordset("tblHolidays", dbopendynaset)
With rsHolidays
.FindFirst "Close1 = #" & Format(varDate2, "yyyy-mm-dd") & "#"
Do While Not .NoMatch
varDate2 = varDate2 + 7
.FindNext "Close1 = #" & Format(varDate2, "yyyy-mm-dd") & "#"
Loop
End With
rsHolidays.Close
Set rsHolidays = Nothing
With rsTimetable
.AddNew
!Code = varCode
!StartDate = varDate3
!From = varTime5
!To = varTime6
.Update
End With
varSessionsTotal = varSessionsTotal - 1
varDate3 = varDate3 + 7
Set db = CurrentDb
Set rsHolidays = db.OpenRecordset("tblHolidays", dbopendynaset)
With rsHolidays
.FindFirst "Close1 = #" & Format(varDate3, "yyyy-mm-dd") & "#"
Do While Not .NoMatch
varDate3 = varDate3 + 7
.FindNext "Close1 = #" & Format(varDate3, "yyyy-mm-dd") & "#"
Loop
End With
rsHolidays.Close
Set rsHolidays = Nothing
Loop
rsTimetable.Close
Set rsTimetable = Nothing
Set db = Nothing
DoCmd.GoToControl "Continue"
Else
DoCmd.GoToControl "Date4"
End If
End Sub