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

时间:2015-09-18 19:59:08

标签: excel-vba ms-access-2010 vba excel

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

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

我包含了部分代码。我已经使用此代码执行其他查询,它工作正常。此外,getDate方法确实返回日期。请参阅下面的代码:

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

1 个答案:

答案 0 :(得分:1)

更改

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做同样的事。