简体   繁体   English

MS Access 快速组合框与 VBA

[英]MS Access fast Combo Box with VBA

I have a form which has a ComboBox on it that pulls records via ID and displays Name from a linked table.我有一个表单,上面有一个 ComboBox,它通过 ID 提取记录并从链接表中显示名称。 Standard look for values in the form combo box wizard generated.标准在生成的表单组合框向导中查找值。 It works perfectly fine, but it takes 3-4 minutes every time to find a single record.它工作得很好,但每次找到一条记录需要 3-4 分钟。

I've been trying to research this and found something that looks useful, but can't seem to get it right.我一直在尝试对此进行研究,并发现了一些看起来很有用但似乎无法正确处理的东西。

The code I have at the moment:我目前拥有的代码:

 Private Sub Combo81_Change()
 Dim strText As String
 Dim strSelect As String
 strText = Nz(Me.Combo81.Text, "")
 If Len(strText) > 2 Then
 strSelect = "SELECT Name FROM CTable WHERE Name LIKE '*" & strText & "*'; "
 Debug.Print strSelect
 Me.Combo81.RowSource = strSelect
 Me.Combo81.Dropdown
 End If
 End Sub

I found this code on two forums, this is supposed to do the following: "the key is to not have a Row Source defined for the Combo Box. The row source will be defined as the user starts typing letters. Once they get to 3 letters then the row source of the combo box will be defined and the combo box will be told to dropdown."我在两个论坛上找到了这段代码,这应该执行以下操作:“关键是没有为组合框定义行源。行源将在用户开始输入字母时定义。一旦他们达到 3字母然后组合框的行源将被定义,组合框将被告知下拉。”

When I get to 3 letters, a dropdown appears, but it's blank, it doesn't display any results.当我达到 3 个字母时,会出现一个下拉列表,但它是空白的,它不显示任何结果。

I would like when the user types, eg "Smith" only those people with the name Smith come up.我想当用户键入时,例如“Smith”,只有那些名为 Smith 的人出现。

I'm relatively new to Access and the DB I'm using the FE/BE with linked tables to a shared network folder and FE on users Desktops.我对 Access 和数据库比较陌生,我正在使用 FE/BE 将表链接到共享网络文件夹和用户桌面上的 FE。

Any advice?有什么建议吗? Or alternatively a different solution as to how take my combo box faster and still keep values unique?或者关于如何更快地使用我的组合框并且仍然保持值唯一的不同解决方案?

组合框属性

you can use following codes to search value in a combo-box in ms access as user type, suppose we have a combo-box name org_id in our form, for search a value in org_id we need three event on org_id combo-box.您可以使用以下代码作为用户类型在 ms 访问的组合框中搜索值,假设我们的表单中有一个组合框名称 org_id,要在 org_id 中搜索值,我们需要 org_id 组合框上的三个事件。 AfterUpdate, LostFocus and KeyPress events. AfterUpdate、LostFocus 和 KeyPress 事件。 codes are:代码是:

Dim strFilter As String ' Module scope variable used for filter on our combo (org_id)

Private Sub org_id_AfterUpdate()
    strFilter = ""
    strSQL = "SELECT org_tbl.org_id, org_tbl.org_name, org_tbl.org_code FROM org_tbl" & _
             " ORDER BY org_tbl.org_code"
    org_id.RowSource = strSQL
    
End Sub

Private Sub org_id_LostFocus()
    strFilter = ""
    strSQL = "SELECT org_tbl.org_id, org_tbl.org_name, org_tbl.org_code FROM org_tbl" & _
             " ORDER BY org_tbl.org_code"
    org_id.RowSource = strSQL
End Sub

Private Sub org_id_KeyPress(KeyAscii As Integer)
    strSQL = "SELECT org_tbl.org_id, org_tbl.org_name, org_tbl.org_code FROM org_tbl ORDER BY org_tbl.org_code"
    
    If KeyAscii <> 8 Then ' pressed key is not backspace key
        strFilter = strFilter & Chr(KeyAscii)
    End If
    
    If IsNull(strFilter) = True Or strFilter <> "" Then
        If KeyAscii = 8 Then ' pressed key is backspace key
            strFilter = Left(strFilter, (Len(strFilter) - 1))
        End If
    End If
    
    strSQL = "SELECT org_tbl.org_id, org_tbl.org_name, org_tbl.org_code FROM org_tbl" & _
             " WHERE org_name Like '*" & strFilter & "*' ORDER BY org_tbl.org_code"
    org_id.RowSource = strSQL
    org_id.Dropdown
End Sub

I hope this (answer) helps you.我希望这个(答案)对你有帮助。

edit: you can download sample file from following link: Access combo box to search as you type sample file编辑:您可以从以下链接下载示例文件:访问组合框以在键入示例文件时进行搜索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM