Results 1 to 2 of 2
  1. #1
    StevenBee is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    1

    SQL query with an optional date field

    I am trying to execute the following code:

    If Not IsNull(DLookup("EventName", "tbEVENT", _
    "EventName = " & qt & EventName_tb & qt & " and " _
    & "EventType = " & qt & EventType_cb & qt & " and " _
    & "EventDate = " & "#" & EventDate_tb & "#" & " and " _
    & "Venue_ID = " & VenueName_cb & " and " _
    & "EventDescription = " & qt & Description_tb & qt & " and " _
    & "StartTime = " & "#" & StartTime_tb & "#" & " and " _
    & "EndTime = " & "#" & EndTime_tb & "#" & " and " _
    & "Price = " & qt & Price_tb & qt & " and " _
    & "EventPictureURL = " & qt & EventPictureURL_tb & qt & " and " _
    & "EventPicturePosition = " & qt & EventPicturePosition_frm & qt)) Then
    It searches the database for the existence of a record which matches all of those conditions. All of the _tb & _cb's are textbox & controlbox controls on the form inputted by the user.



    The problem I'm having is that one of those dates, "EndTime" is optional, so if the user doesn't type anything, the control is empty and Dlookup searches for "EndTime = ##" and returns an error.

    The only way I've thought my way out of this is extremely awkward, such as putting two separate If statements dependent upon EndTime being blank or not, OR something just ugly like:

    if EndTime_tb = "" then
    tempstring = ""
    else
    tempstring = "EndTime = " & "#" & EndTime_tb & "#" & " and "
    endif

    If Not IsNull(DLookup("EventName", "tbEVENT", _
    "EventName = " & qt & EventName_tb & qt & " and " _
    & "EventType = " & qt & EventType_cb & qt & " and " _
    & "EventDate = " & "#" & EventDate_tb & "#" & " and " _
    & "Venue_ID = " & VenueName_cb & " and " _
    & "EventDescription = " & qt & Description_tb & qt & " and " _
    & "StartTime = " & "#" & StartTime_tb & "#" & " and " _
    & tempstring _
    & "Price = " & qt & Price_tb & qt & " and " _
    & "EventPictureURL = " & qt & EventPictureURL_tb & qt & " and " _
    & "EventPicturePosition = " & qt & EventPicturePosition_frm & qt)) Then
    Can anyone suggest a better option for me?

  2. #2
    khalid's Avatar
    khalid is offline MS-Access Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2010
    Location
    Kuwait
    Posts
    244
    so far this is acceptable solution

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

Similar Threads

  1. Replies: 7
    Last Post: 05-31-2011, 11:51 AM
  2. Compile Error: Argument Not Optional
    By bg18461 in forum Access
    Replies: 1
    Last Post: 12-01-2010, 08:47 AM
  3. Skipping optional arguments
    By canfish in forum Programming
    Replies: 3
    Last Post: 08-19-2010, 01:35 PM
  4. Replies: 2
    Last Post: 06-23-2010, 06:37 PM
  5. query to show gaps in a date field
    By Lockrin in forum Database Design
    Replies: 1
    Last Post: 05-28-2010, 10:48 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