I have used this script in the past on a different page with no problems. I have tried to modify it to work with a different page. I am able to get it to create a good .csv file, however I cant get it to write to my DB. I get the following error
Code:
<% @Language="VBScript" %>
<%
'***
'* 1) Download NHL statistics via XMLHTTP.
'* 2) Optionally, write the page to a .TXT file.
'* 3) Parse the statistics into an array.
'* 4) Optionally, write the array to a .CSV file.
'* 5) Update a database table from the array.
'***
'*
'* Declare Constants
'*
Const cASP = "NHLStats.asp"
Const cTXT = "NHLStats.txt"
Const cCSV = "NHLStats.csv"
Const cMDB = "dbfiles\NHLPOOL.mdb"
Const cTBL = "NHLStats"
Const cDSN = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="
Const cCOL = "Pos,Player,Team,GP,G,A,PTS,+/-,PPG,SHG,GWG,GTG,PIM,SH,PCT"
Const cURL = "http://www.tsn.ca/nhl/Statistics.asp?category=Total_Points&position=&division=&season=2004&type=skater&endRow=1170&startRow=1"
Const cOUT = True '= Write .TXT and .CSV output files (True/False)
Const cDBG = True '= Debugging via "Response.Write()" (True/False)
'*
'* Declare Globals
'*
Dim arrRST(499,18)
Dim intRST
intRST = 0
Dim strXML
'*
'* Processing
'*
Call Retrieve()
Call Database()
Sub Retrieve()
'*
'* Declare Variables
'*
Dim arrCOL(15)
Dim intCOL
Dim strCOL
Dim strMSG
Dim strREC
Dim strVAL
Dim strVAR
'*
'* Declare Objects
'*
Dim objFSO
Dim objGFI
Dim objOTF
Dim objXML
'*
'* Retrieve Web Page
'*
'Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
objXML.Open "GET", cURL, false
objXML.Send
strXML = objXML.ResponseText
Set objXML = Nothing
'*
'* Optionally Write Files
'*
If cOUT Then
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'*
'* Write Web Page to File
'*
Set objOTF = objFSO.OpenTextFile(Server.MapPath(cTXT),2,true)
objOTF.Write(strXML)
Set objOTF = Nothing
'*
'* Write Web Data to File
'*
Set objOTF = objFSO.OpenTextFile(Server.MapPath(cCSV),2,true)
objOTF.WriteLine(cCOL)
End If
'*
'* Write CSV File from File
'*
'*
strVAR = Mid(strXML, InStr(strXML, "bgcolor=""#F7EDE0""")-32, Len(strXML))
strVAR = Left(strVAR, InStr(strVAR, "</table>")-1)
Do While InStr(strVAR, "<tr") > 0
'* Now extract the 'record' from the table (like above code)
strREC = Mid(strVAR, InStr(strVAR, ">")+1)
strREC = Left(strREC, InStr(strREC, "</tr>")-1)
'* Now we should be left with all the <td>'s
intCOL = 0
Do while InStr(strREC, "<td") > 0
strVAL = Mid(strREC, InStr(strREC, ">")+1)
strVAL = Left(strVAL, InStr(strVAL, "</td>")-1)
'* We have successfully got a value from a <td>!
'* Maybe at this point you could load it into an array:
arrCOL(intCOL) = strVAL
'* Now we have to clean the strREC string up
strREC = MID(strREC, InStr(strREC, "</td>")+5)
intCOL = intCOL + 1
Loop
'* At this point we have a loaded arrCOL with all the values from the record!
'* You could build an SQL string at this point or anything really
strCOL = Join(arrCOL,",")
strCOL = Replace(strCOL,vbCrLf,"")
'* Remove hyperlink around Player and Team
strCOL = Left(strCOL,InStr(strCOL,"<")-1) & Mid(strCOL,InStr(strCOL,">")+1)
strCOL = Replace(strCOL,"</a>","")
'* Remove spaces between Player and Team
Do Until InStr(strCOL,", ") = 0
strCOL = Replace(strCOL,", ",",")
Loop
'* Write Player's data to file
If cOUT Then objOTF.WriteLine(strCOL)
'* Write Player's data to array
intRST = intRST + 1
arrRST(intRST,0) = strCOL
'* Now we have to trim up the main string
strVAR = MID(strVAR, InStr(strVAR, "</tr>")+5)
Loop
'*
'* Destroy Objects
'*
If cOUT Then
Set objOTF = Nothing
Set objFSO = Nothing
End If
End Sub
Sub Database()
'*
'* Declare ADO Constants
'*
Const adCmdTable = &H0002
Const adLockOptimistic = 3
Const adOpenKeySet = 1
'*
'* Declare Variables
'*
Dim intFOR
Dim strNOW
strNOW = Now()
Dim strRST
strRST = Split(cCOL,",")
Dim strXDV
Dim strXTV
'*
'* Extract " as of " Date and Time
'*
strXDV = Mid(strXML,InStr(strXML," as of ")+Len(" as of "))
strXDV = Left(strXDV,InStr(strXDV,"<")-1)
strXTV = Mid(strXDV,InStr(strXDV,", at ")+Len(", at "))
strXTV = Replace(strXTV," ET","")
strXDV = Left(strXDV,InStr(strXDV,", at ")-1)
strNOW = FormatDateTime(DateValue(strXDV)+TimeValue(strXTV) ,0)
'*
'* Prepare Array
'*
If intRST = 0 Then
Response.Write "<b>Missing Stats!</b>"
Exit Sub
End If
If cDBG Then Response.Write("<br>0. " & cCOL)
If UBound(strRST) <> UBound(arrRST,2) Then
Response.Write "<b>Invalid 'cCOL'!</b>"
Exit Sub
End If
For intRST = 0 To UBound(arrRST,2)
arrRST(0,intRST) = strRST(intRST)
Next
'*
'* Declare Objects
'*
Dim objADO
Set objADO = Server.CreateObject("ADODB.Connection")
objADO.Open cDSN & Server.MapPath(cMDB)
Dim objRST
Set objRST = objADO.Execute("DELETE FROM " & cTBL)
Set objRST = Server.CreateObject("ADODB.Recordset")
objRST.Open cTBL, objADO, adOpenKeySet, adLockOptimistic, adCmdTable
'*
'* Update Database
'*
For intFOR = 1 To UBound(arrRST,1)
If arrRST(intFOR,0) <> "" Then
If cDBG Then Response.Write("<br>" & intFOR & ". " & arrRST(intFOR,0))
strRST = Split(arrRST(intFOR,0),",")
For intRST = 0 To UBound(arrRST,2)
arrRST(intFOR,intRST) = strRST(intRST)
Next
objRST.AddNew
objRST("ID") = intFOR
For intRST = 0 To UBound(arrRST,2)
objRST(arrRST(0,intRST)) = arrRST(intFOR,intRST)
Next
objRST("AsOf") = strNOW
objRST.Update
End If
Next
'*
'* Destroy Objects
'*
objRST.Close
Set objRST = Nothing
Set objADO = Nothing
End Sub
%>
Thanks for looking.