Results 1 to 3 of 3
  1. #1
    access is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    28

    Compare date in a sql query

    I have a query inside a form as below



    stSql = "SELECT Customer.CUST_ID, Customer.WEIGHT, Customer.START_DATE, Customer.PROJECT " _
    & "FROM Customer WHERE " _
    & "(Customer.PROJECT='" & Comboproject & "') AND " _
    & "(Customer.WEIGHT = " & Nz(txtWeight, "NULL") & ") AND " _
    & "(Customer.START_DATE ='" & Nz(txtStartDate, "") & "') "

    I'm facing 2 issues with the above query
    1. There can be situations where txtWeight can be null, (WEIGHT field type is NUMBER in table). When there is no data entered in txtWeight, I'm getting error message.
    2. Also when data is selected from calendar into txtStartDate, i'm getting "data type mismatch ..." error. Do we need to parse the txtStartDate before assigning to Customer.START_DATE as i have seen some ex giving # before and after the date. But still its not working. Can any one please help me out.

  2. #2
    access is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    28
    Well, I have removed quotes for txtStartDate in my query and able to resolve the error as below:

    stSql = "SELECT Customer.CUST_ID, Customer.WEIGHT, Customer.START_DATE, Customer.PROJECT " _
    & "FROM Customer WHERE " _
    & "(Customer.PROJECT='" & Comboproject & "') AND " _
    & "(Customer.WEIGHT = " & Nz(txtWeight, "NULL") & ") AND " _
    & "(Customer.START_DATE =" & Nz(txtStartDate, "") & ") "

    But the query is not returning any record eventhough the entry exists in table.

  3. #3
    access is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    28
    I'm able to resolve the date issue as below:

    Dim strDate as String
    strDate = "#" & txtStartDate & "#"

    stSql = "SELECT Customer.CUST_ID, Customer.WEIGHT, Customer.START_DATE, Customer.PROJECT " _
    & "FROM Customer WHERE " _
    & "(Customer.PROJECT='" & Comboproject & "') AND " _
    & "(Customer.WEIGHT = " & Nz(txtWeight, "NULL") & ") AND " _
    & "(Customer.START_DATE =" & Nz(txtStartDate, "NULL") & ") "

    I'm still left with the weight, how can i accept null fields for Number field types?.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 02-26-2009, 11:31 AM
  2. Replies: 4
    Last Post: 01-29-2009, 02:43 AM
  3. Query (Date Question)
    By cillajones in forum Queries
    Replies: 1
    Last Post: 08-09-2008, 12:05 PM
  4. query date that is five years older than today
    By cpsummer in forum Queries
    Replies: 2
    Last Post: 09-26-2007, 02:31 PM
  5. Need to set date in query for automation
    By Valeda in forum Queries
    Replies: 0
    Last Post: 06-07-2006, 06:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums