home *** CD-ROM | disk | FTP | other *** search
- 'create a database table and insert records
-
- option explicit
-
- Dim DBConnection
-
- const adStateOpen = 1
- const adOpenDynamic = 2
- const adLockOptimistic = 3
- const adCmdTable = 2
-
-
- function processLine(line, EOL)
- 'insert each line into the table
-
- Dim s, DBORecord
-
- on error resume next
-
- 'split out each line into recno, lastname, firstname, address1 based on commas
- s = split( line, "," )
-
- clearErrors
- Set DBORecord = CreateObject("ADODB.Recordset")
- DBORecord.Open "table1", DBConnection, adOpenDynamic, adLockOptimistic, adCmdTable
- DBORecord.AddNew
- DBORecord("recno") = s(0)
- DBORecord("lastname") = s(1)
- DBORecord("firstname") = s(2)
- DBORecord("address1") = s(3)
- ' DBORecord.AddNew Array("recno", "lastname", "firstname", "address1"), Array( s(0), s(1), s(2), s(3) )
-
- if wasError() then
- reportErrors "Can't insert record"
- end if
-
- DBORecord.Update
- DBORecord.Close
-
- processLine = "Inserted " & line & EOL
- set DBORecord = Nothing
-
- end function
-
-
- sub startJob()
-
- on error resume next
-
- clearErrors
-
- msgBox "Opening database connection - using " & GetScriptEngineInfo
-
- Set DBConnection = CreateObject("ADODB.Connection")
- DBConnection.connectionString = "Data Source='Crystal';"
- DBConnection.ConnectionTimeout = 30
- DBConnection.open
-
- if DBConnection.State <> adStateOpen then
- reportErrors "Can't connect to data source 'Crystal'"
- end if
-
- msgBox "Creating the table1 table"
- DBConnection.Execute("CREATE TABLE table1 (recno char(5), lastname char(20), firstname char(20), address1 char(20) )")
- if wasError() then
- msgBox "Can't create table table1"
- clearErrors
- else
- msgBox "Table table1 created successfully"
- end if
-
- end sub
-
-
- sub endJob()
-
- on error resume next
-
- 'end of script, drop table
- msgBox "Dropping table table1"
- DBConnection.Execute("DROP TABLE table1")
- if wasError() then
- reportErrors "Can't drop table table1"
- else
- msgBox "Table table1 dropped successfully"
- end if
-
- msgBox "Closing the connection"
- 'close the connection
- DBConnection.Close
- reportErrors "Couldn't close connection"
- set DBConnection = Nothing
-
- end sub
-
-
- function startFile()
- startFile = ""
- end function
-
-
- function endFile()
- endFile = ""
- end function
-
-
- function wasError()
-
- wasError = (Err.Number <> 0) or (DBConnection.Errors.count <> 0)
-
- end function
-
-
- sub clearErrors()
-
- 'clear all messages first
- if (Err.Number <> 0) then
- Err.clear
- End if
-
- if DBConnection <> null then
- DBconnection.errors.clear
- end if
-
- end sub
-
-
- Function GetScriptEngineInfo
- Dim s
- s = "" ' Build string with necessary info.
- s = ScriptEngine & " Version "
- s = s & ScriptEngineMajorVersion & "."
- s = s & ScriptEngineMinorVersion & "."
- s = s & ScriptEngineBuildVersion
- GetScriptEngineInfo = s ' Return the results.
- End Function
-
-
- sub reportErrors( ErrorDescription )
-
- dim X
-
- If (Err.Number <> 0) or (DBConnection.Errors.count <> 0) Then
- msgBox ErrorDescription
-
- If (Err.Number <> 0) then
- MsgBox "Error # " & CStr(Err.Number) & " " & Err.Description
- Err.clear
- End if
-
- For Each X In DBConnection.Errors
- msgBox "DBError: " & X.Description
- Next
- DBConnection.Errors.clear
-
- Exit Sub
- End If
-
- end sub
-
-
- 'use this code for testing inside a separate .vbs file
- startJob
- startFile()
- msgBox processLine("100,Carter,Simon,Woodbine Court", chr(13) & chr(10) )
- msgBox processLine("101,Carter,Simon,Woodbine Court", chr(13) & chr(10) )
- msgBox processLine("102,Carter,Simon,Woodbine Court", chr(13) & chr(10) )
- endFile()
- endJob
-