I have a 'do..while' section of code where it loops through lines in a text file, counts them, and when the line = "" the loop is supposed to stop and report the number of lines it counted. This all works fine in debugging mode... but when I compile it and try to run it from a program it doesn't stop when the line = "" it keeps going... it doesn't even stop at EOF.

Oddly enough... when I popped some msgbox's in there to see why it wasn't stopping when the line = "" it worked perfectly!

Anyone have ANY idea what could be going on? Here is the code, you'll notice the test msgbox I had put in:

Open filename For Input As #1
        Do Until EOF(1) 'until end of file
            Input #1, strLineValue 'read each line of text file, assign value

            If strLineValue = "Optimal Parameter Set" Then
                Input #1, strLineValue
                Input #1, strLineValue

                'figure out how many ARCAEM parameters there are
                Do While strLineValue <> ""
                    arrCellValue = Split(strLineValue)
                    If arrCellValue(0) <> "ARCAEM_BG_K" Then
                        param = param + 1
                    Else
                        BGK = True
                    End If
                    Input #1, strLineValue
                    MsgBox strLineValue & ": " & param
                Loop
            End If
        Loop
    Close #1

Your code will fail when your data file has no empty lines. Consider changing your second DO statement to this:

Do While strLineValue <> "" And Not EOF(1)

I think it would be a string type conversion (ASCII - Unicode thing) I use

trim(cstr(strLineValue)) <> ""

to make sure the values are evenly analysed.

Greetings

Thanks for the input. I know that the text file will, and always will, be in the same format which includes "" lines, so there was no need to change the condition statement.

I played around with it yesterday and tried to come up with my own work-around. I concluded that since it always works when there was a msgbox there, it basically works when it has a slight 'pause.' So I put a .1 second pause in there, and now it works!

But I'm still baffled as to why it works... and was that the only way to fix it? (I'm afraid to play with it any more since it's finally working)

culebrin - do you think that because of some speed issue, the values were not being evenly analyzed, thus the reason for the infinite loop?

I think it would be a string type conversion (ASCII - Unicode thing) I use

trim(cstr(strLineValue)) <> ""

to make sure the values are evenly analysed.

Greetings

I did give this a try, but it still did an infinite loop.

Hi,

So Why not Trim and Check...?

Do While Trim(strLineValue) <> ""

REgards
Veena

Hi Nakhla,

Well, if you did give it a try, there's nowhere else to go with that advice.

If you try to put pause into the code, it will slow down the loop, the best to use in these cases is the DoEvents command, it will execute all the unfinished commands until that line of code.

I hope this is what you are looking for...

Greetings

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.