|
-
Jul 27th, 2007, 11:18 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] SQL 'BETWEEN' not returning any results
My SQL statement sometimes returns some results and other times nothing.
There are records to display, but for some reason it's not finding them.
I get rs.EOF = true.
Code:
strSQL = "SELECT * FROM tbl_Finances"
strSQL = strSQL & " WHERE Finance_Date BETWEEN #" & dtDateOne & "# AND #" & dtDateTwo & "#"
debug.Print strsql
SELECT * FROM tbl_Finances WHERE Finance_Date BETWEEN #01/06/2007# AND #01/07/2007#
In my tset table, I should be returning 6 records.
The code that loads from the recordset ro the flexgrid works when all records are displayed, so i am guessing that that code is not the problem.
-
Jul 27th, 2007, 11:20 AM
#2
Re: SQL 'BETWEEN' not returning any results
If you run your SELECT form within Access do you get the results?
-
Jul 27th, 2007, 11:32 AM
#3
Re: SQL 'BETWEEN' not returning any results
Are you searching for data from January?
in SQL statements, the date format needs to be like "yyyy-mm-dd" or "mm/dd/yyyy". Using non-US date formats will work occasionally (when the dd part is 13 or more), and at other times be unpredictable.
-
Jul 27th, 2007, 11:40 AM
#4
Re: SQL 'BETWEEN' not returning any results
What if you change it from 07 to 08.... do you then get your records?
-tg
-
Jul 27th, 2007, 11:40 AM
#5
Thread Starter
Frenzied Member
Re: SQL 'BETWEEN' not returning any results
 Originally Posted by Hack
If you run your SELECT form within Access do you get the results?
Not quite sure how to do this. I will take a look at Access.
-
Jul 27th, 2007, 11:49 AM
#6
Thread Starter
Frenzied Member
Re: SQL 'BETWEEN' not returning any results
 Originally Posted by si_the_geek
Are you searching for data from January?
in SQL statements, the date format needs to be like "yyyy-mm-dd" or "mm/dd/yyyy". Using non-US date formats will work occasionally (when the dd part is 13 or more), and at other times be unpredictable.
This seems to be the problem here.
How do I reformat the date?
I have tried
FormattedDate(0) = Format(CDate(txtDate(0).Text), "yyyy,mm,dd")
which doesn't seem to do anything.
How about manipulate the text to swap around the month and day?
-
Jul 27th, 2007, 11:54 AM
#7
Thread Starter
Frenzied Member
Re: SQL 'BETWEEN' not returning any results
Hmmm...
If I Debug.Print this
FormattedDate(0) = Format(CDate(txtDate(0).Text), "yyyy,mm,dd")
I get this
2007,06,01
Yet if I put the cursor over the variable FormattedDate(0) it shows 01,06,2007
And I still get no records shown!
-
Jul 27th, 2007, 12:01 PM
#8
Re: SQL 'BETWEEN' not returning any results
Format the date field like this :
Code:
strSQL = "SELECT * FROM tbl_Finances"
strSQL = strSQL & " WHERE Finance_Date BETWEEN #" & Format(dtDateOne,"mm/dd/yyyy") & "# AND #" & Format(dtDateTwo,"mm/dd/yyyy") & "#"
Try that and see if you get any records (US date format)
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 27th, 2007, 12:08 PM
#9
Thread Starter
Frenzied Member
Re: SQL 'BETWEEN' not returning any results
 Originally Posted by GaryMazzone
Format the date field like this :
Code:
strSQL = "SELECT * FROM tbl_Finances"
strSQL = strSQL & " WHERE Finance_Date BETWEEN #" & Format(dtDateOne,"mm/dd/yyyy") & "# AND #" & Format(dtDateTwo,"mm/dd/yyyy") & "#"
Try that and see if you get any records (US date format)
Yeah, this returns what is expected each time. 
So what is the difference between me formatting it with
mm,dd,yyyy
to
yyyy,mm,dd
as the second didn't seem to work?
-
Jul 27th, 2007, 12:09 PM
#10
Re: SQL 'BETWEEN' not returning any results
What is FormattedDate declared as? If it is a Date data type, there is no point formatting the value you put into it - Date variables do not have formats, only values (the VB IDE automatically formats these values when displaying them to you).
Also, as you are converting from text the CDate and Format functions are likely to be doing things that you don't want - they make guesses as to what the current format of the text is, and convert based on that. It is best to avoid functions like that when converting from a text value to a date value - tho the Format function is ideal to convert from date to text.
The best thing to do is use a control which specifically deals with Date values (rather than text/string), such as the DateTimePicker. When you are then using the values in a string (such as an SQL statement) you should format them as part of adding them to the string (as in Gary's post).
-
Jul 27th, 2007, 12:13 PM
#11
Thread Starter
Frenzied Member
Re: SQL 'BETWEEN' not returning any results
I have changed my code to gary's now and it seems to be working.
But, in answer to your questions;
FormattedDate was declared as a Date value.
The text from the text box is validated as the format dd/mm/yyyy
-
Jul 27th, 2007, 12:15 PM
#12
Re: SQL 'BETWEEN' not returning any results
I think I would go with SI comment on using DateTimePickers for allowing the user to enter date data. (I always do). That way you are sure that it is a date and then will have no problem with formating.
If not I would use an IsDate() function before using the data and test it.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 27th, 2007, 12:21 PM
#13
Thread Starter
Frenzied Member
Re: SQL 'BETWEEN' not returning any results
Which component has the DateTimePickers ?
-
Jul 27th, 2007, 12:47 PM
#14
Re: SQL 'BETWEEN' not returning any results
They are in Common Controls 2, along with a MonthView control which is also useful for this kind of thing.
 Originally Posted by Gary
If not I would use an IsDate() function before using the data and test it.
Unfortunately IsDate is even worse - all it tells you is that somehow the value can be implicitly converted into a date (which is true for lots of values that we can easily tell aren't actually valid input).
-
Jul 27th, 2007, 01:06 PM
#15
Thread Starter
Frenzied Member
Re: SQL 'BETWEEN' not returning any results
I have now changed this to DateTimePickers, and along with Gary's code it all seems to be working now.
Thanks ALL
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
|