0

注意:我对 VBA 很陌生。我正在尝试为我们的自定义过滤器创建一个功能,用户可以选择过滤一个或另一个。我认为用户需要选择所有三个字段、比较和标准以使其成为一个选择,然后当按下按钮时将生成另一个选择,依此类推......直到单击过滤器按钮。我有过滤器上的代码,到目前为止它确实有效。

在此处输入图像描述 这只是我迄今为止拥有的一个愚蠢的草稿 Private Sub cmdOr_Click()

    Dim selectOr As Integer        

这里我不确定如何将这三个字段设置为一个指标

    selectOr = Field & Comparison & Criteria

    If IsNull(selectOr) Then
        MsgBox "All three fields, Comparison and Criteria must be selected", vbOKCancel, "Filter"
    Else

在这里我想增加我的selectOr,以便用户可以选择尽可能多的Or选项

        selectOr 1
End Sub

我希望我很清楚,如果没有,请告诉我!谢谢!

到目前为止,这是我的过滤器按钮中的内容。注意:还有词典、收藏等模块用到。

Private Sub cmdOK_Click()
        Dim Filter As IFilter, filterString As String, filterComparison As String, FilterCriteria As String, sanitizedFilterCriteria As
    String, PrimaryFilter As String, primarySchema As String,
    primaryTable As String, primaryKey As String, targetTable As String,
    targetField As String, Target() As String, filterItem As
    ControlDefinition_Filter

            Me.Dirty = False

            'Gather data for the primary filter from the active form

            With XFormToFilter
                .ResetFilter
                Set .FilterBuilder.FormToFilter = FormToFilter

                PrimaryFilter = IIf(.BaseFilter = vbNullString, _
                                        .NullFilter, _
                                        .BaseFilter)
                primaryTable = .FilterTable
                primaryKey = .key
            End With

            With Me.RecordsetClone
                If .RecordCount > 0 Then
                    .MoveFirst
                    Do While Not .EOF
                        'Get the schema, table/view, and control targetted by the filter
                        Target = Split(.fields("Field").Value, ".")
                        targetTable = IIf(Left(Target(0), 6) = "SELECT", GetTableName(Target(0)), Target(0))
                        targetField = Target(1)

                        filterComparison = Nz(.fields("Comparison").Value, vbNullString)
                        FilterCriteria = Nz(.fields("Criteria").Value, vbNullString)

                        Set filterItem = filterDict.item(.fields("Field").Value)
                        If filterItem.HasCriteria Then 'Get the stored equivalent for the criterion if available
                            sanitizedFilterCriteria = filterItem.GetCriteriaValue(FilterCriteria)
                            If sanitizedFilterCriteria <> vbNullString Then _
                                FilterCriteria = sanitizedFilterCriteria
                        End If

                        filterString = _
                            CreateFilterString(.fields("FieldType"), targetField, targetTable, _
                                                filterComparison, FilterCriteria)
                        filterString = FilterRegExp(filterString)

                        If Filter Is Nothing Then
                            Set Filter = XFormToFilter.FilterBuilder
                            Filter.SetPrimaryFilter PrimaryFilter, primaryTable, primaryKey
                        End If

                        Filter.AddSubFilter "Filter" & .fields("ID"), _
                            filterString, targetTable, subformDict(targetTable)
                        .MoveNext
                    Loop
                End If
            End With

            If Not Filter Is Nothing Then
                Dim finalFilter As String
                Filter.OpenFilteredRecordset
                XFormToFilter.SetFoundRecords

                DoCmd.Close acForm, "Filter_Create_popup", acSaveNo
            End If
        'error handling is here   
 End Sub`
4

1 回答 1

1

我有一个带有有限数量组合框的功能搜索(所以我不会动态添加它们。看看它,这对你有帮助。

Dim ctrl As Control
Dim search_string As String
search_string = ""
For Each ctrl In Me.Controls
    If ctrl.ControlType = acComboBox Then
        If ctrl.value <> "" Then
            If Len(search_string) > 1 Then
                search_string = search_string & " AND "
            End If
            search_string = search_string & LCase(Replace(ctrl.value, " ", "_"))
            If ctrl.Column(1) = "Number" Then
                search_string = search_string & " Like '*" & CStr(Me.Controls("Cval" & Right(ctrl.Name, 1)).value) & "*'"
            ElseIf ctrl.Column(1) = "Date" Then
                search_string = search_string & "=#" & Me.Controls("Cval" & Right(ctrl.Name, 1)).value & "#"
            Else
                search_string = search_string & " LIKE '*" & Me.Controls("Cval" & Right(ctrl.Name, 1)).value & "*'"
            End If
        End If
    End If
Next ctrl
DoCmd.OpenForm "frmSearchResults", acNormal, , search_string

它循环遍历控件并为 OpenForm 事件构建过滤器字符串。在我的情况下,我的包含额外的代码来帮助处理数据类型。

于 2013-09-24T21:02:18.623 回答