|
-
Nov 4th, 2007, 03:37 AM
#1
Thread Starter
Fanatic Member
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
-
Nov 4th, 2007, 10:54 AM
#2
Frenzied Member
Re: Error in Query
What is the date Format you are passing to the querry? It should be MM/dd/yyyy.
-
Nov 4th, 2007, 12:15 PM
#3
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 & ")"
-
Nov 4th, 2007, 02:34 PM
#4
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...
-
Nov 4th, 2007, 02:49 PM
#5
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
-
Nov 5th, 2007, 06:54 AM
#6
Re: Error in Query
 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.
-
Nov 5th, 2007, 06:57 AM
#7
Re: Error in Query
 Originally Posted by zynder
Correct me if im wrong buddy.
You are right.
-
Nov 5th, 2007, 06:59 AM
#8
Lively Member
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 
-
Nov 5th, 2007, 07:01 AM
#9
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.
-
Nov 6th, 2007, 12:40 AM
#10
Thread Starter
Fanatic Member
-
Nov 6th, 2007, 12:46 AM
#11
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|