J
Jani
I have code (started from vba code from this site... thanks!!!) where data is
exported from Excel to Access. It works correctly when put into a regular
Access table. When I try to export it to a SQL table, I get a run-time error
3219 - Invalid Operation. The code is below and the problem is on line 8. Can
anyone provide directions on how to export from Excel directly into an SQL
table? Thanks in advance, as always... Jani
Sub ExportProjectToAccess()
' exports data from the GlobalOpsOnly worksheet to a table in an Access
database
' must set references to DAO object in order for macro to run
' this procedure must be edited before use
Dim db As Database, rs As Recordset, r As Long
Set db = OpenDatabase("C:\DT\CARMGMT\CapitalExpenditure2.mdb")
' open the database
***** Set rs = db.OpenRecordset("dbo_uCARData2", dbOpenTable)
' get all records in a table
r = 2 ' 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("Sent1") = Range("A" & r).Value
.Fields("Date1") = Range("B" & r).Value
.Fields("Stage") = Range("C" & r).Value
.Fields("Comments1") = Range("D" & r).Value
.Fields("BudgetID") = Range("E" & r).Value
.Fields("CAROriginator") = Range("F" & r).Value
.Fields("CostCenter") = Range("G" & r).Value
.Fields("NPV") = Range("H" & r).Value
.Fields("MIRR") = Range("I" & r).Value
.Fields("AlphaCode") = Range("L" & r).Value
.Fields("Country") = Range("M" & r).Value
.Fields("Description") = Range("O" & r).Value
.Fields("Purpose") = Range("P" & r).Value
.Fields("Category") = Range("Q" & r).Value
.Fields("DateRecd") = Range("R" & r).Value
.Fields("ApprovedBudget") = Range("S" & r).Value
.Fields("CapitalRequested") = Range("T" & r).Value
.Fields("ExpenseRequested") = Range("U" & r).Value
.Fields("Budgeted") = Range("V" & r).Value
.Fields("Funded") = Range("W" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
MsgBox "The job is done!"
Set rs = Nothing
db.Close
Set db = Nothing
End Sub
exported from Excel to Access. It works correctly when put into a regular
Access table. When I try to export it to a SQL table, I get a run-time error
3219 - Invalid Operation. The code is below and the problem is on line 8. Can
anyone provide directions on how to export from Excel directly into an SQL
table? Thanks in advance, as always... Jani
Sub ExportProjectToAccess()
' exports data from the GlobalOpsOnly worksheet to a table in an Access
database
' must set references to DAO object in order for macro to run
' this procedure must be edited before use
Dim db As Database, rs As Recordset, r As Long
Set db = OpenDatabase("C:\DT\CARMGMT\CapitalExpenditure2.mdb")
' open the database
***** Set rs = db.OpenRecordset("dbo_uCARData2", dbOpenTable)
' get all records in a table
r = 2 ' 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("Sent1") = Range("A" & r).Value
.Fields("Date1") = Range("B" & r).Value
.Fields("Stage") = Range("C" & r).Value
.Fields("Comments1") = Range("D" & r).Value
.Fields("BudgetID") = Range("E" & r).Value
.Fields("CAROriginator") = Range("F" & r).Value
.Fields("CostCenter") = Range("G" & r).Value
.Fields("NPV") = Range("H" & r).Value
.Fields("MIRR") = Range("I" & r).Value
.Fields("AlphaCode") = Range("L" & r).Value
.Fields("Country") = Range("M" & r).Value
.Fields("Description") = Range("O" & r).Value
.Fields("Purpose") = Range("P" & r).Value
.Fields("Category") = Range("Q" & r).Value
.Fields("DateRecd") = Range("R" & r).Value
.Fields("ApprovedBudget") = Range("S" & r).Value
.Fields("CapitalRequested") = Range("T" & r).Value
.Fields("ExpenseRequested") = Range("U" & r).Value
.Fields("Budgeted") = Range("V" & r).Value
.Fields("Funded") = Range("W" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
MsgBox "The job is done!"
Set rs = Nothing
db.Close
Set db = Nothing
End Sub