| re: Updating acess from excel using ADO
Thank you for you prompt respond.
I tried what you send me and I still can not make this work. I have modified the code I had but I am getting Run time error 3704. I am sending you the code and please help and let me know what I am doing wrong. I feel real dumb :( . Thanks again.
here is the code I modified.
Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
For i = 3000 To 65000
project = Cells(i, 2)
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\geo.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "geo", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
' r = 3 ' the start row in the worksheet
Do While Len(Range("D" & i).Formula) > 0
' repeat until first empty cell in column A
With rs
strSQL = "select * from TableName where Field1 = 'project'"
' On Error Resume Next
.Open strSQL, cn, adOpenKeyset, adLockOptimistic, adCmdText
' On Error GoTo 0
If .State = adStateOpen Then ' successfully opened the recordset
If .EOF Then ' no records returned
.AddNew ' create a new record
.Fields("Field2") = Range("B" & i).Value
.Fields("Field3") = Range("C" & i).Value
.Fields("Field4") = Range("D" & i).Value
.Fields("Field5") = Range("E" & i).Value
.Fields("Field6") = Range("F" & i).Value
.Fields("Field8") = Range("H" & i).Value
.Update ' stores the new record
Else ' one (or more records returned)
' edit existing record
.Fields("Field4") = Range("D" & i).Value
.Fields("Field5") = Range("E" & i).Value
.Fields("Field6") = Range("F" & i).Value
.Fields("Field8") = Range("H" & i).Value
.Update ' stores the new record
End If
.Close ' close the recordset
End If
End With
r = 1 + 1
Loop
Next i
Set rs = Nothing
End Sub
|