Skip to content

Instantly share code, notes, and snippets.

@itsho
Created August 23, 2023 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsho/695f1503d33a588d6803335604236ea9 to your computer and use it in GitHub Desktop.
Save itsho/695f1503d33a588d6803335604236ea9 to your computer and use it in GitHub Desktop.
MSAccess VBA - Add Filter and FilterOnLoad to TableDef
Public Sub AddFilter(ByVal p_strNameOfRemoteTable As String, ByVal p_strWhereClause As String)
Dim db As Database
Dim td As TableDef
Dim propFilter As Object
Set db = CurrentDb()
Set td = db.TableDefs(p_strNameOfRemoteTable)
With td
' Enforce table to load data with filter (This property is promised to be exists)
.Properties("FilterOnLoad") = True
Dim objIterator As Object
Dim blnIsFilterPropExists As Boolean
blnIsFilterPropExists = False
For Each objIterator In .Properties
If (Not blnIsFilterPropExists And VarType(objIterator) = vbString And objIterator.Name = "Filter") Then
propFilter = objIterator
blnIsFilterPropExists = True
End If
Next objIterator
' If Filter property does not exist
If (Not blnIsFilterPropExists) Then
' Create filter with value
Set propFilter = .CreateProperty("Filter", dbText, p_strWhereClause)
.Properties.Append propFilter
Else
' Get filter property
Set propFilter = .Properties("Filter")
' set where filter
propFilter.Value = p_strWhereClause
End If
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment