iPw*_*ech 6 ms-access vba combobox access-vba ms-access-2010
我想在Microsoft Access中创建一个简单的搜索类型组合框,如下图所示.
注意:上面的图片来自我想从这里实现的复杂实现
我的组合框命名了ctlSearch.使用Visual Basic,我想挂钩onChange事件,检测用户输入,从而完善可能的结果列表.是否有可能采用这种方法来实现搜索即用型组合框?
Vla*_*ado 12
这是我用来做过滤组合框输入的功能:
Public Sub FilterComboAsYouType(combo As ComboBox, defaultSQL As String, lookupField As String)
Dim strSQL As String
If Len(combo.Text) > 0 Then
strSQL = defaultSQL & " WHERE " & lookupField & " LIKE '*" & combo.Text & "*'"
Else
strSQL = defaultSQL 'This is the default row source of combo box
End If
combo.RowSource = strSQL
combo.Dropdown
End Sub
Run Code Online (Sandbox Code Playgroud)
将组合框 Auto Expand 属性设置为 False 并在 Change 事件中调用 Sub FilterComboAsYouType ,如下所示:
Private Sub cmbProductName_Change()
FilterComboAsYouType Me.cmbProductName, "SELECT * FROM Product", "ProductName"
End Sub
Run Code Online (Sandbox Code Playgroud)
您可以设置组合或列表框,如下所示:
SELECT ID,Hotel,Location FROM Sometable t
WHERE t.Hotel
LIKE "*" & Forms!YourForm!txtSearch.Text & "*"
ORDER BY t.Hotel
Run Code Online (Sandbox Code Playgroud)
然后在更改事件中重新查询组合或列表框。