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
 
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
 
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