如何使列表框根据表单中的输入显示搜索结果? [英] How to make listbox display search results based on input in form?

查看:55
本文介绍了如何使列表框根据表单中的输入显示搜索结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个名为"dbInventory"的表,其中带有"ID,InvName,InvQuantity,InvType"以及与这些列匹配的输入表单.

I got a table called "dbInventory" with "ID, InvName, InvQuantity, InvType" and a entry form matching these columns.

我要实现的目的是让列表框开始根据输入内容显示搜索结果.

What I'm trying to achieve is to have the listbox start displaying search results based on the input.

(我的ID"列中包含条形码,而不是自动编号)

(My ID column contains barcodes, not autonumbers)

因此,例如,如果我扫描条形码以查找已经放入表中的物品,那么我希望它立即出现在列表框中.

So for instance, if I scan a barcode for an item I already put in the table some other time, I would like it to appear on the listbox right away.

那怎么办?

推荐答案

您似乎想处理两种不同的输入方法-通过扫描条形码(这将为您提供整个条形码)或由用户键入条形码.

It seems like you are wanting to deal with two different methods of entry - either by scanning a barcode (which would give you the entire barcode) or by the user typing the barcode in.

我建议同时使用两个控件-一个文本框,用户可以在其中扫描条形码或在条形码的开头键入(并删除键入的数据),然后在其中找到匹配项的列表框显示.

I would suggest using two controls in tandem - a text box, where the user can either scan a bar code or else type in the start of the barcode (and delete typed in data), and then a list box where matches are displayed.

您可以使用文本框的Change事件获取.Text属性,并将其用作列表框的RowSource的基础:

You can use the text box's Change event to get the .Text property and use that as the basis of the list box's RowSource:

Private Sub txtSearch_Change()
    On Error GoTo E_Handle
    If Not (IsNull(Me!txtSearch.Text)) Then
        Me!lstInventory.RowSource = "SELECT ID, InvName, InvQuantity, InvType FROM dbInventory WHERE ID LIKE '" & Me!txtSearch.Text & "*' ORDER BY ID ASC;"
    Else
        Me!lstInventory.RowSource = "SELECT ID, InvName, InvQuantity, InvType FROM dbInventory ORDER BY ID ASC;"
    End If
sExit:
    On Error Resume Next
    Exit Sub
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "frmInventory!txtSearch_Change", vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume sExit
End Sub

我不确定将条形码扫描到文本框中是否会触发OnChange事件-应该这样做!

I'm not sure whether scanning a barcode into the text box will trigger the OnChange event - it should do!!

如果您现在使用2个不同的控件进行搜索(ID和名称上的部分匹配),则应使用一个小的过程来创建所需的ListBox的RowSource,然后从任一文本框的OnChange事件中调用它.像下面的代码这样的东西应该让您入门:

If you are now using 2 different controls to search (part matching on ID and Name) then you should use a small procedure that creates the RowSource of the ListBox as needed, and then call it from the OnChange event of either text box. Something like the code below should get you started:

Private Sub txtSearchID_Change()
    Call sSearchForInventory(Nz(Me!txtSearchID.Text, ""), Nz(Me!txtSearchName.Value, ""))
End Sub
Private Sub txtSearchName_Change()
    Call sSearchForInventory(Nz(Me!txtSearchID.Value, ""), Nz(Me!txtSearchName.Text, ""))
End Sub
Sub sSearchForInventory(strID As String, strName As String)
    On Error GoTo E_Handle
    Dim strSQL As String
    If Len(strID) > 0 Then
        strSQL = " AND ID LIKE '" & strID & "*' "
    End If
    If Len(strName) > 0 Then
        strSQL = strSQL & " AND InvName LIKE '" & strName & "*' "
    End If
    If Left(strSQL, 4) = " AND" Then
        strSQL = "WHERE " & Mid(strSQL, 6)
    End If
    Me!lstInventory.RowSource = "SELECT ID, InvName, InvQuantity, InvType FROM dbInventory " & strSQL & " ORDER BY ID ASC;"
    Me!lstInventory.Requery
sExit:
    On Error Resume Next
    Exit Sub
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "sSearchForInventory", vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume sExit
End Sub

请注意,您需要使用要更改的控件的.Text属性,但要使用另一个控件的.Value属性.

Notice that you need to use the .Text property of the control that is being changed, but the .Value property of the other control.

此致

这篇关于如何使列表框根据表单中的输入显示搜索结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆