Hi All
I am trying to query my data to get the top 1 record based on a todays date as part of the WHERE criteria but have found that if i use Date() no records are found when 1 should, if i put the date in manually it doesn't return records. This was getting very frustrating but then i just tried for the heck of it the date in MM/DD/YYYY format instead of the local UK format of DD/MM/YYYY and it works and returns the correct record.
Weird thing is if i create an ACTUAL query and run it directly it returns the right records so this seems to be an issue with doing it via VBA.
if i have to reformat the date to cope with this i will just wondering if there is a setting somewhere that i've missed, also i will need to run this query in UK and in Latin America so may need to cope with multiple date formats anyway
Thanks in advance
Glen
I am trying to query my data to get the top 1 record based on a todays date as part of the WHERE criteria but have found that if i use Date() no records are found when 1 should, if i put the date in manually it doesn't return records. This was getting very frustrating but then i just tried for the heck of it the date in MM/DD/YYYY format instead of the local UK format of DD/MM/YYYY and it works and returns the correct record.
Weird thing is if i create an ACTUAL query and run it directly it returns the right records so this seems to be an issue with doing it via VBA.
Code:
Todays date is 12th October 2021 - 12/10/2021
This returns 0 records when it should return 1
strConsDeliveriesSQL = "SELECT DISTINCT TOP 1 [FACT-Consignments].cons_id, [FACT-Deliveries].del_arrival_date,[FACT-Deliveries].del_id" & _
" FROM [FACT-Consignments] INNER JOIN [FACT-Deliveries] ON [FACT-Consignments].cons_id = [FACT-Deliveries].del_cons_id" & _
" WHERE [FACT-Consignments].cons_id = " & CStr(Me.cons_id) & " And [FACT-Deliveries].del_arrival_date >= #" & Cstr(Date) & "# ORDER BY [FACT-Deliveries].del_arrival_date DESC"
This returns 0 records when it should return 1
strConsDeliveriesSQL = "SELECT DISTINCT TOP 1 [FACT-Consignments].cons_id, [FACT-Deliveries].del_arrival_date,[FACT-Deliveries].del_id" & _
" FROM [FACT-Consignments] INNER JOIN [FACT-Deliveries] ON [FACT-Consignments].cons_id = [FACT-Deliveries].del_cons_id" & _
" WHERE [FACT-Consignments].cons_id = " & CStr(Me.cons_id) & " And [FACT-Deliveries].del_arrival_date >= #12/10/2021# ORDER BY [FACT-Deliveries].del_arrival_date DESC"
This works:
strConsDeliveriesSQL = "SELECT DISTINCT TOP 1 [FACT-Consignments].cons_id, [FACT-Deliveries].del_arrival_date,[FACT-Deliveries].del_id" & _
" FROM [FACT-Consignments] INNER JOIN [FACT-Deliveries] ON [FACT-Consignments].cons_id = [FACT-Deliveries].del_cons_id" & _
" WHERE [FACT-Consignments].cons_id = " & CStr(Me.cons_id) & " And [FACT-Deliveries].del_arrival_date >= #10/12/2021# ORDER BY [FACT-Deliveries].del_arrival_date DESC"
if i have to reformat the date to cope with this i will just wondering if there is a setting somewhere that i've missed, also i will need to run this query in UK and in Latin America so may need to cope with multiple date formats anyway
Thanks in advance
Glen