Solved

Loop Statement - Incrementing a String

Posted on 2001-06-21
5
172 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 143

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 51

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

828 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