使用组合框筛选拆分表单

时间:2013-12-30 20:56:56

标签: ms-access combobox access-vba

我在Access中使用Split Form,我有三(3)个下拉组合框: ReportedLOB(在此示例中使用) 帐户 StandCategory

这些组合框中的每一个都与所选的特定业务单位相关联,因此它限制了组合框中的项目数量。我在ReportedLOB中使用以下代码:

 Private Sub Combo_Reported_LOB_Selection_Change()

' If the combo box is cleared, clear the form filter.
  If Nz(Me.Combo_Reported_LOB_Selection.Text) = "" Then
  Me.Form.Filter = ""
  Me.FilterOn = False

' If a combo box item is selected, filter for an exact match.
' Use the ListIndex property to check if the value is an item in the list.
  ElseIf Me.Combo_Reported_LOB_Selection.ListIndex <> -1 Then
  Me.Form.Filter = "[ReportedLOB] = '" & _
                 Replace(Me.Combo_Reported_LOB_Selection.Text, "'", "''") & "'"
  Me.FilterOn = True
  End If
 End Sub

假设下拉列表中有4个项目:MCD,Comp,GRD,DRR 当我选择MCD时,它会正确过滤MCD结果。但是,在与团队一起审核后,他们想要删除MCD的过滤器以再次获得所有结果,因此我创建了一个额外的表,并将其加入到所有下拉项中以添加到列表中。

 SELECT DISTINCT dbo_ztblGAAP_Splits_TableA.ReportedLOB FROM
dbo_ztblGAAP_Splits_TableA WHERE (((dbo_ztblGAAP_Splits_TableA.BU)=[Forms]![Frm_Main]!
[Frm_Main_TextBox_Display_BU_Number_HIDDEN])) 
ORDER BY dbo_ztblGAAP_Splits_TableA.ReportedLOB
UNION ALL 
SELECT Top 10, "**ALL**" FROM  
dbo_tTbl_ADMIN_ForFiltering
ORDER BY ReportedLOB;

现在,百万美元的问题......当我从下拉列表中选择所有时,它会将我的所有记录更改为全部,而不是给我所有报告LOB的原始结果。有没有人处理过这个问题?我到处搜索试图将这些代码拼凑在一起。

1 个答案:

答案 0 :(得分:1)

现在您的组合有一个有效的行源查询,我建议您使用组合的After Update事件来驱动对表单.Filter属性的更改。

Dim strFilter As String

With Me.Combo_Reported_LOB_Selection
    If IsNull(.Value) Or .Value = "**ALL**" Then
        ' If the combo box is cleared or ALL selected, clear the form filter.
        Me.Filter = vbNullString
        Me.FilterOn = False
    Else
        ' item other than ALL is selected, filter for an exact match.
        strFilter = "[ReportedLOB] = '" & _
            Replace(.Value, "'", "''") & "'"
        Debug.Print strFilter ' check this in Immediate window in case of trouble
                              ' you can use Ctrl+g to go to the Immediate window
        Me.Filter = strFilter
        Me.FilterOn = True
    End If
End With

请注意,组合在After Update中没有焦点,因此其.Text属性不可用。所以我们改用.Value.Text属性仅在您更改值时才有用。当你进行这些更改时,组合会有所关注,因此可以使用.Text。几乎任何时候,使用.Value

如果您确实希望在每次组合击键时不断更改.Filter,则必须针对组合的Change事件调整上述代码。

相关问题