ODSOFilter Object

Microsoft Office Visual Basic

ODSOFilter Object

ODSOFilters ODSOFilter

Represents a filter to be applied to an attached mail merge data source. The ODSOFilter object is a member of the ODSOFilters object.

Using the ODSOFilter object

Each filter is a line in a query string. Use the Column, Comparison, CompareTo, and Conjunction properties to return or set the data source query criterion. The following example changes an existing filter to remove from the mail merge all records that do not have a Region field equal to "WA".

Sub SetQueryCriterion()
    Dim appOffice As Office.OfficeDataSourceObject
    Dim intItem As Integer

    Set appOffice = Application.OfficeDataSourceObject
    appOffice.Open bstrConnect:="DRIVER=SQL Server;SERVER=ServerName;" & _
        "UID=user;PWD=;DATABASE=Northwind", bstrTable:="Employees"

    With appOffice.Filters
        For intItem = 1 To .Count
            With .Item(intItem)
                If .Column = "Region" Then
                    .Comparison = msoFilterComparisonNotEqual
                    .CompareTo = "WA"
                    If .Conjunction = "Or" Then .Conjunction = "And"
                End If
            End With
        Next intItem
    End With
End Sub
		

Use the Add method of the ODSOFilters object to add a new filter criterion to the query. This example adds a new line to the query string and then applies the combined filter to the data source.

Sub SetQueryCriterion()
    Dim appOffice As OfficeDataSourceObject

    Set appOffice = Application.OfficeDataSourceObject
    appOffice.Open bstrConnect:="DRIVER=SQL Server;SERVER=ServerName;" & _
        "UID=user;PWD=;DATABASE=Northwind", bstrTable:="Employees"

    With appOffice.Filters
        .Add Column:="Region", _
            Comparison:=msoFilterComparisonIsBlank, _
            Conjunction:=msoFilterConjunctionAnd
        .ApplyFilter
    End With
End Sub