Results 1 to 12 of 12

Thread: Selecting records between two dates

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    86

    Selecting records between two dates

    I want to select records between two dates from a table.

    StartDate and EndDate are two Dates but the code below does not seem to work. I get an operator/operand type mismatch.

    rstChkdisb.Open "select * from mytable where ck_date >= " & StartDate & " And ck_date <= " & EndDate & " ORDER BY ck_no", Cnn2, adOpenStatic, adLockReadOnly

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    Try this:
    rstChkdisb.Open "select * from mytable where ck_date >= '" & StartDate & "' And ck_date <= '" & EndDate & "' ORDER BY ck_no", Cnn2, adOpenStatic, adLockReadOnly
    with a ' befor and after the dates. And it is better to use between:

    rstChkdisb.Open "select * from mytable where ck_date between '" & StartDate & "' '" & EndDate & "' ORDER BY ck_no", Cnn2, adOpenStatic, adLockReadOnly

  3. #3
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Re: Selecting records between two dates

    Originally posted by IrelandsOwn
    I want to select records between two dates from a table.

    StartDate and EndDate are two Dates but the code below does not seem to work. I get an operator/operand type mismatch.

    rstChkdisb.Open "select * from mytable where ck_date >= " & StartDate & " And ck_date <= " & EndDate & " ORDER BY ck_no", Cnn2, adOpenStatic, adLockReadOnly
    VB Code:
    1. rstChkdisb.Open "SELECT * FROM mytable WHERE ck_date BETWEEN #" & StartDate & "# AND #" & EndDate & "# ORDER BY ck_no", Cnn2, adOpenStatic, adLockReadOnly

    Cheers...

  4. #4
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by Lightning
    Try this:
    rstChkdisb.Open "select * from mytable where ck_date >= '" & StartDate & "' And ck_date <= '" & EndDate & "' ORDER BY ck_no", Cnn2, adOpenStatic, adLockReadOnly
    with a ' befor and after the dates. And it is better to use between:

    rstChkdisb.Open "select * from mytable where ck_date between '" & StartDate & "' '" & EndDate & "' ORDER BY ck_no", Cnn2, adOpenStatic, adLockReadOnly
    when u r dealing with date u need use # instead of '

    Cheers...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    86
    I have tried this but am now getting a syntax error.

  6. #6
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Show us what you got till now.

    to select between two dates you can use the between operator instead.

    like wrack mentioned
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  7. #7
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by IrelandsOwn
    I have tried this but am now getting a syntax error.
    post the code u have here and use vbcode tags...

    Cheers...

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    86
    PHP Code:
    rstChkdisb.Open "SELECT * FROM mytable WHERE ck_date BETWEEN #" StartDate "# AND #" EndDate "# ORDER BY ck_no"Cnn2adOpenStaticadLockReadOnly 

  9. #9
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by IrelandsOwn
    PHP Code:
    rstChkdisb.Open "SELECT * FROM mytable WHERE ck_date BETWEEN #" StartDate "# AND #" EndDate "# ORDER BY ck_no"Cnn2adOpenStaticadLockReadOnly 
    What error r u getting...

  10. #10
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    This we knew.

    Can you change your code like this
    VB Code:
    1. strSQL = "SELECT * FROM mytable WHERE ck_date BETWEEN #" & StartDate & "# AND #" & EndDate & "# ORDER BY ck_no"
    2.  
    3. Debug.Print strSQL
    4. '
    5. rstChkdisb.Open strSQL, Cnn2, adOpenStatic, adLockReadOnly

    And show us what is printed in the immediate pane
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    86
    Run Time Error '-2147217900 (80040e14)':

    [Microsoft][ODBC Visual FoxPro Driver] Syntax Error

  12. #12
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    try the ' in stead of the #.Maybe that will 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
  •  



Click Here to Expand Forum to Full Width