PDA

View Full Version : Solved: Object Required error when inserting data from Excel to Access



dak4548
03-15-2007, 12:45 PM
I have a macro that goes through each worksheet within an Excel file and creates a string of data to insert into an Access database table. When I run the insert statement from within the macro I am getting an error saying "Object required". Yet, I can copy the data from the insert statement and put it in an insert query in Access, and it inserts the row just fine. I know my connection string is ok because I delete all the data from the tables early on in the process, and that works fine. Below is my Insert routine. I appreciate any insight anyone can offer.

Deb K.
-------------------------
Sub InsertRow(notNull As Boolean, rcdDetail As String, colHead As String, tblname As String, cnt)

'Begin transaction processing
On Error GoTo EndUpdate
cnt.BeginTrans

'If record consists of only Null values, do not insert it to table, otherwise
'insert the record
Select Case notNull
Case Is = True
rst.Open "INSERT INTO " & tblname & colHead & " VALUES " & rcdDetail, cnt
Case Is = False
'do not insert record
End Select

EndUpdate:
'Check if error was encounted
If Err.Number <> 0 Then
'Error encountered. Rollback transaction and inform user
MsgBox "There was an error. Update was not succesful!" + vbCrLf + Err.Description, vbCritical, "Error!"
On Error Resume Next
cnt.RollbackTrans
Else
On Error Resume Next
cnt.CommitTrans
End If
End Sub

dak4548
03-15-2007, 12:51 PM
I found my error. I didn't have the recordset defined for the Insert routine. I declared it as a public variable and then my inserts worked fine.