我想從Access表導入數據到Excel中。我得到一個語法錯誤當我運行這條線時出現這個錯誤:。Select [Time],[Tank],FROM「UnitOneRouting」,WHERE [Date] =「& RpDate & ORDER BY Tank,Time」,cn,adOpenStatic ,adLockOptimistic,adCmdTable 在訪問表中有四列(按順序)日期,時間,坦克,評論。我只想輸入兩列,時間和坦克,但是按照坦克,時間的順序;我想根據Excel工作表中給出的日期導入它們。謝謝從Access表導入數據到Excel表
如何重新排列以下部分以打開表格,選擇列(基於日期)並導入數據。
隨着RS '打開記錄 。開 「UnitOneRouting」,CN,ADOPENSTATIC,ADLOCKOPTIMISTIC,adCmdTable ' 過濾器行根據日期 。選擇[時間],[坦克]從 「UnitOneRouting」,其中[日期] = 「& RpDate & ORDER BY坦克,時間」,CN,ADOPENSTATIC,ADLOCKOPTIMISTIC,adCmdTable rs.Open,TargetRange 尾隨着
**代碼開始這裏**
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"
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
.Open "UnitOneRouting", cn, adOpenStatic, adLockOptimistic, adCmdTable
' filter rows based on date
.Select [Time], [Tank], FROM "UnitOneRouting", WHERE [Date] = " & RpDate & ORDER BY Tank, Time", cn, adOpenStatic, adLockOptimistic, adCmdTable
rs.Open , TargetRange
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
當我運行該行時,出現錯誤「FROM子句中的語法錯誤」。 。打開「SELECT Time,Tank FROM」&TableName&「WHERE [Date] =」&RpDate&「ORDER BY Tank,Time」,cn,adOpenStatic,adLockOptimistic,adCmdTable – Kish 2014-09-25 13:31:54
TableName是一個變量不僅僅是硬編碼?像:「SELECT Time,Tank FROM UnitOneRouting WHERE [Date] =」&RpDate&_ 「ORDER BY Tank,Time」,cn,adOpenStatic,adLockOptimistic,adCmdTable – 2014-09-25 14:16:55
是的,我可以硬編碼它。我試圖改變,以前沒有給出解決方案。我從上面複製了代碼並進行了測試。下面的行給出了一個編譯錯誤:預期:行號或標籤或語句或語句結尾 「選擇時間,坦克FROM UnitOneRouting WHERE [日期] =」 – Kish 2014-09-25 14:42:00