Solved

Visual basic 6.0 Looping

Posted on 2007-11-16
7
2,941 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 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