0

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.

9
  • 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 Commented Jan 7, 2015 at 12:39
  • :/ I read that a while ago! Commented Jan 7, 2015 at 12:41
  • 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)? Commented Jan 7, 2015 at 12:44
  • 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. Commented Jan 7, 2015 at 12:44
  • 1
    @MaciejLos, This is one of those just because you can doesn't mean you should things in my book. Commented Jan 7, 2015 at 16:43

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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')#"

2 Comments

I can't use that. VBA has " problems. Can you format it?
try between #" & Format([StartDate], "MM/dd/yyyy") & "# AND #" & Format([EndDate], "MM/dd/yyyy") & "#"
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.