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

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

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

问题

我在MS Access 2016中有一个带有数据表视图子表的表单。我有一列是下拉框,我正在根据输入筛选值。它对于第一次选择有效,但在我进行选择后,我想要要么转到下一条记录并进行另一个选择,要么更改我在当前行中的选择时,所有的值都会消失并为空。这是我的代码:

  1. Option Compare Database
  2. Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"
  3. Public Sub FilterComboAsYouType(combo As ComboBox, defaultSQL As String, lookupField As String)
  4. Dim strSQL As String
  5. If Len(combo.Text) > 0 Then
  6. strSQL = defaultSQL & " WHERE " & lookupField & " LIKE '*" & combo.Text & "*'"
  7. Else
  8. strSQL = defaultSQL '这是下拉框的默认行源
  9. End If
  10. combo.RowSource = strSQL
  11. combo.Dropdown
  12. End Sub
  13. Private Sub Select_Item_AfterUpdate()
  14. '重置下拉列表
  15. Select_Item.RowSource = RecordSQL
  16. Select_Item.Requery
  17. Select_Item.Dropdown
  18. Select_Item.SetFocus
  19. End Sub
  20. Private Sub Select_Item_Change()
  21. FilterComboAsYouType Me.Select_Item, RecordSQL, "Description"
  22. 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:

  1. Option Compare Database
  2. Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"
  3. Public Sub FilterComboAsYouType(combo As ComboBox, defaultSQL As String, lookupField As String)
  4. Dim strSQL As String
  5. If Len(combo.Text) > 0 Then
  6. strSQL = defaultSQL & " WHERE " & lookupField & " LIKE '*" & combo.Text & "*'"
  7. Else
  8. strSQL = defaultSQL 'This is the default row source of combo box
  9. End If
  10. combo.RowSource = strSQL
  11. combo.Dropdown
  12. End Sub
  13. Private Sub Select_Item_AfterUpdate()
  14. 'reset dropdown list
  15. Select_Item.RowSource = RecordSQL
  16. Select_Item.Requery
  17. Select_Item.Dropdown
  18. Select_Item.SetFocus
  19. End Sub
  20. Private Sub Select_Item_Change()
  21. FilterComboAsYouType Me.Select_Item, RecordSQL, "Description"
  22. End Sub

答案1

得分: 0

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

  1. '
  2. Option Compare Database
  3. Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"
  4. Public Sub FilterComboAsYouType(Filter As Boolean)
  5. Dim strSQL As String
  6. strSQL = RecordSQL '这是组合框的默认行来源
  7. Me.Select_Item.RowSource = strSQL
  8. 'Me.Select_Item.Requery
  9. DoEvents
  10. If Filter Then
  11. DoEvents
  12. strSQL = RecordSQL & " WHERE Description LIKE '*" & Me.Select_Item.Text & "*'"
  13. Me.Select_Item.RowSource = strSQL
  14. 'Me.Select_Item.Requery
  15. Me.Select_Item.Dropdown
  16. 'Forms![PurchaseOrders]![PO_Con Subform]!Select_Item.Refresh
  17. 'Forms![PurchaseOrders]![PO_Con Subform].Refresh
  18. 'Me.Repaint
  19. End If
  20. End Sub
  21. Private Sub Form_Load()
  22. FilterComboAsYouType False
  23. End Sub
  24. Private Sub Select_Item_AfterUpdate()
  25. FilterComboAsYouType False
  26. '设置焦点在主窗体控件上
  27. 'Me.Parent.Text17.SetFocus
  28. 'Me.PoQty.SetFocus
  29. End Sub
  30. Private Sub Select_Item_Change()
  31. FilterComboAsYouType True
  32. End Sub
  33. Private Sub Select_Item_GotFocus()
  34. FilterComboAsYouType False
  35. End Sub
  36. Private Sub Select_Item_LostFocus()
  37. FilterComboAsYouType False
  38. End Sub
  39. Private Sub txtSerial_KeyUp(KeyCode As Integer, Shift As Integer)
  40. Me.Select_Item.SetFocus
  41. FilterComboAsYouType True
  42. 'DoEvents
  43. Me.Select_Item.Dropdown
  44. End Sub
  45. '

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

英文:

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

  1. '''
  2. Option Compare Database
  3. Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"
  4. Public Sub FilterComboAsYouType(Filter As Boolean)
  5. Dim strSQL As String
  6. strSQL = RecordSQL 'This is the default row source of combo box
  7. Me.Select_Item.RowSource = strSQL
  8. 'Me.Select_Item.Requery
  9. DoEvents
  10. If Filter Then
  11. DoEvents
  12. strSQL = RecordSQL & " WHERE Description LIKE '*" & Me.Select_Item.Text & "*'"
  13. Me.Select_Item.RowSource = strSQL
  14. 'Me.Select_Item.Requery
  15. Me.Select_Item.Dropdown
  16. 'Forms![PurchaseOrders]![PO_Con Subform]!Select_Item.Refresh
  17. 'Forms![PurchaseOrders]![PO_Con Subform].Refresh
  18. 'Me.Repaint
  19. End If
  20. End Sub
  21. Private Sub Form_Load()
  22. FilterComboAsYouType False
  23. End Sub
  24. Private Sub Select_Item_AfterUpdate()
  25. FilterComboAsYouType False
  26. 'Set Focus on Mainform Control
  27. 'Me.Parent.Text17.SetFocus
  28. 'Me.PoQty.SetFocus
  29. End Sub
  30. Private Sub Select_Item_Change()
  31. FilterComboAsYouType True
  32. End Sub
  33. Private Sub Select_Item_GotFocus()
  34. FilterComboAsYouType False
  35. End Sub
  36. Private Sub Select_Item_LostFocus()
  37. FilterComboAsYouType False
  38. End Sub
  39. Private Sub txtSerial_KeyUp(KeyCode As Integer, Shift As Integer)
  40. Me.Select_Item.SetFocus
  41. FilterComboAsYouType True
  42. 'DoEvents
  43. Me.Select_Item.Dropdown
  44. End Sub
  45. '''

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:

确定