Results 1 to 11 of 11

Thread: Error in Query

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Error in Query

    Hello i trying to get some results from my tables between 2 dates using VB 6 adn SQL Server 2000.

    i wrote the following query for this
    Code:
    MDateQry = " BETWEEN '" & TxtDtFrom & "' And '" & TxtDtTo & "'"
    
    RsGlt.Open "Select Code,Tdate from glt where Code = " & RsDrt.Fields("Code") & "  and tDate" & MDateQry, Cn, adOpenStatic, adLockOptimistic
    but ig gives the error :

    [Microsoft][odbc sql server driver][sql server] The Conversion of a char Data type to a datetime data type resulted in a out-of-range date time value.

    What is this problem?
    Please help me

  2. #2
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Error in Query

    What is the date Format you are passing to the querry? It should be MM/dd/yyyy.
    IIF(Post.Rate > 0 , , )

  3. #3
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Error in Query

    Try putting a parenthesis in case your confused.

    Code:
    "SELECT Code, Tdate FROM glt WHERE (tDate BETWEEN '" & TxtDtFrom & "' AND '" & TxtDtTo & "') " & _
                 "AND (Code = " & RsDrt.Fields("Code").Value & ")"

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Error in Query

    Try
    Code:
    sSql = "Select Code,Tdate from glt where Code = " & RsDrt.Fields("Code") & "  and tDate" & MDateQry
    Debug.Print sSql
    Occam's Razor...

  5. #5
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Error in Query

    Use date qualifiers (#) instead of text qualifiers ('). Also, make sure your inputs are valid dates. Finally, you should get in the habit of capitalizing SQL key words to make it easier to read and always end your statements with ";".
    Code:
    If IsDate(TxtDtFrom) And IsDate(TxtDtTo) Then
        MDateQry = "BETWEEN #" & TxtDtFrom & "# And #" & TxtDtTo & "#"
        RsGlt.Open "SELECT Code,Tdate FROM glt WHERE (Code = " & RsDrt.Fields("Code") & ") " & _
                   "AND (tDate " & MDateQry & ");"
    Else
        Debug.Print "Invalid input"
    End If

  6. #6
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Error in Query

    Quote Originally Posted by Comintern
    Use date qualifiers (#) instead of text qualifiers ('). Also, make sure your inputs are valid dates. Finally, you should get in the habit of capitalizing SQL key words to make it easier to read and always end your statements with ";".
    Code:
    If IsDate(TxtDtFrom) And IsDate(TxtDtTo) Then
        MDateQry = "BETWEEN #" & TxtDtFrom & "# And #" & TxtDtTo & "#"
        RsGlt.Open "SELECT Code,Tdate FROM glt WHERE (Code = " & RsDrt.Fields("Code") & ") " & _
                   "AND (tDate " & MDateQry & ");"
    Else
        Debug.Print "Invalid input"
    End If
    I don't agree with this. He is using SQL Server 2000 not Access. Using (#) will return an error. The correct way is using (') for BETWEEN statement. Correct me if im wrong buddy.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Error in Query

    Quote Originally Posted by zynder
    Correct me if im wrong buddy.
    You are right.

  8. #8
    Lively Member
    Join Date
    Sep 2002
    Location
    Cheshire, England
    Posts
    111

    Re: Error in Query

    Anytime I deal with dates in SQL I ALWAYS use Format("YYYYMMDD") to guarentee consistency regardless of platform settings. This has the added advanctage of making sure its always a string so the Between will always work with " ' "

    I've been stung too many times to risk leaving it to chance!!
    Background: I have many years VB, Access and SQL experience, and will offer help wherever I can.
    I'm on here because I'm learning vb.net, and appreciate any and all help getting me up to speed.
    cheers
    Ray

  9. #9
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Error in Query

    Thank you. Sometimes we should double check what we post or read the problem details first before posting. It may not be our intention but we could mislead them with wrong advice.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Re: Error in Query

    Thankyou ALL.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Re: Error in Query

    How to use CAST function in QUERY using SQL SERVER and VB

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