I'm querying a table containing a date field that is defined as general date. Table contains records with dates in format dd/mm/yyyy ( which is standard for my location ) for 16/11/2024, 23/11/2024, 7/12/2024 and 14/12/2024. My sql works for 16/11/2024 and 23/11/2024, retrieving records as expected. Select for 7/12/2024 fails on the read with no records found. I think it is treating this as 12 July 2024.
I've researched use of format to explicitly define the format of the date. Built a sql select but keep getting a syntax error when it runs. My variable: Dim vFixtureDate as Date is calculated using general date field from another table.
SQL statement here.
sqlF = "select \* from fixtures where fixture_dt_short = #" & "Format(" & vFixtureDate & ", " & Chr(34) & "dd/mm/yyyy" & Chr(34) & ")#"
SQL looks good - msgbox shows a statement that looks correct with # at either end of the Format parameters. When it runs, dialog box contains "Syntax error in date in query expression. The displayed SQL statement in the dialog box contains ")'. at the end. Closing # is missing.
I'm querying a table containing a date field that is defined as general date. Table contains records with dates in format dd/mm/yyyy ( which is standard for my location ) for 16/11/2024, 23/11/2024, 7/12/2024 and 14/12/2024. My sql works for 16/11/2024 and 23/11/2024, retrieving records as expected. Select for 7/12/2024 fails on the read with no records found. I think it is treating this as 12 July 2024.
I've researched use of format to explicitly define the format of the date. Built a sql select but keep getting a syntax error when it runs. My variable: Dim vFixtureDate as Date is calculated using general date field from another table.
SQL statement here.
sqlF = "select \* from fixtures where fixture_dt_short = #" & "Format(" & vFixtureDate & ", " & Chr(34) & "dd/mm/yyyy" & Chr(34) & ")#"
SQL looks good - msgbox shows a statement that looks correct with # at either end of the Format parameters. When it runs, dialog box contains "Syntax error in date in query expression. The displayed SQL statement in the dialog box contains ")'. at the end. Closing # is missing.
Share Improve this question edited Nov 20, 2024 at 15:28 taller 19.1k2 gold badges8 silver badges23 bronze badges asked Nov 20, 2024 at 13:10 Mick KMick K 11 bronze badge 4 |2 Answers
Reset to default 0Use the ISO sequence and reduce it to:
sqlF = "select * from fixtures where fixture_dt_short = #" & Format(vFixtureDate, "yyyy\/mm\/dd/") & "#"
Make an independent Date format
In MS Access table data stored in date field can be retrieved in any format. The question is how we are using the date format. Some use month in left some use it in middle of the format with two digits numeric value. To make it independent make it 3-Alpha short month name(Jan, Feb, Mar...Dec). for example
Format(vFixtureDate, "Mmm-DD-YYYY")
or
Format(vFixtureDate, "DD-Mmm-YYYY")
Value from above (Nov-20-2024 or 20-Nov-2024) will give you the same resultset in SQL statement. Finally we can write
sqlF = "SELECT * FROM fixtures WHERE fixture_dt_short = #" & Format(vFixtureDate, "DD-Mmm-YYYY") & "#"
or
sqlF = "SELECT * FROM fixtures WHERE fixture_dt_short = #" & Format(vFixtureDate, "Mmm-DD-YYYY") & "#"
Implemented this code on MS Access 2016.
Thanks
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742354489a4428164.html
Debug.Print
to see the resultingsqlF
, and add this to the question. – Andre Commented Nov 21, 2024 at 0:19