【问题标题】:Microsoft Access - combo box dropdown errorsMicrosoft Access - 组合框下拉错误
【发布时间】:2015-11-07 02:03:42
【问题描述】:

我是 VBA/Access 的新手,所以我一直在使用我在网上找到的查询/VBA 代码来帮助我构建我当前的表单。我的组合框出现了奇怪的行为,我认为这可能是由于我附加的 VBA/查询所致。

下面的代码用于使组合框成为动态搜索工具:当您键入每个字母时,它会重新运行查询以仅使用与键入的字母匹配的姓氏来更新列表。

但是,按箭头键/tab/enter 没有任何作用。在下拉列表中导航/选择值的唯一方法是单击鼠标。当我单击我想要的值时,表单会填充该记录中的数据(耶!)但下拉菜单保持可见,直到我单击表单的背景。

我希望下拉菜单在我选择所需记录后立即消失,并且如果可能的话,我希望能够使用箭头键进行导航。

“On Change”事件有这个代码:

Private Sub Combo1397_Change()

Dim strText, strFind
Combo1397.SetFocus

strText = Me.Combo1397.Text

If Len(Trim(strText)) > 0 Then

strFind = "[Last Name] Like '"
For i = 1 To Len(Trim(strText))
    If (Right(strFind, 1) = "*") Then
        ' When adding another character, remove the
        ' previous "*," otherwise you end up with
        ' "*g**w*" instead of "*g*w*."
        ' This has no apparent impact on the user, but
        ' ensures that the SQL looks as intended.
        strFind = Left(strFind, Len(strFind) - 1)
    End If
    strFind = strFind & "*" & Mid(strText, i, 1) & "*"
Next
strFind = strFind & "'"  


strSQL = "SELECT tbl_RC_Main.pk_CandidateID, tbl_RC_Main.[Last Name],tbl_RC_Main.[First Name], tbl_RC_Main.Email FROM tbl_RC_Main Where " & _
strFind & " ORDER BY [Last Name];"


Me.Combo1397.RowSource = strSQL

Else
    ' Show the entire list.
    strSQL = "SELECT tbl_RC_Main.pk_CandidateID, tbl_RC_Main.[Last Name],tbl_RC_Main.[First Name], tbl_RC_Main.Email FROM tbl_RC_Main ORDER BY tbl_RC_Main.[Last Name]; "
    Me.Combo1397.RowSource = strSQL
End If

Me.Combo1397.Dropdown
End Sub

另外, 在“更新后”事件中,我有一个“搜索记录”宏

【问题讨论】:

    标签: ms-access combobox vba ms-access-2010


    【解决方案1】:

    因为您在Change 事件中有Me.Combo1397.Dropdown,所以在选择一个值时组合框将再次下拉。您应该改为将 Me.Combo1397.Dropdown 移动到 Enter 事件。

    如果您还删除了Combo1397.SetFocus(我不明白为什么需要它),那么您也可以使用 Enter/Tab/Arrow 键。

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2020-06-02
      • 1970-01-01
      相关资源
      最近更新 更多