0
votes

Hello I'm using MS JET OLEDB 4.0 on VBA (MS ACCESS)

SQL = "SELECT COUNT(Date) FROM [Orders] WHERE [Orders].[Date] BETWEEN " & "#" & StartDate & "#" & " AND " & "#" & EndDate & "#"

The [Date] is formatted like f dd/mm/yyyy (French) and the StartDate and EndDate are formatted like mm/dd/yyyy (Two date objects).

My belief is that BETWEEN only compares dates formatted mm/dd/yyyy, so is there a way like a function to convert the [Date] formatting into mm/dd/yyyy so the between function can compare correctly?

Edit: Using a string instead of the dates like follow:

StartDateFormatted = Format(StartDate,"dd/mm/yyyy")
EndDateFormatted = Format(EndDate,"dd/mm/yyyy")

So as to be sure of the startdate and enddate format. It doesn't work still.

I'm left to assume two things:

  • Either BETWEEN onlys compares mm/dd/yyyy formats

  • I have to use a function to format [Date] to mm/dd/yyyy

Thanks to @Maciej Los for answering my question.

3
Don't know if it helps but found a piece of doc about a convert function: i.e. Format (Now, “MM/dd/yyyy”) udemy.com/blog/vba-format-date - Robert
:/ I read that a while ago! - Amen Jlili
However, since the columns should be date fields, the format shouldn't have any effect on the comparison, or am I wrong!? Is it possible that [Orders].[Date] is of another type (maybe a text)? - Robert
I would do a "select getDate()" and watch the format returned then use that format in the query. Another thing is that he formatting of the dates is a localization setting for the database. - Fabooo
@MaciejLos, This is one of those just because you can doesn't mean you should things in my book. - Tony Hopkinson

3 Answers

3
votes

Always use ISO date formatting YYYY-MM-DD and you will have no problems. Without formatting.

1
votes

Use Format function.

SQL = "SELECT COUNT(Date)" & vbcr & _
"FROM [Orders]" & vbcr & _
"WHERE [Orders].[Date] BETWEEN #Format(" & StartDate & "], 'MM/dd/yyyy')# AND #Format(" & EndDate & ",'MM/dd/yyyy')#"
1
votes

I have noticed from the SQL compilation of the query when using Between, that it turns it to MM/DD/YYYY and it did not work till I used Format(startdate, "mm/dd/yyyy"). Please check the snapshop.

enter image description here