在子窗体网格中键入以筛选选择的组合框

huangapple go评论52阅读模式
英文:

Typing in a Combobox to filter for selection in a subform grid

问题

我在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    '这是下拉框的默认行源
    End If
    combo.RowSource = strSQL
    combo.Dropdown
End Sub

Private Sub Select_Item_AfterUpdate()
    '重置下拉列表
    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
英文:

I have a form in ms access 2016 with a subform in datagrid view. I have a column that is a combobox and I am filtering the values as you type. It works for the first selection, but when after I make my selection and I want to either go to the next record and make another selection or change the selection I made in my current row, all the values disappear and are blank. This is my code:

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

答案1

得分: 0

经过研究和代码实验,我找到了这个解决方案,我根据我的应用程序进行了修改。下面的代码适用于我所需的情况:

' 

Option Compare Database

Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"

Public Sub FilterComboAsYouType(Filter As Boolean)
    Dim strSQL As String
    
    strSQL = RecordSQL    '这是组合框的默认行来源
    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
End Sub


Private Sub Form_Load()
    FilterComboAsYouType False
End Sub

Private Sub Select_Item_AfterUpdate()
    
    FilterComboAsYouType False
    
    '设置焦点在主窗体控件上
    'Me.Parent.Text17.SetFocus
    'Me.PoQty.SetFocus
    

End Sub

Private Sub Select_Item_Change()
    
    FilterComboAsYouType True

End Sub

Private Sub Select_Item_GotFocus()
    FilterComboAsYouType False
End Sub

Private Sub Select_Item_LostFocus()
    FilterComboAsYouType False
End Sub

Private Sub txtSerial_KeyUp(KeyCode As Integer, Shift As Integer)
    Me.Select_Item.SetFocus
    FilterComboAsYouType True
    'DoEvents
    Me.Select_Item.Dropdown
End Sub

' 

这是您提供的代码的翻译部分。

英文:

After researching and playing with code, I found this solution which I modified to my application. This code below worked for what I needed:

''' 

Option Compare Database

Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"

Public Sub FilterComboAsYouType(Filter As Boolean)
    Dim strSQL As String
    
    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
End Sub


Private Sub Form_Load()
    FilterComboAsYouType False
End Sub

Private Sub Select_Item_AfterUpdate()
    
    FilterComboAsYouType False
    
    'Set Focus on Mainform Control
    'Me.Parent.Text17.SetFocus
    'Me.PoQty.SetFocus
    

End Sub

Private Sub Select_Item_Change()
    
    FilterComboAsYouType True

End Sub

Private Sub Select_Item_GotFocus()
    FilterComboAsYouType False
End Sub

Private Sub Select_Item_LostFocus()
    FilterComboAsYouType False
End Sub

Private Sub txtSerial_KeyUp(KeyCode As Integer, Shift As Integer)
    Me.Select_Item.SetFocus
    FilterComboAsYouType True
    'DoEvents
    Me.Select_Item.Dropdown
End Sub

'''

huangapple
  • 本文由 发表于 2023年2月19日 22:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定