Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA where clause with a date between two dates

Status
Not open for further replies.

InspectorHound

Programmer
Nov 21, 2014
48
US
Hi,

I am building an SQL query string and I need to add a part that restricts field value to be between a start and an end date. Is this right:
If Not (IsNull(Me.txtStart) Or IsNull(Me.txtEnd)) Then
strSQL = strSQL & " AND [Authorized Travel Begin Date]>=" & Format (Me.txtStart, "dd/mm/yyyy") & _
"[Authorized Travel Begin Date]<=" & Format(Me.txtEnd, "dd/mm/yyyy")

End If
 
Hi,

Hi,

You were missing an AND, and your Format()s, returning ambiguous strings needed to be converted to DATEs, hence the unambiguous string and the conversion ## delimiters.

Code:
strSQL = strSQL & " AND [Authorized Travel Begin Date]>=#" & Format (Me.txtStart, "yyyy/mm/dd") & _
"# AND [Authorized Travel Begin Date]<=#" & Format(Me.txtEnd, "yyyy/mm/dd") &"#"
 
You may also try this:

Code:
strSQL = strSQL & " AND [Authorized Travel Begin Date] BEETWEN #" & Format (Me.txtStart, "yyyy/mm/dd") & _
"# AND #" & Format(Me.txtEnd, "yyyy/mm/dd") & "#"  

Debug.Print strSQL

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
I prefer Andy's method with a small correction to the spelling of BEETWEN
Code:
strSQL = strSQL & " AND [Authorized Travel Begin Date] BETWEEN #" & Format (Me.txtStart, "yyyy/mm/dd") & _
"# AND #" & Format(Me.txtEnd, "yyyy/mm/dd") & "#"  

Debug.Print strSQL

Duane
Hook'D on Access
MS Access MVP
 
Hi,

Yes, this works -- thanks. But no data is returned on my form. What could be the problem -- do I have to format the [Authorized Travel Begin Date] variable?
 
Is your field [tt]Authorized Travel Begin Date[/tt] declared as Date in your data base? Or is it a text / string?

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
It is data type "Short Text" in the database table[highlight #8AE234][/highlight]
 
When you have this ‘Short Text’ field and any Date field, the data in them may look the same to you, but it is treated totally different. So when Duane says: “convert it to a date” he means ‘convert your Short Text data field to a Date type in your data base and then convert your data to a Date’

You wouldn’t keep LastName or FirstName data in the field defined as Number, right?
:)

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Of course, I understand that. I think my problem is a space issue. I don't think it is doing the conversion. I will keep playing around with it.
 
Space issue? Dates are just simple numbers, text takes a lot more space. Computers are a lot better with numbers than with text.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
When I tried to convert it, I got such a message. I started from the beginning, re-imported the table, this time setting the field type, and now it works, using the same code as before. It never worked when I was trying to convert the date at any other point.

Thanks for your help.
 
setting the field type" to Date?
"now it works, using the same code as before" which is....?

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Initially, I was trying to use CDate. I first used it in the select part of the query that was the recordsource for the form. When I later used that variable to test the date range, nothing would happen.
The only way I for the code to work was when I re-imported this very huge table and set the field type to "Date/Time" when I did the import.
 
If your "date" strings are DMY, I seriously doubt that CDate() will work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top