David Evans
Active Member
Code:
Sub ExcelToAccess()
' exports data from the active worksheet to a table in an Access database
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim R As Long
Sheets("F2 Expense Sheet").Select
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
"Data Source= \\XXXX\1. Template\Fund Expenses\Fund Stuff.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Expenses", cn, adOpenKeyset, adLockOptimistic
' all records in a table
R = 6 ' the start row in the worksheet
Do While Len(Range("A" & R).Text) > 0
Debug.Print R
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("Fund Name") = Range("A" & R).Value
.Fields("Share Type") = Worksheets("F2 Expense Sheet").Range("B2").Value
.Fields("Fund Number") = Range("B" & R).Value
.Fields("Management Fees") = Range("D" & R).Value
.Fields("Other Expenses") = Range("E" & R).Value
.Fields("Fee Waiver Amount") = Range("H" & R).Value
.Fields("Gross Fees") = Range("F" & R).Value
.Fields("Net Fees") = Range("I" & R).Value
.Fields("As of Date") = Worksheets("F2 Expense Sheet").Range("B3").Value
.Update ' stores the new record
End With
R = R + 1 ' next row
Loop
rs.Close
Set rs = Nothing
' Sheets("Summary").Select
End Sub
The code fails here - rs.Open"Expenses", cn, adOpenKeyset, adLockOptimistic, although that Access file exists and has a Table named Expenses ...
Any ideas from the bored?