![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
i need help with my coding for some reason when i run it on VB it says "compile error: do without loop" i don't know how to run it without any errors. heres the code. For this i've got 3 txt boxes and 1 commmand box, can someone please help me fix this problem.
Dim size As Integer
Dim step As Integer
Dim row As Integer
Dim col As Integer
Dim count1 As Integer
Dim number As Integer
Dim array1(1 To 10, 1 To 10) As Integer
Dim newrow As Integer
Dim newcol As Integer
========================================
Private Sub cmdOK_Click()
Open "c:\temp\vbout01.txt" For Output As #1
Write #1,
row = 1
Write #1, "row", row
col = (size + 1) / 2
count1 = 1
Write #1, "count = ", number
number = start
Write #1, "number = ", number
Do
array1(row, col) = number
Write #1, "array"; (",row,"",col") = ",number"
number = number + step
Write #1, "number = ", number
count1 = count1 + 1
Write #1, "count =", count1
newrow = newrow - 1
Write #1, "newrow =", newrow
newcol = newcol - 1
Write #1, "newcol =", newcol
If newrow <= 1 Then
newrow = size
Write #1, "newcol =", newcol
End If
If array1(newrow, newcol) = Not Empty Then
newrow = row + 1
Write #1, "newrow=", newrow
newcol = col
Write #1, "newcol=", newcol
If newrow > size Then
newrow = 1
Write #1, "newrow=", newrow
End If
row = newrow
Write #1, "col=", col
Loop Until count1 > (size * size)
Close #1
End Sub
==============================
Private Sub sizebox_Change()
size = sizebox.Text
End Sub
=====================================
Private Sub startbox_Change()
start = startbox.Text
End Sub
=====================================
Private Sub stepbox_Change()
step = stepbox.Text
End Sub |
|
|
|
|
|
#2 |
|
Programmer
|
As far as I know, for a Do loop in VB you need the exit statement at the top, not at the bottom of the loop.
It should look like: Do Until [whatever your exit condition is] ... your code ... Loop
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
i've just tried that and now when i run it it says "Compile Error: Block if without end if" and highlight "End Sub".
|
|
|
|
|
|
#4 |
|
Programmer
|
Okay...
Looks like you're missing an "end if": If array1(newrow, newcol) = Not Empty Then newrow = row + 1 Write #1, "newrow=", newrow newcol = col Write #1, "newcol=", newcol End IF
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
And BTW, you don't need to put the Until statement at the top (though it would be better if you did, in this case). You just need to get rid of the Loop keyword:
Do
...
Until a = b |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
i've done what you said, but now when i run it it still has an error message it says "Compile Error: Do without Loop" and highlights "end sub".
If array1(newrow, newcol) = Not Empty Then newrow = row + 1 Write #1, "newrow=", newrow newcol = col Write #1, "newcol=", newcol End If If newrow > size Then newrow = 1 Write #1, "newrow=", newrow End If row = newrow Write #1, "col=", col Do Until count1 > (size * size) Close #1 End Sub |
|
|
|
|
|
#7 |
|
Programmer
|
okay...looks like you fixed the if statement but broke the do loop again...
You can either have a Do Until [exit condition]
...code...
LoopDo
...code...
Until [exit condition]If you have the Do Until/While [exit condition] at the top, you have to have the Loop keyword at the end to indicate where you want it to loop to. If you just have the Do keyword at the top, you need to put the Until/While [exit condition] at the bottom of where you want it to loop, for the same reason. In your code, you've got the Do Until at the top of the loop, but nothing to indicate to the compiler where you want it to actually loop from.
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
i've finally fixed that do loop, but now whenever i run the program it says "Run Time Error 9: Subscripts out of range" and highlights "If array1(newrow, newcol) = 0 Then" could someone please help
![]() |
|
|
|
|
|
#9 |
|
Programmer
|
That means you've exceeded the bounds of array1 (tried to put a value in an array element that is higher than the upper bound of your array).
If you used: "Dim array1(n) as Integer" then you're using a static array and can't use any array element higher than n+1 (array's start counting a 0). If you *want* to have a variable-sized array you need to declare it as a dynamic array and use "ReDim" or "ReDim Preserve" to change the size of it.
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|