Solved

Loop Statement - Incrementing a String

Posted on 2001-06-21
5
168 Views
Last Modified: 2010-05-02
I wish to write a statement that allows me to add an increment to a string.
eg,
     repeat until i = 11
     i=i+1
     cat = "Number" + i
     

     so i get cat = "Number 1"
           cat = "Number 2"

Thanks
Dave Steers
0
Comment
Question by:dsteers
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6213901
For i = 1 to 11
  cat = "Number " & i
next

Cheers
0
 
LVL 3

Accepted Solution

by:
cmgarnett earned 50 total points
ID: 6213916
dim intX as integer
dim strCode as String

   For intX = 1 To 9999
      strCode = "Number " & Format(intX, "0000")
      debug.Print strCode
   Next

This gives you "Number 0001" to "Number 9999"
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6214027
Optional Code return same result:

Dim i As Integer
    i = 1
    List1.Clear
    Do Until i > 11
        List1.AddItem "Number " & i
        i = i + 1
    Loop
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6214032
As cmgarnett suggests format is a good function. It handles null values which you will discover the more time you spend with VB.  So if you get in the habit it the best to use.  You can also use CSTR() and STR()

Format(123,"0") produces "123" ' output controlled by format string
Cstr(123) Produces "123"
Str(123) Produces " 123" ' adds a leading space or minus.

angelIII suggests using "& I" which is fine because it will work in your circumstaces.  It works by converting the number to a variant first then a string.  But if you use the & operator instead of the + (concatination) in other circumstances you may get problems.  So it is better to get into a habit of using Format, CStr or STR whenever  you need to convert numeric to string.   Others may not share this view.

hope this helps, inthedark.
0
 

Author Comment

by:dsteers
ID: 6224380
cmgarnet,
Thanks for your help and quick response.

Regards
Dave Steers
0

Featured Post

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.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now