Loops, Loops, Loops
July 6, 2006 6:29 PM Subscribe
Alright Visual Basic Gurus: Do your worst! Why Can't I Loop? (VB6)
This code makes sense to me, but when I run it, it says "Loop Without Do". Any suggestions on how to fix?
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
This code makes sense to me, but when I run it, it says "Loop Without Do". Any suggestions on how to fix?
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
To clarify, every Do loop must begin with "Do" and end with "Loop".
Here's an article on the syntax.
posted by null terminated at 6:36 PM on July 6, 2006
Here's an article on the syntax.
posted by null terminated at 6:36 PM on July 6, 2006
Its been a long time since I've done any serious VB. Can you really have multiple "loop"s with only one "do"?
In any other language I'm familiar with, I think you'd need to match each of your loop-equivalents with a corresponding do-equivalent. Here you've got only one "do" and three "loops". If the loops are meant to be nested you may have to deploy a couple more dos appropriately.
posted by hwestiii at 6:38 PM on July 6, 2006
In any other language I'm familiar with, I think you'd need to match each of your loop-equivalents with a corresponding do-equivalent. Here you've got only one "do" and three "loops". If the loops are meant to be nested you may have to deploy a couple more dos appropriately.
posted by hwestiii at 6:38 PM on July 6, 2006
For your "Loop"s inside the if statements, are you trying to move to the next iteration of the loop? If so, try using "Continue Do" instead of "Loop"
posted by null terminated at 6:39 PM on July 6, 2006
posted by null terminated at 6:39 PM on July 6, 2006
Response by poster: Yes, I'm trying to go to the next iteration of the loop, without reaching the other conditions. Would "else if" do the trick?
posted by fvox13 at 6:46 PM on July 6, 2006
posted by fvox13 at 6:46 PM on July 6, 2006
Best answer: Yes, or you can change the two "Loop" statements inside your if statements to "Continue Do".
posted by null terminated at 6:49 PM on July 6, 2006
posted by null terminated at 6:49 PM on July 6, 2006
you can only have one entry and one exit point for a loop. Use only one "Loop" statement. Make your if..then logic fit inside the loop.
posted by blue_beetle at 6:49 PM on July 6, 2006
posted by blue_beetle at 6:49 PM on July 6, 2006
Best answer: Sounds like you should be using "continue" as suggested above. See here for "continue" syntax discussion.
posted by hwestiii at 6:53 PM on July 6, 2006
posted by hwestiii at 6:53 PM on July 6, 2006
Best answer: I don't have VB on my machine, but something like this should work. (may need some syntax fixes).
posted by blue_beetle at 7:08 PM on July 6, 2006
Dim FirstPaymentDate As DateDim CurrRowValue As Singlefor CurrentRowVal = 1 to MSHFlexGrid2.Rows-1 MSHFlexGrid2.Row = CurrRowVal If MSHFlexGrid2.TextMatrix(CurrRowVal, 2) = Date Then MSHFlexGrid1.AddItem (MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab & "First Bill" else if MSHFlexGrid2.TextMatrix(CurrRowVal, 3) = Date Then MSHFlexGrid1.AddItem (MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab & "Last Bill" else FirstPaymentDate = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 2) if Day(FirstPaymentDate) = Day(Date) and DateDiff(m,Date,FirstPaymentDate)<10 then MSHFlexGrid1.AddItem (MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)) & vbTab & "Interval Bill" end if end ifnext CurrentRowVal
posted by blue_beetle at 7:08 PM on July 6, 2006
This thread is closed to new comments.
posted by null terminated at 6:34 PM on July 6, 2006