Search code examples
vbams-accessms-access-2016

MS Access filtering for date causes Error 3075


I have a text field, called DateSelector that is formatted as a Short Date, and a subform with a list of entries, one column named ControlDate contains values that are formatted as Short Date as well.

What I want:
If you change the value of DateSelector it is supposed to filter the column ControlDate to between now and the value of DateSelector, or at least be below that of DateSelector.

This is the code I have:

Private Sub DateSelector_AfterUpdate()
On Error GoTo Proc_Error

If Me.DateSelector.Value = "" Then
   Me.ListView.Form.filter = ""
   Me.ListView.Form.FilterOn = False

Else
    MsgBox (Me.DateSelector.Value)
'This is a Check, if the Value is correct.
    Me.ListView.Form.filter = "ControlDate >=" & Me.DateSelector.Value
    Me.ListView.Form.FilterOn = True
End If
Proc_Exit:
   Exit Sub
Proc_Error:
   MsgBox "Error " & Err.Number & " when creating Filter:" & vbCrLf & Err.Description
   Resume Proc_Exit
End Sub

The error thrown is 3075: Syntax Error. The MsgBox shows the correct date, where the error message shows the date missing the last digit (e.g.: 05.12.2018 --> 05.12.201) and I have absolutely no clue why.

I am thankfull for any answers, thank you for reading,

_Ninsa


Solution

  • You filter will end up as:

    "ControlDate >= 01.12.2018"
    

    which Access cannot read. So, apply a proper format of the string expression of the date value:

    "ControlDate >= #" & Format(Me!DateSelector.Value, "yyyy\/mm\/dd") & "#"