I need to query a general date field in Access Table from Excel. Please suggest how to format the date correctly for the query to work. It is probably a minor detail that I’m missing in the query that I can’t figure it out. I've tried to format the date differently and the query worked if the format of date field in Access was changed to short date but not with general date format that I need to use. Here is what worked to query short date in Access:
SQLwhere = SQLwhere & "[MDL_Table1].[UploadDate] BETWEEN " & Format(UserForm1.txtStartDate, "\#mm\/dd\/yyyy\#") & " AND " _
& Format(UserForm1.txtEndDate, "\#mm\/dd\/yyyy\#") & " AND "
I'm including a part of my code. I've used this code to perform other queries and it worked fine. Also, getDate method does return the date. Please see my code below:
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
Answer
Change
DateMin = Format(frmCalendar.getDate(DateMin), "mm/dd/yyyy hh:mm:ss AM/PM")
to
DateMin = Format(frmCalendar.getDate(DateMin), "mm-dd-yyyy hh:mm:ss")
Do the same with datemax.
No comments:
Post a Comment