0

SQL database with dates. I have a list form. At the top of the form, I am trying to filter the data by Date Received. When it brings it into Access, DateReceived shows in yyyy-MM-dd format so I have the SQL data invisible and I have a formatted text box (txtDateReceived) which takes the SQL date and formats it into mm/dd/yyyy which is visible. The user will enter the date in the mm/dd/yyyy format OR pick from the calendar. I tried to apply the filter to DateReceived and to txtDateReceived, and can't get either to work. Here is my code (includes everything I tried):

   Private Sub TextDateR_AfterUpdate()
      'declare variables
      Dim sFilter As String
    
      'in this case, the ID is text so the ID value
      'needs to be wrapped in single quotes.
      sFilter = "[DateReceived]= " & Format(Me.TextDateR, "yyyy-MM-dd")
      'sFilter = "DateReceived = #" & Me.TextDateR & "#"
      'sFilter = "txtDateReceived =" & Me.TextDateR
      
      'assign the filter value,and turn filtering on
      Me.Filter = sFilter
      Me.FilterOn = True
      Me.Recalc
      Forms!frmSurplusList.Requery
      Forms!frmSurplusList.Repaint
      
      sFilter = ""
    End Sub

2 Answers 2

0

Figured it out! sFilter = "[DateReceived]= '" & Format(Me.TextDateR, "yyyy-MM-dd") & "'"

Sign up to request clarification or add additional context in comments.

Comments

0

You should create not a text date (that will be casted) but a date value expression using octothorpes:

sFilter = "[DateReceived] = #" & Format(Me.TextDateR, "yyyy-MM-dd") & "#"

2 Comments

When you link a SQL database to an Access frontend, date fields show up as text, not date.
That will only happen if you use DateTime2 and the old SQL Server ODBC driver. Use DateTime and a current SQL Server driver like version 18.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.