Created
August 23, 2023 18:06
-
-
Save itsho/695f1503d33a588d6803335604236ea9 to your computer and use it in GitHub Desktop.
MSAccess VBA - Add Filter and FilterOnLoad to TableDef
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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