PDA

View Full Version : [SOLVED] EXCEL to ACCESS



austenr
05-16-2005, 09:55 AM
The following code is intended to write EXCEL records to ACCESS, however it will not compile. The compile error is on the Set cn =. However cn is defined above. What is up? Thanks in advance for your help!


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
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\FolderName\DataBaseName.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 3 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("FieldName1") = Range("A" & r).Value
.Fields("FieldName2") = Range("B" & r).Value
.Fields("FieldNameN") = Range("C" & r).Value
' 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

JKwan
05-16-2005, 10:02 AM
You need to REFERENCE "Microsoft ActiveX Data Objects 2.x Library"

austenr
05-16-2005, 10:07 AM
example please

JKwan
05-16-2005, 10:32 AM
In VBE goto Tools - References, scroll down until you see "Microsoft ActiveX Data Objects 2.x Library"

Bob Phillips
05-16-2005, 10:32 AM
example please

Go to the VB IDE - Alt-F11

Menu Item Tools>References

In the dialog box that pops up, scroll down to
"Microsoft ActiveX Data Objects 2.x Library"

where x is a version number (7 on my 2002 system)

check that box

exit, and try again

(or go late binding)

austenr
05-16-2005, 10:33 AM
Thanks both of you. I found it earlier.