troubles with do while loop

G

Greg

I am trying to loop through some code to strip out parts of a string that
are in a comma delimited format. I haven't been able to force the order of
evaluation of the Do While statement to give my desired result of looping
until there is no longer a comma delimiter contained in the string.

Here is the code that I am trying to use.
Do While Not (varPos = InStr(StrToParse, ",")) = 0

' Work with first date code

' copy first date code without white spaces

strDateCode = Trim(Left(StrToParse, varPos - 1))

Debug.Print strDateCode;

' strip first date code from the string

StrToParse = Right(StrToParse, Len(StrToParse) - varPos)

Loop

If I put the varPos = InStr(StrToParse, ",") on it's own line it evaluates
to 7 when StrToParse = "X2608 , X3208,X1209" . When I include this in the Do
while not statement below, I can't seem to get the varPos =
InStr(StrToParse, ",") to evaluate to anything but zero no matter how I
place parenthesis.

There may be another way to accomplish the loop (suggestions are welcome),
but I would really like to correct / clarify my understanding of the do
while not loop structure so that I don't need help in the future.

Thank you in advance for any help.

Greg
 
J

June7 via AccessMonster.com

Do you want final result to be the original string just without the commas
but still one continuous string? Is the number of substrings always known,
how many?

Think you need to:
1. Use different condition for the loop, such as length of string
2. Handle the last substring which does not have a comma following it.
3. Your example shows a space before a comma following the first string. Is
this expected? Is Trim function to handle that?
4. Reset the varPos back to zero right after the StrToParse =

Do While Len(StrToParse) > 0
varPos = InStr(StrToParse, ",")
strDateCode = Iif(varPos = 0, StrToParse, Trim(Left(StrToParse, varPos -
1)))
Debug.Print strDateCode;
' strip first date code from the string
StrToParse = Iif(varPos = 0, "", Mid(StrToParse, varPos + 1))
varPos = 0
Loop

Check out the Split function. It will parse a string and populate a one-
dimensional array. Then you can address whichever element of the array you
want.
 
P

PieterLinden via AccessMonster.com

Greg said:
I am trying to loop through some code to strip out parts of a string that
are in a comma delimited format. I haven't been able to force the order of
evaluation of the Do While statement to give my desired result of looping
until there is no longer a comma delimiter contained in the string.

Here is the code that I am trying to use.
Do While Not (varPos = InStr(StrToParse, ",")) = 0

' Work with first date code

' copy first date code without white spaces

strDateCode = Trim(Left(StrToParse, varPos - 1))

Debug.Print strDateCode;

' strip first date code from the string

StrToParse = Right(StrToParse, Len(StrToParse) - varPos)

Loop

If I put the varPos = InStr(StrToParse, ",") on it's own line it evaluates
to 7 when StrToParse = "X2608 , X3208,X1209" . When I include this in the Do
while not statement below, I can't seem to get the varPos =
InStr(StrToParse, ",") to evaluate to anything but zero no matter how I
place parenthesis.

There may be another way to accomplish the loop (suggestions are welcome),
but I would really like to correct / clarify my understanding of the do
while not loop structure so that I don't need help in the future.

Thank you in advance for any help.

Greg

I would definitely go for the Split option. MUCH easier. Split returns an
array of values, which you can easily walk by doing something like this:

For intPosition = LBound(varArray) to UBound(varArray)
debug.print varArray(intPosition)
next intPosition

if your function returns a Variant data type, you can just pass that array on
to something else.
 
G

Greg

Oh boy, I'm terribly sorry, but I forgot rule #1 of posting. I am stuck
with Access 97 due to a problem with ODBC driver compatability. The split
function is exactly what I am looking for to solve my actual problem.

I am still wondering if anyone can tell me why my code is not working as I
expect. When I use varPos = InStr() on it's own line, it works. I
incorrectly assumed that in the code Do While Not (varPos = InStr())= 0, it
would first evaluate the varPos = InStr() as it did when on its own line.
It does not, and I don't understand why not.

I have working code now, but would like to know what the problem with my
first code block is.

Thanks for the help.

Greg
 
J

June7 via AccessMonster.com

One problem I see with the original code is that the final substring would
not be captured because it is not followed by a comma. Also, apparently
trying to set the varPos value in the While statement doesn't work so varPos
never gets reset from 0. Did you step debug? Do that and see what the value
of varPos is immediately after the While line is executed.
Oh boy, I'm terribly sorry, but I forgot rule #1 of posting. I am stuck
with Access 97 due to a problem with ODBC driver compatability. The split
function is exactly what I am looking for to solve my actual problem.

I am still wondering if anyone can tell me why my code is not working as I
expect. When I use varPos = InStr() on it's own line, it works. I
incorrectly assumed that in the code Do While Not (varPos = InStr())= 0, it
would first evaluate the varPos = InStr() as it did when on its own line.
It does not, and I don't understand why not.

I have working code now, but would like to know what the problem with my
first code block is.

Thanks for the help.

Greg
[quoted text clipped - 46 lines]
on
to something else.
 
S

Steve Sanford

Greg,

This is what I think is happening:

When you evaluate the condition, there is an "order of operations" (aka
"order of precedence") that must be followed.

Starting from the inner most parentheses, you evaluate the InStr() function.
In your example, it is 7. Instead of setting varPos to 7, because of the next
set of parentheses, you are doing a comparison of 7 (result of the INStr())
to the variable "varPos". But it appears that "varPos" is NULL. Is 7 equal to
NULL?? No.

So now you have (first time thru the loop)

Not (varPos = InStr(StrToParse, ",")) = 0

Not (varPos = 7) = 0

Not ( NULL = 7) = 0

Not (FALSE) = 0

True = 0

But, in a comparison, zero (0) means FALSE.

Which results in

TRUE = FALSE


which is false, so the "Do While" loop ends.



Here is the code I used: (try stepping thru this)

'-------------------------------
Public Sub try()
Dim StrToParse As String
Dim varPos As Integer
Dim bl As Boolean

StrToParse = "X2608 , X3208 X1209"

MsgBox (varPos = InStr(StrToParse, ","))

bl = Not (varPos = InStr(StrToParse, ",")) = 0

End Sub
'--------------------------------


In any case, I would never change a variable in the condition clause of a
loop. (I think that is a rule, but I could be wrong)



HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Greg said:
Oh boy, I'm terribly sorry, but I forgot rule #1 of posting. I am stuck
with Access 97 due to a problem with ODBC driver compatability. The split
function is exactly what I am looking for to solve my actual problem.

I am still wondering if anyone can tell me why my code is not working as I
expect. When I use varPos = InStr() on it's own line, it works. I
incorrectly assumed that in the code Do While Not (varPos = InStr())= 0, it
would first evaluate the varPos = InStr() as it did when on its own line.
It does not, and I don't understand why not.

I have working code now, but would like to know what the problem with my
first code block is.

Thanks for the help.

Greg
 

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