Forum Discussion
Access selecting specific date on a calendar
I think the issue may actually be coming from how txtSelectDate is being populated or interpreted rather than the Format() function itself. If Me.txtSelectDate is not holding a full valid date value internally, Access can sometimes substitute the current day when rebuilding the date with CDate().
You probably do not need to split the date into separate day/month/year variables at all. Try simplifying it like this instead:
Private Sub lblTue3_Click() Dim dSelectDate As Date dSelectDate = Me.txtSelectDate.Value DoCmd.OpenForm "frmThatContainsMyData", _ acNormal, , _ "[DateOnFormUsedForFilter]=#" & Format(dSelectDate, "yyyy-mm-dd") & "#", _ , acDialog End Sub
Using the ISO format (yyyy-mm-dd) is usually much safer in Access filters because it avoids regional date interpretation issues.
Also double check that txtSelectDate really contains the exact clicked date and not only the month/year display value from the calendar form.
I recently ran into a somewhat similar date parsing issue while testing some VBA utilities and date handling logic on one of my own small tool projects:
https://peptixcalc.com
Access date handling can be surprisingly tricky sometimes.
Thank you for your quick reply and help! I did use your advise and went through some of my previous code to make sure everything was in ISO format. How can you tell if txtSelectDate contains the exact clicked date? I don't think it does that, as the code you provided didn’t work.
I suspect my issues have something to do with the rest of the code I used to create the calendar; I'll post it in a reply along with some pictures of the layout.