elsenorjose
Technical User
I've tried searching the forum for this but can't find exactly what I'm looking for. I have a directory with Excel workbooks that differ only in name; i.e. workbook1, workbook2, etc. but have the same structure. I have created a table in Access that has all the fields I need and now am trying to write a macro that will import the data from each workbook into my table. Basically, and UPDATE or APPEND query but based on the workbooks. I have some code I've run in Excel to EXPORT the data but that would involve opening each workbook and running the macro. Can someone help me modify this to run in Access so I can just IMPORT the workbooks? Sorry if this isn't clear, feel free to ask for more clarification if needed. Here's my sample code in Excel (it works by the way.)
Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
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:\Documents\MyDB.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Table", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 8 ' 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("Rec") = Range("A" & r).Value
.Fields("Date") = Range("B" & r).Value
.Fields("DMA") = Range("C" & r).Value
.Fields("Store") = Range("F" & r).Value
.Fields("Ord Ct") = Range("L" & r).Value
.Fields("Avg Sale") = Range("N" & r).Value
.Fields("Sale PCYA") = Range("O" & r).Value
.Fields("Cost %") = Range("I" & r).Value
.Fields("Cost % Variance") = Range("I" & 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
Thanks in advance for the help!
Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
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:\Documents\MyDB.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Table", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 8 ' 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("Rec") = Range("A" & r).Value
.Fields("Date") = Range("B" & r).Value
.Fields("DMA") = Range("C" & r).Value
.Fields("Store") = Range("F" & r).Value
.Fields("Ord Ct") = Range("L" & r).Value
.Fields("Avg Sale") = Range("N" & r).Value
.Fields("Sale PCYA") = Range("O" & r).Value
.Fields("Cost %") = Range("I" & r).Value
.Fields("Cost % Variance") = Range("I" & 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
Thanks in advance for the help!