HOWTO: Query for Dates Using a SQL Statement in Visual BasicID: Q105173
|
When you query for Dates in a Microsoft Access database, you may receive
an incorrect result or a 'Type Mismatch' error message. To query for a
date in a SQL statement in Visual Basic for Windows, enclose the date in
pound signs (#).
NOTE: This article shows dates written in American format (MM/DD/YYYY). For
example, 12/31/1960.
The following example code selects every field from the Employees table in the Nwind.mdb sample database where the field Birth Date is greater than 12/31/1960. Nwind.mdb is the Microsoft Access sample database provided with Microsoft Access versions 1.0 and 1.1, and 2.0.
' Data1 is a data control.
Data1.DataBaseName = "C:\ACCESS\NWIND.MDB"
' Enter the following two lines as one, single line:
Data1.RecordSource = "SELECT * FROM Employees
WHERE [Birth Date] > #12/31/1960#"
Data1.Refresh
' The following example uses FindFirst with the same Data Control:
Data1.RecordSet.FindFirst "[Hire Date] <= #9/21/1992#"
Additional query words:
Keywords : kbVBp300 kbVBp400
Version : WINDOWS:3.0,4.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: June 21, 1999