apr2505
asked on
Visual basic 6.0 Looping
I have a code for the problem below however I can't get the numbers per line command to work. If I put 3 it doesn't work or put in 2 it doesn't work etc. If someone can assist.
PROBLEM:
The form contains three boxes, labelled Start at:, Stop at:, and Number on a line:. When the user clicks the button, you should print out all the numbers from the start value to the stop value, printing only number values on a given line.
For example, if one starts at 2, and counts to 14, with 3 numbers per line, it should look like this:
2 3 4
5 6 7
8 9 10
11 12 13
14
MY CODE:
Private Sub cmdCount_Click()
Dim StartAt As Double
Dim StopAt As Double
Dim LineLen As Double
Dim Counter As Double
StartAt = Val(txtStartAt.Text)
StopAt = Val(txtStopAt.Text)
LineLen = Val(txtLineLen.Text)
picResults.Cls
Counter = StartAt
Do While Counter <= StopAt
picResults.Print Counter
Counter = Counter + 1
Loop
End Sub
PROBLEM:
The form contains three boxes, labelled Start at:, Stop at:, and Number on a line:. When the user clicks the button, you should print out all the numbers from the start value to the stop value, printing only number values on a given line.
For example, if one starts at 2, and counts to 14, with 3 numbers per line, it should look like this:
2 3 4
5 6 7
8 9 10
11 12 13
14
MY CODE:
Private Sub cmdCount_Click()
Dim StartAt As Double
Dim StopAt As Double
Dim LineLen As Double
Dim Counter As Double
StartAt = Val(txtStartAt.Text)
StopAt = Val(txtStopAt.Text)
LineLen = Val(txtLineLen.Text)
picResults.Cls
Counter = StartAt
Do While Counter <= StopAt
picResults.Print Counter
Counter = Counter + 1
Loop
End Sub
ASKER
I've added that part and the results remain the same it runs straight down instead of 3 numbers per line. Here is what it looks like after adding For Counter = StartAt to StopAt Step 3 maybe I put it in the wrong spot.
Private Sub cmdCount_Click()
Dim StartAt As Double
Dim StopAt As Double
Dim LineLen As Double
Dim Counter As Double
StartAt = Val(txtStartAt.Text)
StopAt = Val(txtStopAt.Text)
LineLen = Val(txtLineLen.Text)
picResults.Cls
For Counter = StartAt To StopAt Step 3
Do While Counter < StopAt
picResults.Print Counter
Counter = Counter + 1
Loop
Next
End Sub
Private Sub cmdCount_Click()
Dim StartAt As Double
Dim StopAt As Double
Dim LineLen As Double
Dim Counter As Double
StartAt = Val(txtStartAt.Text)
StopAt = Val(txtStopAt.Text)
LineLen = Val(txtLineLen.Text)
picResults.Cls
For Counter = StartAt To StopAt Step 3
Do While Counter < StopAt
picResults.Print Counter
Counter = Counter + 1
Loop
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use the For..Next loop instead of the Do..Loop
For Counter = StartAt To StopAt Step 3
picResults.Print Counter
Next
For Counter = StartAt To StopAt Step 3
picResults.Print Counter
Next
Private Sub cmdCount_Click()
Dim StartAt As Double
Dim StopAt As Double
Dim LineLen As Double
Dim Counter As Double
StartAt = Val(txtStartAt.Text)
StopAt = Val(txtStopAt.Text)
LineLen = Val(txtLineLen.Text)
picResults.Cls
For Counter = StartAt To StopAt Step 3
picResults.Print Counter & vbtab & counter + 1 & vbtab & counter + 2
Next
End Sub
Regards
Chris
Dim StartAt As Double
Dim StopAt As Double
Dim LineLen As Double
Dim Counter As Double
StartAt = Val(txtStartAt.Text)
StopAt = Val(txtStopAt.Text)
LineLen = Val(txtLineLen.Text)
picResults.Cls
For Counter = StartAt To StopAt Step 3
picResults.Print Counter & vbtab & counter + 1 & vbtab & counter + 2
Next
End Sub
Regards
Chris
To ensure a stop at the correct point and a change to replace hard value 3 with linelen:
Private Sub cmdCount_Click()
Dim StartAt As Double
Dim StopAt As Double
Dim LineLen As Double
Dim Counter As Double
StartAt = Val(txtStartAt.Text)
StopAt = Val(txtStopAt.Text)
LineLen = Val(txtLineLen.Text)
picResults.Cls
For Counter = StartAt To StopAt Step linelen
picResults.Print Counter & iif(counter +1 <= stopat,vbtab & counter + 1,"") & iif(counter+2 <= stopat, vbtab & counter + 2,"")
Next
End Sub
regards
Chris
Private Sub cmdCount_Click()
Dim StartAt As Double
Dim StopAt As Double
Dim LineLen As Double
Dim Counter As Double
StartAt = Val(txtStartAt.Text)
StopAt = Val(txtStopAt.Text)
LineLen = Val(txtLineLen.Text)
picResults.Cls
For Counter = StartAt To StopAt Step linelen
picResults.Print Counter & iif(counter +1 <= stopat,vbtab & counter + 1,"") & iif(counter+2 <= stopat, vbtab & counter + 2,"")
Next
End Sub
regards
Chris
ASKER
Thank you so much I see where i went wrong.
That would be...
For Counter = StartAt to StopAt Step 3 'or whatever variable 3 is.
' your code goes here
Next