1

Morning all,

I am hoping this is a quick one. I have a query in Excel that requests records between two date points. It returns without errors but also returns unexpected values, seemingly because the date has been swapped around. I am working with the UK date format (dd/mm/yyy).

I have tried passing the date as a date type and a string type, as a datevalue and just the text. I have also tried passing it as yyyymmdd.

In the below example sFromDate is 06/04/2016 (6th April) and sToDate is 12/04/16 (12th April). Dates between June and December are being returned.

sSelectedArea = "ThisTMRacf"
sFromDate = DateValue(Sht_SpecificView.Range("D5"))
sToDate = DateValue(Sht_SpecificView.Range("F5"))

Set rex = db.OpenRecordset("SELECT eGain.RecordDate FROM [eGain], [Staff] 
WHERE eGain.Racf = Staff.Racf AND (Staff.TeamManager = '" & sSelectedArea & "' 
OR Staff.CSM = '" & sSelectedArea & "') 
AND eGain.RecordDate BETWEEN #" & sFromDate & "# AND #" & sToDate & "# ;")

Is there another way to pass the date so that it doesn't convert?

4
  • As an aside, old-style joins are never pretty and could bite you in the bottom further down the line Commented Apr 12, 2017 at 10:35
  • Appreciate the feedback, John. How would you restructure it? Commented Apr 12, 2017 at 11:15
  • FROM [eGain] inner join [Staff] on eGain.Racf = Staff.Racf WHERE... Commented Apr 12, 2017 at 11:47
  • Thanks John! Much appreciated. Commented Apr 12, 2017 at 14:00

2 Answers 2

2

Formatting the dates as yyyy/mm/dd worked. Example below.

sFromDate = Format(Sht_Dashboard.Range("D5"), "yyyy/mm/dd")
sToDate = Format(Sht_Dashboard.Range("F5"), "yyyy/mm/dd")
Sign up to request clarification or add additional context in comments.

Comments

1

Might have to be ruthless with this and break the dates down and rebuild...

sFromDateY = Year(DateValue(Sht_SpecificView.Range("D5")))
sFromDateM = Month(DateValue(Sht_SpecificView.Range("D5")))
sFromDateD = Day(DateValue(Sht_SpecificView.Range("D5")))

Then use

AND eGain.RecordDate BETWEEN DateSerial(" & sFromDateY &", "& sFromDateM &", "&sFromDateD &") ...

2 Comments

If it works I don't mind a bit of ruthlessness! Will try shortly and report back.
Thanks for the suggestion, John. I managed to utilise a simpler approach. I have posted the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.