I am running this code to import data from Access to Excel and getting a run-time error:
"syntax error in FROM clause."
The table in Access has four columns: Date, Time, Tank, Comments, and I want to import Time and Tank, based on a date in the spreadsheet.
I want to order these columns in the order Tank, Time.
The error is in the line:
.Open "Select [Time], [Tank] FROM [UnitOneRouting] WHERE [Date] = " & RpDate & " ORDER BY Tank, Time", cn, adOpenStatic, adLockOptimistic, adCmdTable
Code Snippet:
Sub ADOImportFromAccessTable()
Dim DBFullName As String
Dim TableName As String
Dim TargetRange As Range
Dim RpDate As Range
DBFullName = "U:\Night Sup\Production Report 2003 New Ver 5-28-10_KA.mdb"
TableName = "UnitOneRouting"
Worksheets("TankHours").Activate
Set TargetRange = Range("C5")
Set RpDate = Range("B2").Cells
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
"U:\Night Sup\Production Report 2003 New Ver 5-28-10_KA.mdb" & ";"
Set rs = New ADODB.Recordset
With rs
' open the recordset
' filter rows based on date
.Open "Select [Time], [Tank] FROM [UnitOneRouting] WHERE [Date] = " & RpDate & " ORDER BY Tank, Time", cn, adOpenStatic, adLockOptimistic, adCmdTable
rs.Open , TargetRange
TargetRange.CopyFromRecordset rs
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
.Open "Select [Time] ...line and run your code. When the code breaks on that line, copy the entire SQL string including the first and last quotation marks. In the immediate window, type "?" and then paste the SQL string and hit enter. What is the resulting String? This is exactly what gets sent to Access as the request, so the problem should be visible. Add it to your question down at the bottom and that will help us help you :D