Solved

Visual basic 6.0 Looping

Posted on 2007-11-16
7
2,945 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

856 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