我正在尝试根据特定的日期条件将特定记录从Access表导入Excel电子表格。代码在尝试执行sql语句时失败,错误消息显示为
“数据类型不匹配”
我已经探索了尽可能多的方法,或者我可以在网上找到什么方式来设置日期的数据类型,但似乎没有任何效果。在修改代码之后,我能够摆脱错误消息,但代码无法识别访问表中的数据。任何帮助将不胜感激。如果我的问题没有任何意义,请提前道歉。试图让我了解这是一个新手开发者...感谢您的耐心等待。
Public Sub ImportData()
Application.ScreenUpdating = False
'
' Initialize shtArray (Public Array)
'
With ThisWorkbook
shtArray = Array(.Sheets("shtDom"))
End With
'
' Initialize tblArray (Public Array)
'
tblArray = Array("tbl_DOM")
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim dbPath As String
Dim SQL As String
Dim i As Integer
Dim sht As Worksheet, lastRow As Long
Dim dtDom As String, dtObk As String
If Weekday(frmInterface.dtPickDomestic, vbMonday) = 1 Then
dtDom = Format(frmInterface.dtPickDomestic - 3, "dd/mm/yyyy")
Else
dtDom = Format(frmInterface.dtPickDomestic - 1, "dd/mm/yyyy")
End If
If Weekday(frmInterface.dtPickOtherBanks, vbMonday) = 1 Then
dtObk = Format(frmInterface.dtPickOtherBanks - 3, "dd/mm/yyyy")
Else
dtObk = Format(frmInterface.dtPickOtherBanks - 1, "dd/mm/yyyy")
End If
dbPath = ThisWorkbook.Path & "\DOMESTIC SETTLEMENTS.mdb"
Set con = New ADODB.Connection
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
For i = LBound(tblArray) To UBound(tblArray)
Select Case i
Case 0, 8, 9
SQL = "SELECT * FROM " & tblArray(i) & " WHERE [BAL_DATE] = #" & dtDom & "#"
Case 10, 11, 12, 13
'SQL = "SELECT * FROM " & tblArray(i) & " WHERE [BAL_DATE] = #" & dtObk & "#"
Case Else
GoTo continue
End Select
Set sht = shtArray(i)
lastRow = sht.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Set rs = New ADODB.Recordset
rs.Open SQL, con
If rs.EOF And rs.BOF Then
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
MsgBox "No records!!!", vbCritical
Exit Sub
End If
shtArray(i).Range("A" & lastRow).CopyFromRecordset rs
rs.Close
Set rs = Nothing
continue:
Next i
con.Close
Set con = Nothing
Set sht = Nothing
On Error GoTo 0
Exit Sub
errHandler:
Set rs = Nothing
Set con = Nothing
End Sub