Access 2013 - Embedded query filtered by combo box on form

时间:2015-09-01 21:22:48

标签: forms ms-access access-vba ms-access-2013

I'm new to Access and this is the problem I'm suffering: I have four tables - Task, Person, Role, and TaskPerson (mapping table). I have a form that at the top has a unbound combo box displaying a list of people from Person. In the body of the form I have a query pulling from the Task and TaskPerson tables that is embedded as a datasheet. The fields from TaskPerson perform a lookup on Person and Role to display the actual values. Each task can have multiple people assigned to it and each person can have multiple roles. I am looking to pick a name from the combo box with the datasheet updating to only show the tasks associated with that person (i.e. matching the name from the combo box to the name in the person field (which is a lookup) on the form and only showing those tasks).

I have tried adjusting the Record Source for the query so the person field criteria would pull from the combo box using

'Forms![Task Form]![Combo11]'

but that hasn't worked. I have also tried a version of this answer:

Private Sub Form_SelectionChange()

' If the combo box is cleared, clear the form filter.
  If Nz(Form) = "" 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.Combo11.ListIndex <> -1 Then
  Me.Form.Filter = "[Combo11] = '" & _
             Replace(Me.Combo11.Text, "'", "''") & "'"
  Me.FilterOn = True
  End If

End Sub

While the code is not balking, it also isn't grabbing the selected name from the combo box, so it doesn't update. A likely factor is when I type Me.Combo11.Text, it doesn't actually display Combo11 as an option. I tried typing it in, in hopes of working, but I know that is a bit foolish.

Any detailed answers would be appreciated. I'm still learning my way around and I get lost a bit easily.

Steve.

1 个答案:

答案 0 :(得分:2)

第一种方法更容易。
在查询中

WHERE TaskPerson = Forms![Task Form]![Combo11]

请注意&#39;围绕组合参考。使用'Forms![Task Form]![Combo11]',整个事物被解释为字符串,因此它不起作用。

然后在Combo11_AfterUpdate中你只需要

Me.Requery

此方法的缺点:您始终必须选择一个人,否则表单将为空。

第二种方法:

您的查询列出了所有记录,组合框应用了一个过滤器。如果用户清除组合框,则将其删除。

我建议您回到您使用的answer,并且只替换为 Combo_Reported_LOB_Selection Combo11的{​​{1}} 和
[ReportedLOB]的{​​{1}}

代码不会进入[TaskPerson],而是进入Form_SelectionChange()