Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Posts
    989
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Where clause for dates (2003)

    I'm looking for a nice/ simple expression to create part of a where clause relating to dates. I've two text boxes txtEarliest and txtLatest. If both are not null I will create the clause between ... and .... If the earliest is null I want to use the date 01/01/1990; if the latest is null I want to assume Date(). If both are null I want to ignore them (and therefore show all dates). I've created a (messy) If.. Then.. ElseIf structure but I feel sure that this has been encountered before, if not frequently. Andy.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Where clause for dates (2003)

    You can do something like this:

    Dim strWhere As String

    If Not IsNull(Me.txtEarliest) Then
    strWhere = strWhere & " AND DateField >= #" & Format(Me.txtEarliest, "mm/dd/yyyy") & "#"
    End If

    If Not IsNull(Me.txtLatest) Then
    strWhere = strWhere & " AND DateField <= #" & Format(Me.txtLatest, "mm/dd/yyyy") & "#"
    End If

    If Not strWhere = "" Then
    strWhere = Mid(strWhere, 6)
    End If

    Replace DateField with the name of the date field.

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Posts
    989
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Where clause for dates (2003)

    Thanks once again. I would ask what the Mid function does, but I'll create a function and test it to discover. Andy.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Where clause for dates (2003)

    You can type Mid in the Visual Basic Editor and press F1 to get help.

Posting Permissions

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