Solved

Visual basic 6.0 Looping

Posted on 2007-11-16
7
2,947 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 66

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 66

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

696 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