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

Dani AI

Generated

Short answer and a robust fix you can apply immediately.

The root cause is almost always the way the file is being read rather than a mysterious compiler bug: Input # reads delimited fields (commas/quoted values) while Line Input / TextStream.ReadLine reads a whole line up to CR or CR+LF. That difference makes blank lines and delimiters behave unexpectedly when you test for "". Always check EOF before any Input/Line Input call. (learn.microsoft.com)

Practical, reliable approach (avoid Input # for whole-line parsing):

  • Read line-by-line with the FileSystemObject TextStream (or use Line Input #) so you get the real text of each line.
  • After reading a line use Len(Trim$(line)) = 0 (or line = vbNullString) to detect an empty line and exit the inner loop.
  • Always check AtEndOfStream / EOF before reading to prevent Input‑past‑EOF errors. (learn.microsoft.com)

Example (FileSystemObject/TextStream — not used in the thread already):

Dim fso As Object, ts As Object, s As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(filename, 1)   ' 1 = ForReading
Do While Not ts.AtEndOfStream
    s = ts.ReadLine
    If Len(Trim$(s)) = 0 Then Exit Do   ' stop on blank line
    ' process s (Split, increment param, etc.)
Loop
ts.Close
Set ts = Nothing
Set fso = Nothing

Troubleshooting checklist (why MsgBox helped but pause was a symptom):

  • was right to recommend guarding against EOF in inner loops — read-before-check or check-before-read is essential.
  • /’s Trim advice helps detect lines that look empty but contain spaces, but it won’t fix using Input # for whole-line parsing.
  • A MsgBox/pause or DoEvents can hide timing or error‑handling symptoms but doesn’t fix parsing logic; prefer deterministic fixes (Line Input or TextStream + EOF checks) and add logging (Debug.Print or write to a temp file) to see the actual line contents and any invisible characters.
  • If problems persist, dump each line’s character codes to detect non‑printable characters (Chr(0), etc.) and explicitly remove them before comparison.

References: Microsoft docs for Input #, Line Input #, EOF and TextStream are useful background when switching to the safer pattern above. (learn.microsoft.com)

Recommended Answers

All 6 Replies

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.