John wrote:
Hi
I have a form with tow fields for dates in dd/mm/yyyy format. I am trying to
use the fields in a query's where clause as below;
"SELECT * " & _
"FROM Orders " & _
" WHERE Orders.[Delivery Date])>= #" & Format([Forms]![Batch
Invoices]![FromDate], "dd/mm/yyyy") & "# And Orders.[Delivery Date]<= #" &
Format([Forms]![Batch Invoices]![ToDate], "dd/mm/yyyy") & "# "
My problem is that query doe snot read the dates correctly and rather takes
them as in mm/dd/yyyy format i.e. if 01/07/2006 (dd/mm/yyyy) was entered,
query brings records for 07/01/2006 (mm/dd/yyyy).
What is the ideal way to deal with the dates in this case so the dates are
taken as dd/mm/yyyy by the query?
Thanks
Regards
If I had to deal with this I would ... probably ... maybe ... create a
public function that translated 12/07/2006 to #07/12/2006#.
Here is an example (there are many ways of doing this).
Public Function SQLStringFromDD MMYYYY(ByVal DDMMYYYY As String) As
String
Dim aOldParts() As String
Dim aNewParts(0 To 2) As String
aOldParts = Split(DDMMYYYY, "/")
aNewParts(0) = aOldParts(1)
aNewParts(1) = aOldParts(0)
aNewParts(2) = aOldParts(2)
SQLStringFromDD MMYYYY = _
"#" & Join(aNewParts, "/") & "#"
End Function
Then I would use the function to create my SQL as:
"SELECT * " & _
"FROM Orders " & _
" WHERE Orders.[Delivery Date])>=" _
& SQLStringFromDD MMYYYY([Forms]![Batch Invoices]![FromDate]) _
& " Orders.[Delivery Date]<=" _
SQLStringFromDD MMYYYY([Forms]![Batch Invoices]![ToDate])
If the Batch Invoices Form had a module I would modify this slightly to
"SELECT * " & _
"FROM Orders " & _
" WHERE Orders.[Delivery Date])>=" _
& SQLStringFromDD MMYYYY(Form_Bat ch_Invoices.Fro mDate) _
& " Orders.[Delivery Date]<=" _
SQLStringFromDD MMYYYY(Form_Bat ch_Invoices.ToD ate)
or if the code is run in the form module to
"SELECT * " & _
"FROM Orders " & _
" WHERE Orders.[Delivery Date])>=" _
& SQLStringFromDD MMYYYY(Me.FromD ate) _
& " Orders.[Delivery Date]<=" _
SQLStringFromDD MMYYYY(Me.ToDat e)
or
"SELECT * " & _
"FROM Orders " & _
" WHERE Orders.[Delivery Date])>=" _
& SQLStringFromDD MMYYYY(FromDate ) _
& " Orders.[Delivery Date]<=" _
SQLStringFromDD MMYYYY(ToDate)
This all assumes you are using Access >= 2000 and that FromDate and
ToDate are strings in the format dd/mm/yyyy
Actually I wouldn't do this at all. I would provide the users with some
sort of calendar or drop down to choose the date ... but that is
another story.
As I just typed the SQL there may be syntax errors of course. The
function !!!!should!!!! be OK.