ADO UPDATE Errors
This is only my 2nd posting. I'm not sure of the protocol so don't hesitate to set me straight. Anyway I'm using Excel VBA w/Jet ADO to access a text file called stgenlist.csv which has a header row. The 1st column is "Insid" which is a unique identifier. "Ma" is the 10th column and is a float. Please take a look at the following code modified from page 401 of Excel 2002 Programmers..Ref. So far, I'm able to INSERT a new record but when I get to the UPDATE portion I keep getting a
"No Value Given for one or more parameters" error
I've been stepping thru the code to make it work.
Here's the code
=========================================
Public Sub InsertUpdateDelete()
Dim objCommand As ADODB.Command
Dim rsData As ADODB.Recordset
Dim lRecordsAffected As Long
Dim lKey As Long
Dim szConnect As String
Dim FileName As String
Dim IdType1 As String
Dim IdType2 As String
Dim Id As String
Dim CompID As String
On Error GoTo ErrorHandler
CompID = "ST"
FileName = CompID & "GenList.csv"
IdType1 = "Insid"
IdType2 = "Insdate"
Id = "TestID1"
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("LbData")
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
stDB = ThisWorkbook.Path & "\Data\" & CompID & "data\"
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & stDB & ";" & _
"Extended Properties=Text;"
' Create the Command object we'll use for all three queries.
Set objCommand = New ADODB.Command
objCommand.ActiveConnection = szConnect
'**** INSERT a new record into the database **************
' Load the SQL string into the Command object.
objCommand.CommandText = "INSERT INTO " & FileName & "(" & IdType1 & "," & IdType2 & ",batid) " & _
"VALUES('" & Id & "','1/1/2001','aa1');"
' Execute the SQL statement.
objCommand.Execute Recordsaffected:=lRecordsAffected, _
Options:=adCmdText Or adExecuteNoRecords
' Check for errors. Only one record should have been affected.
If lRecordsAffected <> 1 Then Err.Raise Number:=vbObjectError + 1024, _
Description:="Error executing INSERT statement."
'################################################# ################
'HERE'S THE PROBLEM AREA, If it works as anticipated, we should
'find the newly inserted record by it's "insid" and update the "Ma" column
'with the value 1.234
'**** UPDATE the record we just created ******************
' Load the SQL string into the Command object.
objCommand.CommandText = "UPDATE " & FileName & _
" SET Ma= '1.234' WHERE Insid= " & Id & " ;"
' Execute the SQL statement.
objCommand.Execute Recordsaffected:=lRecordsAffected, _
Options:=adCmdText Or adExecuteNoRecords
' Check for errors. Only one record should have been affected.
If lRecordsAffected <> 1 Then Err.Raise Number:=vbObjectError + 1024, _
Description:="Error executing UPDATE statement."
ErrorExit:
' Destroy our ADO objects.
Set objCommand = Nothing
Set rsData = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbCritical
Resume ErrorExit
End Sub
Rene
|