0

I have created a button to filter records to be below a certain number AND with a specific category. I can't get it to work.

The code I have used is:

Private Sub KIAResearch_Click()
Dim strsc As String
strsc = "Killed in Action"
Me.Filter = "WikiDegrees BETWEEN " & 1 & " AND " & 8 & " And Service Category=" & strsc
Me.FilterOn = True
End Sub
Andre
  • 26,751
  • 7
  • 36
  • 80

1 Answers1

1

Have a look here: How to debug dynamic SQL in VBA

Avoid column names with spaces - if you must use them, you need square brackets in SQL: [Service Category].

To safely concatenate variables with SQL, use Gustav's CSql() function. It handles strings (which need quotes) and other variables.

So:

strFilter = "WikiDegrees BETWEEN " & 1 & " AND " & 8 & " And [Service Category]=" & CSql(strsc)
Debug.Print strFilter
Me.Filter = strFilter
Andre
  • 26,751
  • 7
  • 36
  • 80