我正在尝试从Access表中将数据导入Excel。我收到语法错误当我运行该行时出现此错误:。选择[Time],[Tank],FROM" UnitOneRouting",WHERE [Date] =" &安培; RpDate&订购坦克,时间",cn,adOpenStatic,adLockOptimistic,adCmdTable 在访问表中有四列(按顺序)日期,时间,坦克,评论。我只想导入两列,时间和坦克,但顺序坦克,时间;我想根据Excel工作表中给出的日期导入它们。感谢
如何重新排列以下部分以打开表格,选择列(基于日期)并导入数据。
用rs '打开记录集 .Open" UnitOneRouting",cn,adOpenStatic,adLockOptimistic,adCmdTable '根据日期过滤行 。选择[Time],[Tank],FROM" UnitOneRouting",WHERE [Date] =" &安培; RpDate&订购坦克,时间",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
答案 0 :(得分:0)
问题是第二个和第三个.Open命令。调整你的声明:
With rs
.Open "SELECT Time, Tank FROM " & TableName & " WHERE [Date] = " & RpDate & _
"ORDER BY Tank, Time", cn, adOpenStatic, adLockOptimistic, adCmdTable
' all records
' cn.CursorLocation = adUseClient
' filter rows based on date
End With