女士访问:VBA 组合框
Posted
技术标签:
【中文标题】女士访问:VBA 组合框【英文标题】:Ms Access: VBA combobox 【发布时间】:2015-10-20 13:28:53 【问题描述】:我将以下 VBA 代码用于 MS Access 表单中的组合框
Private Sub ComboEmployee_AfterUpdate()
Dim myFilter As String
myFilter = "Select * from Filter_Employee where ([LastName] = iif('" & ComboEmployee & "'='(All)',[LastName],'" & ComboEmployee & "'))"
Me.Employee_subform.Form.RecordSource = myFilter
Me.Employee_subform.Form.Requery
End Sub
这很好,问题是我现在有一个组合框,它在组合框中包含多于一列(见图)
如果我使用上面的代码它不起作用..我应该如何调整我的 vba 代码,以便它在过滤时起作用。
【问题讨论】:
您能否更具体地谈谈“不起作用”的内容 【参考方案1】:由于您的代码在组合的 After Update 事件中,您可以将 SELECT
调整为组合值 --- 仅在组合值不等于时添加 WHERE
子句“(全部)”。
Private Sub ComboEmployee_AfterUpdate()
Dim myFilter As String
myFilter = "Select * from Filter_Employee"
If Me.ComboEmployee.Value <> "(ALL)" Then
myFilter = myFilter & " where [LastName] = '" & Me.ComboEmployee.Value & "'"
End If
Debug.Print myFilter '<- view this in Immediate window; Ctrl+g will take you there
Me.Employee_subform.Form.RecordSource = myFilter
End Sub
【讨论】:
谢谢@HansUp,所以我的代码看起来像这样Private Sub Combo11_AfterUpdate() Dim myFilter As String myFilter = "Select * from Employee_ComboBox" & _ " where Forms!Employee_subform!Combo11 = '(All)'" & _ " Or [LastName] = Forms!Employee_subform!Combo11" Debug.Print myFilter '<- view this in Immediate window; Ctrl+g will take you there Me.Employee_subform.Form.RecordSource = myFilter End Sub
但是我收到提示“输入表单的参数值!Employee_subform!Combo11”......关于我做错了什么的任何想法?谢谢
如果组合在子表单上,您必须通过其父表单(包含子表单的表单)引用它......就像这种模式:Forms!ParentForm!SubformControl!ComboName
但是,我将添加不同的方法回答,因为我怀疑它会更容易。
成功了,我发现我做错了什么.. :) 非常感谢。
不客气。抱歉,我没有早点发现更简单的方法。
:您好,很抱歉再次打扰您。过滤器在我创建的表单中工作得很好。但是现在我将表单链接到导航表单中,它不起作用..我收到“输入参数值 - 表单!Employee_form!Combo11”代码:Private Sub Combo11_AfterUpdate() Dim myFilter As String myFilter = "Select * from Filter_Employee" & _ " where Forms!Employee_Form!Combo11 = '(All)'" & _ " Or [LastName] = Forms!Employee_Form!Combo11" Debug.Print myFilter '<- view this in Immediate window; Ctrl+g will take you there Me.Employee_subform.Form.RecordSource = myFilter End Sub
以上是关于女士访问:VBA 组合框的主要内容,如果未能解决你的问题,请参考以下文章
使用带有 postgresql 表的 vba 填充组合框访问 2007