Really stupid looping problem

S

Steven Smith

Hi everyone,

This code makes sense to me, but when I run it, it says "Loop Without Do".
Any Suggestions?

Thanks,
Steven Smith


Dim FirstPaymentDate As Date
Dim CurrRowValue As Single
Dim NumStudentsInFlex2 As Single
NumStudentsInFlex2 = MSHFlexGrid2.Rows
CurrRowVal = 1
Do While CurrRowVal < NumStudentsInFlex2 'While counter is less than number of rows in all students grid
YesDate = 0
MSHFlexGrid2.Row = CurrRowVal
'MsgBox CurrRowVal & "(" & MSHFlexGrid2.Row & ")" & " / " & MSHFlexGrid2.Rows & " " & MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)
If MSHFlexGrid2.TextMatrix(CurrRowVal, 2) = Date Then
MSHFlexGrid1.AddItem (MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab & "First Bill"
CurrRowVal = CurrRowVal + 1
Loop
End If
If MSHFlexGrid2.TextMatrix(CurrRowVal, 3) = Date Then
MSHFlexGrid1.AddItem (MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab & "Last Bill"
CurrRowVal = CurrRowVal + 1
Loop
End If
FirstPaymentDate = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 2)
YesDate = 0
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 1, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 2, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 3, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 4, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 5, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 6, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 7, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 8, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If Format(Date, "MM/DD/YYYY") = Format(DateAdd("m", 9, FirstPaymentDate), "MM/DD/YYYY") Then YesDate = 1
If YesDate = 1 Then MSHFlexGrid1.AddItem (MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab & "Interval Bill"
CurrRowVal = CurrRowVal + 1
Loop
 
S

Scott M.

The "Loop" keyword can not be in a conditional statement, like an IF. If
you are using 2005, use the continue statement.
 
J

Jan Hyde

Steven Smith <[email protected]>'s wild thoughts were
released on Fri, 07 Jul 2006 01:27:19 GMT bearing the
following fruit:
Hi everyone,

This code makes sense to me, but when I run it, it says "Loop Without Do".
Any Suggestions?

Thanks,
Steven Smith
I'm guessing the following is what your trying to achieve.

Dim FirstPaymentDate As Date
Dim CurrRowValue As Single
Dim NumStudentsInFlex2 As Single

NumStudentsInFlex2 = MSHFlexGrid2.Rows
CurrRowVal = 1
Do While CurrRowVal < NumStudentsInFlex2 'While counter
is less than number of rows in all students grid

YesDate = 0
MSHFlexGrid2.Row = CurrRowVal
If MSHFlexGrid2.TextMatrix(CurrRowVal, 2) = Date Then
MSHFlexGrid1.AddItem
(MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab &
"First Bill"
CurrRowVal = CurrRowVal + 1
ElseIf MSHFlexGrid2.TextMatrix(CurrRowVal, 3) = Date
Then
MSHFlexGrid1.AddItem
(MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab &
"Last Bill"
CurrRowVal = CurrRowVal + 1
Else
FirstPaymentDate =
MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 2)
YesDate = 0
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 1, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 2, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 3, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 4, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 5, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 6, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 7, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 8, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If Format(Date, "MM/DD/YYYY") =
Format(DateAdd("m", 9, FirstPaymentDate), "MM/DD/YYYY") Then
YesDate = 1
If YesDate = 1 Then MSHFlexGrid1.AddItem
(MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab &
"Interval Bill"
CurrRowVal = CurrRowVal + 1
End If
Loop


Jan Hyde (VB MVP)
 
S

Scott M.

Or better yet:

Dim FirstPaymentDate As Date
Dim CurrRowValue, NumStudentsInFlex2 As Single
NumStudentsInFlex2 = MSHFlexGrid2.Rows
CurrRowVal = 1
Dim BillType As String ' <--- This is what you are really trying to
figure out after all, right?

Do While CurrRowVal < NumStudentsInFlex2 'While counter is less than # of
rows in all students grid
MSHFlexGrid2.Row = CurrRowVal

If MSHFlexGrid2.TextMatrix(CurrRowVal, 2) = Date Then
BillType="First Bill"
ElseIf MSHFlexGrid2.TextMatrix(CurrRowVal, 3) = Date Then
BillType="Last Bill"
Else
FirstPaymentDate = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 2)

Dim x As Integer = 1
For x = 1 To 9
'You don't have to format a date to work with the month
portion of it
If Date = DateAdd("m", x, FirstPaymentDate) Then
BillType="Interval Bill"
Next
End If

'Instead of writing the same line of code 3 times with one small
difference each time,
'just write it once and work on the real problem, which is figuring
the bill type.
MSHFlexGrid1.AddItem (MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0))
& vbTab & BillType

'In a While loop, you want to be careful to only increase the
counter when absolutely necessary,
'so, let's only do it here
CurrRowVal = CurrRowVal + 1
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