我在 ms access 2016 中有一个表单,在数据网格视图中有一个子表单。我有一个组合框列,我在您键入时过滤值。它适用于第一个选择,但是当我做出选择并且我想转到下一条记录并进行另一个选择或更改我在当前行中所做的选择时,所有值都会消失并且为空白。这是我的代码:
Option Compare Database
Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"
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
Private Sub Select_Item_AfterUpdate()
'reset dropdown list
Select_Item.RowSource = RecordSQL
Select_Item.Requery
Select_Item.Dropdown
Select_Item.SetFocus
End Sub
Private Sub Select_Item_Change()
FilterComboAsYouType Me.Select_Item, RecordSQL, "Description"
End Sub
在研究和玩代码之后,我找到了这个解决方案,我修改了我的应用程序。下面的代码可以满足我的需要:
'''
选项比较数据库
Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"
Public Sub FilterComboAsYouType(过滤为布尔值) 将 strSQL 调暗为字符串
strSQL = RecordSQL 'This is the default row source of combo box
Me.Select_Item.RowSource = strSQL
'Me.Select_Item.Requery
DoEvents
If Filter Then
DoEvents
strSQL = RecordSQL & " WHERE Description LIKE '*" & Me.Select_Item.Text & "*'"
Me.Select_Item.RowSource = strSQL
'Me.Select_Item.Requery
Me.Select_Item.Dropdown
'Forms![PurchaseOrders]![PO_Con Subform]!Select_Item.Refresh
'Forms![PurchaseOrders]![PO_Con Subform].Refresh
'Me.Repaint
End If
结束子
私有子Form_Load() FilterComboAsYouType 假 结束子
Private Sub Select_Item_AfterUpdate()
FilterComboAsYouType False
'Set Focus on Mainform Control
'Me.Parent.Text17.SetFocus
'Me.PoQty.SetFocus
结束子
私有子 Select_Item_Change()
FilterComboAsYouType True
结束子
私有子 Select_Item_GotFocus() FilterComboAsYouType 假 结束子
私有子 Select_Item_LostFocus() FilterComboAsYouType 假 结束子
Private Sub txtSerial_KeyUp(KeyCode As Integer, Shift As Integer) 我.Select_Item.SetFocus FilterComboAsYouType 真 '做事件 我.Select_Item.Dropdown 结束子
'''