Results 1 to 4 of 4

Thread: SQL ? (WHERE clause and DATE format)

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Miami, FL
    Posts
    35

    SQL ? (WHERE clause and DATE format)

    I'm using a simple SQL query to return a RecordSet WHERE a field labeled DoctorID equals a string variable AND the ApptDateTime field equals a variable holding a Date. The query works when I omit the second WHERE clause (it only matches the DoctorID), so I know the error is either how I'm setting up my query with 2 WHERE clauses compounded, or how I'm referring to the date. The date passed to the variable matches the date in my database exactly though.

    Here's the code in my function:

    VB Code:
    1. Private Function DuplicateAppt(DocID As String, DateTime As Date) As Boolean
    2.     Dim strSQL As String
    3.     strSQL = "SELECT * FROM Appointments WHERE DoctorID ='" _
    4.         & DocID & "' AND ApptDateTime = %" & DateTime & "%;"
    5.     Call rsAppointments.Open(strSQL, cnSchedule, adOpenStatic, adCmdText)
    6.     DuplicateAppt = Not (rsAppointments.EOF)
    7.     rsAppointments.Close
    8.    
    9. End Function

    The value I'm passing to the DateTime argument works like this:

    VB Code:
    1. dtpDate.Value + TimeValue(dtpTime.Value)

    and it looks fine when I debug to check the value that gets passed.

    The error message I get when I run it is:

    Run-time error '-2147217900 (80040e14)
    Syntax error in query expression 'DoctorID = '1005' AND ApptDateTime = %11/27/2002 9:00:00 AM%'.

    Am I passing the date wrong? Am I doing my multiple WHERE clauses wrong? I've played around with both and still get similar error message every time.

    Thanks!
    Rich Stern
    http://www.AcidPlanet.com/Jasp182
    http://www.InsomniacRecords.com
    http://www.mp3.com/Jasp182

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    % is wrong should be either # or '. # in access, ' for SQL
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Miami, FL
    Posts
    35
    Dude...I love this place!

    I tried the ' symbol, but I didn't try the pound sign (#), and now it works like a charm.

    Thanks a million!
    Rich Stern
    http://www.AcidPlanet.com/Jasp182
    http://www.InsomniacRecords.com
    http://www.mp3.com/Jasp182

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    your welcome.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width