【问题标题】:Searching Access List Boxes data as-you-type in MS access forms在 MS 访问表单中搜索访问列表框数据
【发布时间】:2012-02-09 20:07:37
【问题描述】:

我正在尝试实现搜索列表框数据,以便在键入时使用文本框字段填充。

我在网上阅读了一些文档和有用的材料,发现以下链接对实现我的要求很有用,因此我使用了几乎相同的代码,但最终遇到了问题。

http://www.opengatesw.net/ms-access-tutorials/Access-Articles/Search-As-You-Type-Access.html

我的表单中有一个“Primary_skill”列表框字段,其中包含 100 多个项目,我正在尝试实现数据以根据我在表单中输入 txt 框字段的搜索自动显示。

我在这里运行的问题,我无法在此处选择两个不同的搜索项。 (表单代码中的 Me.refresh 行出现了一些错误

详细示例:我想选择用户 primary_skills 既是“DB2”又是“SQL server”,因为我最初能够搜索并选择了 db2 的复选框,后来我更改了搜索 txt 我收到错误指向调试.refresh "on change" 事件中的行。

**Form search as-you-type List box code**

Private Sub btnClearFilter_Click()
'CODE FOR THE RED "X" BUTTON TO CLEAR THE FILTER AND SHOW ALL
On Error Resume Next
10      Me.txtsearch.Value = ""
20      txtSearch_Change
End Sub
Private Sub txtSearch_Change()
'CODE THAT HANDLES WHAT HAPPENS WHEN THE USER TYPES IN THE SEARCH BOX
Dim strFullList       As String
Dim strFilteredList   As String


10    If blnSpace = False Then
20      Me.Refresh 'refresh to make sure the text box changes are actually available to use

        'specify the default/full rowsource for the control
30      strFullList = "SELECT TEMP.Primary_Skill FROM TEMP;"

        'specify the way you want the rowsource to be filtered based on the user's entry
40      strFilteredList = "SELECT TEMP.Primary_Skill FROM TEMP WHERE [Primary_Skill] LIKE ""*" & Me.txtsearch.Value & _
                        "*"""

        'run the search
50      fLiveSearch Me.txtsearch, Me.Primary_Skill, strFullList, strFilteredList, Me.txtCount
60    End If

End Sub


Private Sub txtSearch_KeyPress(KeyAscii As Integer)
'NECESSARY TO IDENTIFY IF THE USER IS HITTING THE SPACEBAR
'IN WHICH CASE WE WANT TO IGNORE THE INPUT

10    On Error GoTo err_handle

20       If KeyAscii = 32 Then
30            blnSpace = True
40       Else
50            blnSpace = False
60       End If


70    Exit Sub
err_handle:
80    Select Case Err.Number
          Case Else
90          MsgBox "An unexpected error has occurred: " & vbCrLf & Err.Description & _
                vbCrLf & "Error " & Err.Number & "(" & Erl & ")"
100   End Select
End Sub
Private Sub txtSearch_GotFocus()
' USED TO REMOVE THE PROMPT IF THE CONTROL GETS FOCUS
10    On Error Resume Next
20      If Me.txtsearch.Value = "(type to search)" Then
30        Me.txtsearch.Value = ""
40      End If
End Sub
Private Sub txtSearch_LostFocus()
' USED TO ADD THE PROMPT BACK IN IF THE CONTROL LOSES FOCUS
10    On Error Resume Next
20      If Me.txtsearch.Value = "" Then
30        Me.txtsearch.Value = "(type to search)"
40      End If

End Sub


**Modsearach (Module Code):**

Option Compare Database
Option Explicit
'************* Code Start **************
' This code was originally written by OpenGate Software
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
' OpenGate Software    http://www.opengatesw.net

Function fLiveSearch(ctlSearchBox As TextBox, ctlFilter As Control, _
                      strFullSQL As String, strFilteredSQL As String, Optional ctlCountLabel As Control)
'==================================================================================
'  THIS FUNCTION ALLOWS YOU TO FILTER A COMBO BOX OR LIST BOX AS THE USER TYPES
'  ALL YOU NEED TO DO IS PASS IN THE CONTROL REFERENCE TO THE SEARCH BOX ON YOUR
'  FORM, THE LISTBOX/COMBO BOX YOU WANT TO FILTER, AND WHAT THE FULL AND FILTERED
'  SQL (ROWSOURCE) SHOULD BE.
'
'  ctlSearchBox       THE TEXTBOX THE USER TYPES IN TO SEARCH
'
'  ctlFilter          THE LISTBOX OR COMBOBOX ON THE FORM YOU WANT TO FILTER
'
'  strFullSQL         THE FULL ROWSOURCE YOU WANT TO DISPLAY AS A DEFAULT IF NO
'                     RESULTS ARE RETURNED
'
'  strFilteredSQL     THE FILTERED ROWSOURCE FOR THE LISTBOX/COMBOBOX; FOR EXAMPLE
'                     YOU WOULD WANT TO USE '...like ""*" & me.txtsearch.value & "*"""
'                     TO FILTER THE RESULTS BASED ON THE USER'S SEARCH INPUT
'
' ctlCountLabel       (OPTIONAL) THE LABEL ON YOUR FORM WHERE YOU WANT TO DISPLAY THE
'                     COUNT OF ROWS DISPLAYED IN THE LISTBOX/COMBOBOX AS THEY SEARCH
'=====================================================================================

'ADVANCED PARAMETERS - Change these constants to change the behaviour of the search
  Const iSensitivity = 1 'Set to the number of characters the user must enter before the search starts
  Const blnEmptyOnNoMatch = True 'Set to true if you want nothing to appear if nothing matches their search


10    On Error GoTo err_handle

          'restore the cursor to where they left off
20        ctlSearchBox.SetFocus
30        ctlSearchBox.SelStart = Len(ctlSearchBox.Value) + 1

40        If ctlSearchBox.Value <> "" Then
                 'Only fire if they've input more than two characters (otherwise it's wasteful)
50               If Len(ctlSearchBox.Value) > iSensitivity Then
60                   ctlFilter.RowSource = strFilteredSQL
70                   If ctlFilter.ListCount > 0 Then
80                       ctlSearchBox.SetFocus
90                       ctlSearchBox.SelStart = Len(ctlSearchBox.Value) + 1
100                   Else
110                     If blnEmptyOnNoMatch = True Then
120                      ctlFilter.RowSource = ""
130                     Else
140                      ctlFilter.RowSource = strFullSQL
150                     End If
160                   End If
170             Else
180               ctlFilter.RowSource = strFullSQL
190             End If

200        Else
210           ctlFilter.RowSource = strFullSQL
220        End If

            'if there is a count label, then update it
            'if there is a count label, then update it
'230         If IsMissing(ctlCountLabel) = False Then
'240           ctlCountLabel.Caption = "Displaying " & Format(ctlFilter.ListCount - 1, "#,##0") & " records"
'250         End If
260   Exit Function
err_handle:
270   Select Case Err.Number
    Case 91 'no ctlCountLabel
       'exit
280       Case 94 'null string
       'exit
290       Case Else
300         MsgBox "An unexpected error has occurred: " & vbCrLf & Err.Description & _
            vbCrLf & "Error " & Err.Number & vbCrLf & "Line: " & Erl
310   End Select


End Function
'   ***** Code End ******

知道我在这里缺少什么。谢谢!

【问题讨论】:

  • 那么你得到的错误是什么?
  • 错误是 - 运行时错误“3058”:索引或主键不能包含 NULL 值。
  • 我尝试创建一个简单的表单并观察到这种行为 - 我能够选择第一个值,但是当尝试在文本框中输入一个单词时,第二次表单试图保存调试指向的记录20 Me.Refresh '刷新以确保文本框更改实际上可以使用 有人对此问题有任何想法吗.. 谢谢你的回复克里斯 仅供参考。我的主要技能字段可以“允许多个值”

标签: ms-access ms-access-2007 vba


【解决方案1】:

首先,不要使用“允许多个值”。它只会让你头疼。如果你想使用一对多关系,你应该使用连接表。

编辑:您可以在列表框中允许多个值,但如果您想正确执行,您必须遍历它以查找值并将它们单独放入数据库中。所以我建议避免使用它,除非你知道自己在做什么或者愿意学习更多关于编码和 SQL 的知识。

话虽如此,问题看起来与您导入的代码无关,但与列表框本身有关:

列表框的控制源是什么?例如,如果列表框的控件源是单个列,则默认情况下它不能同时接受多个值。您必须在表单的源表中更改它。列表框需要与数据库匹配:如果数据库只允许一个值,列表框必须跟在后面,如果数据库允许多个,那么列表框也可以有。

当 Me.Refresh 运行时,它会导致列表框失去焦点,这意味着访问将尝试更新列表框的控制源。我的猜测是控制源不能接受多个值并导致观察到的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2011-12-19
    相关资源
    最近更新 更多