PDA

View Full Version : Help Creating VBA to Export/Copy data from Excel to Access



henry l
04-07-2017, 03:52 PM
Using Excel & Access 2013




In Excel, I've a sheet called 'Access' that has relevant data located at (A2:U2).
The Excel document will not always be in the same folder or even on the same computer.


In MS Access I've a table called 'Tbl-Form' that I would like to append the Excel data into.
The Access database is located at "F:\Team All\FileTrack.accdb" for all computers.


I can manually copy the data from (A2:U2) in Excel and Paste/Append in the correct Table in Access without error.


I have tried multiple iterations of VBA codes and do not seem to understand what is necessary to do this.


I will take any and all help and direction and be most appreciative.


Thank you in advance.

henry l
04-10-2017, 10:38 AM
This is the code I am using, but I am currently receiving an error on the following line.

Runtime error '-2147217900 (80040e14)':
Syntax error in FROM clause.

rs.Open "Tbl-Form", cn, adOpenKeyset, adLockOptimistic, adCmdTable



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.ACE.OLEDB.12.0;
Data Source=C:\Users\Henry\Desktop\Tracking.accdb; Persist Security Info=False;"








' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Tbl-Form", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' 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("ID") = Range("A" & r).Value
.Fields("Date Received") = Range("B" & r).Value
.Fields("RD") = Range("C" & r).Value
.Fields("Issue") = Range("D" & r).Value
.Fields("Date Sent for Approval") = Range("E" & r).Value
.Fields("Staff") = Range("F" & r).Value
.Fields("Date Approval Received") = Range("G" & r).Value
.Fields("Date Completed") = Range("H" & r).Value
.Fields("Status") = Range("I" & r).Value
.Fields("RIssue") = Range("J" & r).Value
.Fields("Part Name") = Range("K" & r).Value
.Fields("Part ID") = Range("L" & r).Value
.Fields("Prog ") = Range("M" & r).Value
.Fields("Enroll") = Range("N" & r).Value
.Fields("Enrollment") = Range("O" & r).Value
.Fields("Exit Date Removed") = Range("P" & r).Value
.Fields("DA") = Range("Q" & r).Value
.Fields("Office") = Range("R" & r).Value
.Fields("Staff") = Range("S" & r).Value
.Fields("Notes") = Range("T" & r).Value
.Fields("Field1") = Range("U" & 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

henry l
04-10-2017, 11:11 AM
I've added square brackets around the table name and now I receive no errors.

Unfortunately, nothing is being appended to the table in Access when I now run the macro.

Current code:

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.ACE.OLEDB.12.0;
Data Source=C:\Users\Henry\Desktop\Tracking.accdb; Persist Security Info=False;"








' open a recordset
Set rs = New ADODB.Recordset
rs.Open "[Tbl-Form]", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' 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("ID") = Range("A" & r).Value
.Fields("Date Received") = Range("B" & r).Value
.Fields("RD") = Range("C" & r).Value
.Fields("Issue") = Range("D" & r).Value
.Fields("Date Sent for Approval") = Range("E" & r).Value
.Fields("Staff") = Range("F" & r).Value
.Fields("Date Approval Received") = Range("G" & r).Value
.Fields("Date Completed") = Range("H" & r).Value
.Fields("Status") = Range("I" & r).Value
.Fields("RIssue") = Range("J" & r).Value
.Fields("Part Name") = Range("K" & r).Value
.Fields("Part ID") = Range("L" & r).Value
.Fields("Prog ") = Range("M" & r).Value
.Fields("Enroll") = Range("N" & r).Value
.Fields("Enrollment") = Range("O" & r).Value
.Fields("Exit Date Removed") = Range("P" & r).Value
.Fields("DA") = Range("Q" & r).Value
.Fields("Office") = Range("R" & r).Value
.Fields("Staff") = Range("S" & r).Value
.Fields("Notes") = Range("T" & r).Value
.Fields("Field1") = Range("U" & 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