【问题标题】:Prevent combo box selection to change text防止组合框选择更改文本
【发布时间】:2016-07-20 16:46:47
【问题描述】:

当您键入组合框时,我正在构建搜索。搜索和更新使用更改事件。

Private Sub ItemName_Change()

Dim strText, strFind, strSQL, strSelect, strWhere, strOrderBy As String
strText = Me.ItemName.Text

strSelect = "SELECT DISTINCT [ItemName] FROM ItemsMaster "
strWhere = "WHERE type = 'Sold Goods' "
strOrderBy = " ORDER BY [ItemName];"
If (Len(Trim(strText)) > 0) Then
    'Show the list with only those items containing the types letters
    strFind = "ItemsMaster.ItemName Like '"
    For i = 1 To Len(Trim(strText))
        If (Right(strFind, 1) = "*") Then
            strFind = Left(strFind, Len(strFind) - 1)
        End If
        strFind = strFind & "*" & Mid(strText, i, 1) & "*"
    Next

    strFind = strFind & "'"
    strSQL = strSelect & strWhere & "AND " & strFind & strOrderBy

    Me.ItemName.RowSource = strSQL

Else
    strSQL = strSelect & strWhere & strOrderBy
    Me.ItemName.RowSource = strSQL
End If

Me.ItemName.Dropdown


End Sub

但是当我尝试从下拉列表中选择项目时,它会更新文本并触发更改事件。 有什么方法可以让用户在列表中滚动而不用组合框更改文本框中的文本?

【问题讨论】:

  • 您是否尝试过通过按键而不是组合框的更改事件来运行它?

标签: ms-access vba


【解决方案1】:

您可以通过在事件处理程序开始时测试 listindex 是否大于 -1 来解决这个问题,如果是则退出,例如:

Private Sub ItemName_Change()
    If ItemName.ListIndex > -1 Then
        Exit sub
    End if

    ...
End Sub

我认为,尽管您很有可能完全不使用组合框,而是使用 (a) 文本框(在其中输入文本并触发更改事件)来模仿它的功能've write) & (b) 一个显示搜索结果的列表框 - 然后当用户选择一个项目时,此操作与条目控件解除关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2016-06-25
    • 2013-02-14
    • 1970-01-01
    相关资源
    最近更新 更多