Solved

VB6 For Loop

Posted on 2007-03-22
6
38,706 Views
Last Modified: 2008-02-01
I have a small routeen that removes all characters from a string but not any digit 0 thru 9. It works fine, but I was wondering if my For Next loop could be modified to be cleaner. Here is my code:

'*********************************************

For i = 0 To 47
MyString = Replace(MyString, Chr(i), "")
Next i

For i = 58 To 127
MyString = Replace(MyString, Chr(i), "")
Next i

'********************************************************
0
Comment
Question by:AmericaFan
  • 3
  • 2
6 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 18773570
For i = 0 To 47
  MyString = Replace(MyString, Chr(i), "")
Next i

1. Do you really need to concern yourself with characters less than 32?

2. Is cleaner really your problem, or are you experiencing performance problems?  Your two loops look pretty clean.  You combine these two loops, but wouldn't gain any simplicity or cleanliness.

3. How long are your MyString strings?
0
 

Author Comment

by:AmericaFan
ID: 18773708
My main goal was to see if I could combine the two For Loops into one For Loop and get the same results. I only want 0-9 and any other character has to be removed. I'm validating and any key is possible. Maybe not 0 thru 31, but the rest is not wanted and is on a key board for mistakes. Thanks for you comment aikimark.
0
 
LVL 10

Accepted Solution

by:
Clif earned 50 total points
ID: 18773750
For i = 58 To 127
    MyString = Replace(MyString, Chr(i), "")
    If (i - 58) <= 47 Then
        MyString = Replace(MyString, Chr(i - 58), "")
    End If
Next i
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Clif
ID: 18773997
You can also use regular expressions, which is even more efficient.

You would have to add a reference to "Microsoft VBScript Regular Expressions 5.5" in your project (it's been available since IE 5.5)

0
 

Author Comment

by:AmericaFan
ID: 18774165
Thats what I'm talkin about :) Thanks Clif!
0
 
LVL 10

Expert Comment

by:Clif
ID: 18774253
NP
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

730 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