Solved

Visual basic 6.0 Looping

Posted on 2007-11-16
7
2,939 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
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…
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…

912 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

22 Experts available now in Live!

Get 1:1 Help Now