Run time error 5: invalid procedure call or argument (1 Viewer)

bwc

Registered User.
Local time
Yesterday, 18:19
Joined
Feb 7, 2013
Messages
22
with the help from this site and others, i have cobbled together this code and have had it work. however, this site i wish to extract data from gives me an error 5.
Code:
Private Sub Command56_Click()
 'On Error GoTo ErrorHandler

Dim rstSsn As DAO.Recordset
Set rstSsn = CurrentDb.OpenRecordset("SELECT bbAwards.ssnInd, bbAwards.smName FROM bbAwards;")


  DoCmd.SetWarnings False
'''''''''''' clear table
    DoCmd.RunSQL ("DELETE bbAwardsFed.* FROM bbAwardsFed;")
'''''' start time ''''''
    Me!txtStatus.Value = "Start time: " & Format(Time(), "Hh:Nn") & vbCrLf
    Me.Repaint
      If rstSsn.RecordCount > 0 Then
        rstSsn.MoveFirst
        Do Until rstSsn.EOF
          Debug.Print rstSsn!ssnInd
          
          rawHtml = GetPage("https://aWebSite.asp?SSN=" & rstSsn!ssnInd & " ")
          ' Search forward until we're just before the table we want
          tmpAt = InStr(1, rawHtml, "<h3 align")
          tmpAt = InStr(tmpAt, rawHtml, "<h3 align")
          Debug.Print tmpAt
          ' Get the index of the start of the opening <table> tag
          tableStart = InStr(tmpAt, rawHtml, "<table")
          Debug.Print tableStart
          ' Get the index of the end of the closing </table> tag
          tmpAt = InStr(tableStart, rawHtml, "</table")
          Debug.Print tmpAt
          tableEnd = InStr(tmpAt, rawHtml, ">")
          Debug.Print tableEnd
          ' Extract the table
          tableChunk = Mid(rawHtml, tableStart, tableEnd - tableStart + 1)
          Debug.Print tableChunk
          ' Use native VBA file I/O
          tempFile = Application.CurrentProject.Path & "\tempTable.html"
          Open tempFile For Output As #1
          Write #1, tableChunk
          Close #1
''''''''''''' Import the file to a table
          ' http://www.blueclaw-db.com/transfertext-docmd.htm
          txtImportName = "ImportbbAwardsFed" 'MSysIMEXColumns is a hidden system table
              'file>options, current database>navigation options, put check in "show system objects"
          DoCmd.TransferText acImportHTML, txtImportName, "bbAwardsFed", tempFile, False
    Me!txtStatus.Value = Me!txtStatus.Value & "Getting the award data for " & rstSsn!smName & "..." & vbCrLf
    Me.Repaint

            rstSsn.MoveNext
        ' Delete the temp file
        'Kill tempFile
        Loop
      End If
    rstSsn.Close
    Set rstSsn = Nothing

Where it fails is on this line; tmpAt = InStr(tableStart, rawHtml,"</table")

Debug.Print tmpAt before that line will be 1995; but when it fails, that line is 509
After that I have
Debug.Print tableStart; it will be between 2148 and 2154; but when it fails, that line is 0

Is it fail because there is no html code to count? What is more frustrating is that it does not fail on the same record; this time it failed after 78 records, earlier today it was 80 or so; there has been times it has failed before 50.

i am venturing into something new with scraping from the web, and i am stuck with this one:banghead: thank you for your help
 

Micron

AWF VIP
Local time
Yesterday, 19:19
Joined
Oct 20, 2018
Messages
3,478
Did we teach you to use Option Explicit at the top of every module? I don't see where some of your variables are declared. The variables for the Instr position are probably returning 0, which is invalid. Step through your code and check them all as you go, and for Heaven's sake, go to editor options and turn on Require Variable Declaration for future code, and go back to the existing modules and add Option Explicit - or you deserve what you get (or so I've read).
EDIT - I realize that you said you checked tmpAt but you use other variables for the same thing (tableStart). When it's 509 as you said, it's probably too high. Again, that requires checking all conditions, such as the length of the string you're applying Instr to. And yes, if there is no html (length of string is 0?) 0 is an invalid number for the start position of Instr.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:19
Joined
May 7, 2009
Messages
19,231
you should test first if there is a <table> tag:
Code:
If Instr(1, rawHTML, "<table>") <> 0 then
      'proceed extaction code here
      ...
      ....
End If
 

bwc

Registered User.
Local time
Yesterday, 18:19
Joined
Feb 7, 2013
Messages
22
thank you, arnelgp, works wonderfully now
 

Users who are viewing this thread

Top Bottom