1

This code is part of a function thats is being called several times.

for r = 0 to 4
    do until searchname.AtEndOfStream
        lineData = lcase(searchname.ReadLine())
        if instr(lineData,N(r))>0 then
            if (r = 0) then
                v = v + 1
            elseif (r = 1) then
                w = w + 1
            elseif (r = 2) then
                x = x + 1
            elseif (r = 3) then
                y = y + 1
            elseif (r = 4) then
                z = z + 1
            end if
        end if
    loop
next

My problem is that it only considers r = 0. I've tried ubound(N) instead. I've also tried replacing the for (r = 0) loop with five separate loops for v, w, x, y and z. I've tried several other methods and different formatting too, but it still didn't work.

user692942
  • 16,398
  • 7
  • 76
  • 175

1 Answers1

3

After the r = 0 case, when the inner loop has reached searchname.AtEndOfStream and you increment to the next value or r, searchname is still at the end of the stream. Therefore, the do loop only runs for the first case of the for loop. Consider this alternative:

do until searchname.AtEndOfStream
    lineData = lcase(searchname.ReadLine())
    for r = 0 to 4
        if instr(lineData,N(r))>0 then
            if (r = 0) then
                v = v + 1
            elseif (r = 1) then
                w = w + 1
            elseif (r = 2) then
                x = x + 1
            elseif (r = 3) then
                y = y + 1
            elseif (r = 4) then
                z = z + 1
            end if
        end if
    next
loop

By switching the loops around, you don't reach the end of the stream until you've finished iterating over both.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Thank's for getting back to me so soon. Works perfectly. I wouldn't have picked up on it other wise. Just wondering, if I ever do want to go by the other structure, how can I move the pointer back to the begining after the first loop? – user3782707 Oct 04 '14 at 13:59
  • Well what is `searchname`; what kind of structure, and how is it created? Have you looked in its documentation? – jonrsharpe Oct 04 '14 at 14:26
  • `searchname` is simply a textfile that's been opened. ( `set searchname = objFSO.OpenTextFile(("C:\Inetpub\wwwroot\Files\"&idname), 1, true)` ) – user3782707 Oct 04 '14 at 14:30
  • 1
    Use `ADODB.Stream` so you can use `.Position` property to reset the stream back to the starting position. Still use FSO to locate the file but then open it using the `ADODB.Stream` instead of the `TextStream` FSO object. – user692942 Oct 04 '14 at 15:01
  • Also I wouldn't store files inside your `wwwroot` you should link them via a virtual directory so they are outside of the security model of the website. – user692942 Oct 04 '14 at 15:03