Solved

Loop Statement - Incrementing a String

Posted on 2001-06-21
5
170 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 50

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

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.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

786 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