Solved

Visual basic 6.0 Looping

Posted on 2007-11-16
7
2,934 Views
Last Modified: 2013-11-26
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


   
   
0
Comment
Question by:apr2505
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 20300270
>For example, if one starts at 2, and counts to 14, with 3 numbers per line, it should look like this:
That would be...

For Counter = StartAt to StopAt Step 3  'or whatever variable 3 is.
  ' your code goes here
Next
0
 

Author Comment

by:apr2505
ID: 20300556
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
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 20300583
You can do it like this:

    Dim StartAt As Double
    Dim StopAt As Double
    Dim LineLen As Double
    Dim Counter As Double
    Dim Loops As Integer
    Dim sText As String
   
    StartAt = Val(txtStartAt.Text)
    StopAt = Val(txtStopAt.Text)
    LineLen = Val(txtLineLen.Text)
   
    picResults.Cls
       
    Counter = StartAt
    Loops = 1
 
    Do While Counter <= StopAt
           
        sText = sText & " " & Counter

        If (Loops Mod LineLen = 0) Then
            sText = sText & vbCrLf
        End If
       
        Counter = Counter + 1
        Loops = Loops + 1
    Loop
           
    picResults.Print sText

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 20300591
Use the For..Next loop instead of the Do..Loop

For Counter = StartAt To StopAt Step 3
   picResults.Print Counter
Next
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 20300882
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
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 20300927
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
0
 

Author Closing Comment

by:apr2505
ID: 31409644
Thank you so much I see where i went wrong.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now