How to Query for Dates Using a SQL Statement in Visual Basic

Last reviewed: March 20, 1998
Article ID: Q105173
3.00 4.00 WINDOWS kbprg

The information in this article applies to:

  • Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit only, for Windows, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, version 3.0

SUMMARY

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/YY). For example, 12/31/60 means December 31, 1960.

MORE INFORMATION

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/60. 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.DataBase = "C:\ACCESS\NWIND.MDB"
   ' Enter the following two lines as one, single line:
   Data1.RecordSource = "SELECT * FROM Employees
      WHERE [Birth Date] > #12/31/60#"
   Data1.Refresh

   ' The following example uses FindFirst with the same Data Control:
   Data1.RecordSet.FindFirst "[Hire Date] <= #9/21/92#"


Additional reference words: 3.00 4.00 vb416
KBCategory: kbprg
KBSubcategory: APrgDataAcc
Keywords : APrgDataAcc kbprg
Version : 3.00 4.00
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 20, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.