How to make listbox display search results based on input in form?

倖福魔咒の 提交于 2020-04-17 20:52:14

问题


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.

(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.

How would one go about that?


回答1:


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.

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

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

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

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

Regards,



来源:https://stackoverflow.com/questions/61227416/how-to-make-listbox-display-search-results-based-on-input-in-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!