
Originally Posted by
aluksnietis2
Hello, i need to copy some fields from excel to access database.
Im new to databases, so i dont know much. i found this macro, but it shows me error at range. could someone help me please?
here is code:
Sub ADOFromExcelToAccess()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Users\Lietotajs\Desktop\Database3.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Tabula", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 3 ' the start row in the worksheet
Do While Len(Range(I9).Formula) > 0 <---- error Range(I9)
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("Nr") = Range("I" & 9).Value Range("I"&9)
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub