Parsing a string problem

D

David G.

I'm inspecting/parsing a user input string, starting near the end and
working to the beginning.

code:
i = Len(strApplication(iRecords, 1))

While Asc(Mid(strApplication(iRecords, 1), i, 1)) >= 48 And
Asc(Mid(strApplication(iRecords, 1), i, 1)) <= 57
i = i - 1
Wend

end code

The problem occurs when the code does not find any non-numeric
characters; i eventually becomes "0" which causes the "Mid" function
to raise an error.

I added "And i > 0" to the "While" clause, but VBA tests all three
conditions.

Any suggestions?

Thanks!
THANKS!
David G.
THANKS!
David G.
 
D

Dirk Goldgar

"David G." wrote in message
I'm inspecting/parsing a user input string, starting near the end and
working to the beginning.

code:
i = Len(strApplication(iRecords, 1))

While Asc(Mid(strApplication(iRecords, 1), i, 1)) >= 48 And
Asc(Mid(strApplication(iRecords, 1), i, 1)) <= 57
i = i - 1
Wend

end code

The problem occurs when the code does not find any non-numeric characters;
i eventually becomes "0" which causes the "Mid" function to raise an
error.

I added "And i > 0" to the "While" clause, but VBA tests all three
conditions.

Any suggestions?

Here's an alternative way:

'------ start of code ------
Dim i As Long
Dim strWork As String

' Extract current array entry for greater efficiency.
strWork = strApplication(iRecords, 1)

For i = Len(strWork) To 1 Step -1
Select Case Asc(Mid$(strWork, i, 1))
Case 48 To 57
' Do nothing
Case Else
' not a numeric digit, so leave the loop
Exit For
End Select
Next i
'------ end of code ------
 
D

Daryl S

David -

You didn't tell us what you want to happen if the characters are not
numeric, so I don't know if you want to handle each one on the spot, or if
you want to break out of the loop. The code below will look at each
character in the string -
Try looping through all the characters, like this:

Do while i > 0
If Asc(Mid(strApplication(iRecords, 1), i, 1)) >= 48 And _
Asc(Mid(strApplication(iRecords, 1), i, 1)) <= 57 Then
'everything is OK here. do nothing and keep looping...
Else
'what to do if character is not numeric...
End If
i = i - 1
Loop

You can also use the isNumeric function if you just want to know if the
string can be evaluated as a number...
 
S

Sylvain Lafontaine

Contrary to the For and Do loops, you cannot break out of a While loop using
an Exit statement; so whenever you have a complex logical expression, you
should use either a For or a Do loop where you have a much better control on
the sequence of executed steps. There are many forms of Do loop, see
http://theopensourcery.com/vb05tut.htm ; in your case, you could write:

i = Len(strApplication(iRecords, 1))

Do While Asc(Mid(strApplication(iRecords, 1), i, 1)) >= 48 And
Asc(Mid(strApplication(iRecords, 1), i, 1)) <= 57
i = i - 1

if (I = 0) then Exit Do
Loop

You should also check for the condition of an empty string ("") before
entering your loop. This is why it's much better to simply look for the
variable i > 0 in the loop instruction and test for the more complex stuff
inside the loop like it's shown in the other answers to your post.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Blog/web site: http://coding-paparazzi.sylvainlafontaine.com
Independent consultant and remote programming for Access and SQL-Server
(French)


I'm inspecting/parsing a user input string, starting near the end and
working to the beginning.

code:
i = Len(strApplication(iRecords, 1))

While Asc(Mid(strApplication(iRecords, 1), i, 1)) >= 48 And
Asc(Mid(strApplication(iRecords, 1), i, 1)) <= 57
i = i - 1
Wend

end code

The problem occurs when the code does not find any non-numeric
characters; i eventually becomes "0" which causes the "Mid" function
to raise an error.

I added "And i > 0" to the "While" clause, but VBA tests all three
conditions.

Any suggestions?

Thanks!
THANKS!
David G.
THANKS!
David G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top