从 Excel 查询 Access 中的常规日期字段(错误 13)

Querying General Date field in Access from Excel (Error 13)

我需要从 Excel 查询 Access Table 中的通用日期字段。请建议如何正确设置日期格式以使查询正常工作。这可能是我在查询中遗漏的一个小细节,我无法弄清楚。我尝试以不同的方式设置日期格式,如果 Access 中日期字段的格式更改为 short date 但不是我需要使用的通用日期格式,则查询有效。以下是在 Access 中查询短日期的方法:

SQLwhere = SQLwhere & "[MDL_Table1].[UploadDate] BETWEEN " & Format(UserForm1.txtStartDate, "\#mm\/dd\/yyyy\#") & " AND " _ & Format(UserForm1.txtEndDate, "\#mm\/dd\/yyyy\#") & " AND "

我包含了我的一部分代码。我已经使用此代码执行其他查询并且运行良好。此外,getDate 方法会 return 日期。请在下面查看我的代码:

Dim qc1 As String
Dim DateMin As String
Dim DateMax As String
'add error handling

On Error GoTo errHandler:

'Disable screen flickering.
'FastWB True, 3
DateMin = Format(frmCalendar.getDate(DateMin), "mm/dd/yyyy hh:mm:ss AM/PM")
DateMax = Format(frmCalendar.getDate(DateMax), "mm/dd/yyyy hh:mm:ss AM/PM")
Sheet1.Range("A2:AK5000").ClearContents
dbPath = "H:\DEMO\MDL_IonTorrent.accdb"

'set the search variable
qc1 = "_QC"

Set cnn = New ADODB.Connection ' Initialise the collection class variable
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath

SQLwhere = "WHERE "

SQLwhere = SQLwhere & "[MDL_Table1].[UploadDate] BETWEEN #" & DateMin & "#     AND #" _
& DateMax & "# AND "

SQLwhere = SQLwhere & "[MDL_Table1].[AAchange] LIKE '" & "%" & qc1 & "%" & "' AND "
strSQL = "SELECT * FROM [MDL_Table1] "

'Remove the last AND applicable
If SQLwhere = "WHERE " Then
    SQLwhere = ""
Else
    SQLwhere = Left(SQLwhere, Len(SQLwhere) - 5)
End If

strSQL = strSQL & SQLwhere
Set rs = New ADODB.Recordset 'assign memory to the recordset
rs.Open strSQL, cnn

改变

DateMin = Format(frmCalendar.getDate(DateMin), "mm/dd/yyyy hh:mm:ss AM/PM")

DateMin = Format(frmCalendar.getDate(DateMin), "mm-dd-yyyy hh:mm:ss")

对 datemax 做同样的事情。