Solved

Loop not moving to the next record

Posted on 2010-09-14
23
566 Views
Last Modified: 2012-05-10
I'm so confused and frustrated!  I'm trying to loop through >5K records in table "Files" that list file locations, go to that file, get what I need, put it in another table "Alerts" and move to the next file name in the "Files" table.  It's parsing the data correctly and adding it to "Alerts" ok, but it's all from the same file!  It isn't moving to the next record in the "Files" table and it errors "too many files" after 255 loops.  I assume that's a Free File limitation but I don't know how else to handle it. The files have unique numbers at the end of their names, but I haven't had any luck using them either.  (example: C:\Documents and Settings\sellis\Desktop\Remedy KM\WallboardAlerts\Wallboard2.html")  Here's the code.  I'd really appreciate any suggestions.

Private Sub btnLoad_Click()

Dim txt As String
Dim strF As String
Dim dbs As Database
Dim rstA As Recordset
Dim rstF As Recordset
Set dbs = CurrentDb
Set rstA = dbs.OpenRecordset("Alerts")
Set rstF = dbs.OpenRecordset("Files", dbOpenDynaset)

'LOOP THROUGH FILES
With rstF

    Do While Not .EOF
        rstF.MoveNext
        Forms!form1.cmbFile = File
        Forms!form1.cmbFile.Requery
        Forms!form1.Refresh
       
'LOOP THROUGH HTML
        filenum = FreeFile
        Open Forms!form1.cmbFile For Input As #filenum
   
NextLine:
            Do While Not EOF(filenum)
            Line Input #filenum, txt
            If InStr(txt, "Alert Number:") > 0 Then
                    Do While InStr(txt, "<") > 0
                        txt = Replace(txt, Mid(txt, InStr(txt, "<"), InStr(txt, ">") - InStr(txt, "<") + 1), "")
                    Loop
                    txt = Right(txt, Len(txt) - InStr(txt, ":") - 1)
                    stralertno = Replace(txt, "&#45", "-")
            ElseIf InStr(txt, "Product affected:") > 0 Then
                    Do While InStr(txt, "<") > 0
                        txt = Replace(txt, Mid(txt, InStr(txt, "<"), InStr(txt, ">") - InStr(txt, "<") + 1), "")
                    Loop
                    txt = Right(txt, Len(txt) - InStr(txt, ":") - 1)
                    strprod = Replace(txt, "&#45", "-")
            End If
            Loop
NextFile:
            Close #FileName
    'ADD TO ALERTS TABLE
        With rstA
            .AddNew
            !AlertNo = stralertno
            !Prod = strprod
            .Update
        End With
    Loop
End With

End Sub
0
Comment
Question by:sarahellis
  • 10
  • 6
  • 2
  • +2
23 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 33674067
You are at no point reading any data from Files (rstF).

0
 
LVL 77

Expert Comment

by:peter57r
ID: 33674095
And as an 'incidentally' comment, if you were to read from Files you would be starting from the second record and missing out the first one.

You need a structure of :

rs.movefirst
Do until rs.eof
' do someprocessing

rs.movenext
Loop
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33674101
Close #FileName
should probably be
Close #filenum
that would fix the "too many files" i guess
0
 

Author Comment

by:sarahellis
ID: 33674102
Right - I WAS trying to get them out of the recordset.  Then I changed it so that it was supposed to scroll through the records in a combo box on a form.  I've had more experience with controls than recordsets.  It was a shot in the dark.  Nevertheless....
0
 
LVL 13

Accepted Solution

by:
Surone1 earned 500 total points
ID: 33674115
Forms!form1.cmbFile = File
maybe you want ???
Forms!form1.cmbFile = .File
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33674133
test this



Private Sub btnLoad_Click()



Dim txt As String

Dim strF As String

Dim dbs As Database

Dim rstA As Recordset

Dim rstF As Recordset

Set dbs = CurrentDb

Set rstA = dbs.OpenRecordset("Alerts")

Set rstF = dbs.OpenRecordset("Files", dbOpenDynaset)



'LOOP THROUGH FILES



With rstF 

rstF.MoveFirst

    Do Until .EOF

'        rstF.MoveNext  ' << Moved before Loop

        Forms!form1.cmbFile = File

        Forms!form1.cmbFile.Requery

        Forms!form1.Refresh

       

'LOOP THROUGH HTML

        filenum = FreeFile

        Open Forms!form1.cmbFile For Input As #filenum

   

NextLine:

            Do While Not EOF(filenum)

            Line Input #filenum, txt

            If InStr(txt, "Alert Number:") > 0 Then

                    Do While InStr(txt, "<") > 0

                        txt = Replace(txt, Mid(txt, InStr(txt, "<"), InStr(txt, ">") - InStr(txt, "<") + 1), "")

                    Loop

                    txt = Right(txt, Len(txt) - InStr(txt, ":") - 1)

                    stralertno = Replace(txt, "&#45", "-")

            ElseIf InStr(txt, "Product affected:") > 0 Then

                    Do While InStr(txt, "<") > 0

                        txt = Replace(txt, Mid(txt, InStr(txt, "<"), InStr(txt, ">") - InStr(txt, "<") + 1), "")

                    Loop

                    txt = Right(txt, Len(txt) - InStr(txt, ":") - 1)

                    strprod = Replace(txt, "&#45", "-")

            End If

            Loop

NextFile:

            'Close #FileName  '<< changed to below

             Close #filenum

    'ADD TO ALERTS TABLE

        With rstA

            .AddNew

            !AlertNo = stralertno

            !Prod = strprod

            .Update

        End With

     rstF.MoveNext 

    Loop

End With



End Sub

Open in new window

0
 

Author Comment

by:sarahellis
ID: 33674140
Oh, Surone1 - Good job!  That eliminated that error.  I got 5400 of the same record.  1 down, 1 to go!
0
 
LVL 24

Expert Comment

by:Bitsqueezer
ID: 33674208
Hi,

little hint: If you go to the "Tools" - "Options" menu and select "Require Variable Declaration" (which automatically adds an "Option Explicit" line to all new modules) then you would have found the problem on your own. You should always declare any variable first and if you want to add this check to existing modules, add the line "Option Explicit" as first line in any module where it is missing.
If you use "Debug" - "Compile" the compiler will show you any such errors and any missing variable. With this method you will have a cleaner code as you are forced to think about any variable first.

Cheers,

Christian
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33674248
what are the field names of the "files" recordset?
0
 

Author Comment

by:sarahellis
ID: 33674264
Just one field called "File".  
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33674274
Private Sub btnLoad_Click()

Dim txt As String
Dim strF As String
Dim dbs As Database
Dim rstA As Recordset
Dim rstF As Recordset
Set dbs = CurrentDb
Set rstA = dbs.OpenRecordset("Alerts")
Set rstF = dbs.OpenRecordset("Files", dbOpenDynaset)

'LOOP THROUGH FILES

With rstF
rstF.MoveFirst
    Do Until .EOF
'        rstF.MoveNext  ' << Moved before Loop
        Forms!form1.cmbFile = .File  '<<< changed to get from recordset
        Forms!form1.cmbFile.Requery
        Forms!form1.Refresh
       
'LOOP THROUGH HTML
        filenum = FreeFile
        Open Forms!form1.cmbFile For Input As #filenum
   
NextLine:
            Do While Not EOF(filenum)
            Line Input #filenum, txt
            If InStr(txt, "Alert Number:") > 0 Then
                    Do While InStr(txt, "<") > 0
                        txt = Replace(txt, Mid(txt, InStr(txt, "<"), InStr(txt, ">") - InStr(txt, "<") + 1), "")
                    Loop
                    txt = Right(txt, Len(txt) - InStr(txt, ":") - 1)
                    stralertno = Replace(txt, "&#45", "-")
            ElseIf InStr(txt, "Product affected:") > 0 Then
                    Do While InStr(txt, "<") > 0
                        txt = Replace(txt, Mid(txt, InStr(txt, "<"), InStr(txt, ">") - InStr(txt, "<") + 1), "")
                    Loop
                    txt = Right(txt, Len(txt) - InStr(txt, ":") - 1)
                    strprod = Replace(txt, "&#45", "-")
            End If
            Loop
NextFile:
            'Close #FileName  '<< changed to below
             Close #filenum
    'ADD TO ALERTS TABLE
        With rstA
            .AddNew
            !AlertNo = stralertno
            !Prod = strprod
            .Update
        End With
     rstF.MoveNext
    Loop
End With

End Sub
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 13

Expert Comment

by:Surone1
ID: 33674295
by moving the .movenext down we prevent skipping the first record (file)
0
 

Author Closing Comment

by:sarahellis
ID: 33674370
Thank you!  Sometimes I can't see the forest for the trees!  
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33674390
you should really have split the points here i feel :-(
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33674391
;-)
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33674398
but we are glad to help :-)
0
 

Author Comment

by:sarahellis
ID: 33674459
Ooooh, Sorry Capricorn1 - I didn't notice that his code just expanded on yours.  You've helped me so many times before too!  If a moderator can split, please do.  My sincere apologies.
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33674477
lol cap just gave you the solutions me and peter57r suggested, only sugarcoated ;-)
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33706333
my suggestion:
150 points each to cap and peter57r. the other 200 for me.
but you decide ofcourse! :-)
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33706342
and yes i was that someone else...
0
 

Author Comment

by:sarahellis
ID: 33707853
I'm afraid that I didn't find peter57r helpful.  He made two posts.  One that was just a statement "You are at no point reading any data from Files (rstF).", which was NOT AT ALL helpful.  The other was about reading the first record, which was not related to the problem I was having.  As he said, it was "incidentally."  I actually had .MoveFirst in the code originally and would have put it back once I got it working right.   Unfortunately, I found his tone to be a little bit sarcastic and condescending, so I didn't pay him much attention because of that.

As I looked more closely, at the posts, they were both solved by Surone1.  One issue was the "Close #FileName" vs "#FileNum" and the other was "forms!form1.cmbFile = .File" vs "=File"

These solved the problem I was having.  Capricorn1 was nice to re-post the code with the suggestions.  After re-examining the posts, the other helpful suggestion was from bitsqueezer, suggesting I use Option Explicit, which I did and was helpful.  But, I still think Surone1 should receive all the points.  

I do appreciate everyone's time in helping me with this.  I comment to everyone I know how wonderful the Experts are on this site.  You guys have saved me so many times.  Thank you.
0
 
LVL 24

Expert Comment

by:Bitsqueezer
ID: 33708060
Hi,

in my case it was only meant as I wrote: a little hint, not to assign points for as it was no solution for your problem...:-)
I'm glad that it helpful for you.

Cheers,

Christian
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now