【问题标题】:VBA Search Using Text Box in Access在 Access 中使用文本框进行 VBA 搜索
【发布时间】:2021-02-03 09:26:59
【问题描述】:

我对 Access 和 VBA 都很陌生。我创建了一个搜索按钮,根据在不同组合框中选择的内容查找不同的项目。但是,我想添加另一个搜索条件,如果我在名为“txtNotes”的文本框中键入文本,我可以在“tbl_ContructionOrders”表中的名为“Notes”的表字段中查找看起来像匹配项的记录。匹配必须有点松散,因为每个人输入的注释都不同,并且希望使用此框来查找我们遇到类似问题的工作订单,并且可能有更简单的方法找到解决上述问题的方法。

这是我目前所拥有的,但它不起作用

Private Sub btnLookup_Click()
Dim strWhere As String


Me.Filter = True
strWhere = ""


    If IsNull(Me.txtNotes) Then
    
        If Not IsNull(Me.txtNotes) Then
        
            If strWhere <> "" Then
                strWhere = strWhere & " like [Notes] = """ & Me.txtNotes & """"
            
        Else
                strWhere = strWhere & "[Notes] = """ & Me.txtNotes & """"
             End If
         End If
         
         If Len(strWhere) <= 0 Then
        MsgBox "No Criteria", vbInformation, "No Input."
    Else
        Me.Filter = strWhere
        Me.FilterOn = True
        Me.Requery
    End If
    
    
    If Me.FilterOn Then
        If Me.Recordset.RecordCount = 0 Then
            MsgBox "Nothing Found."
        End If
        
    End If
    End Sub

【问题讨论】:

  • 您要搜索还是过滤?
  • 我想过滤与我在文本框中输入的内容有些匹配的记录

标签: vba ms-access ms-access-2010


【解决方案1】:

试试这个:

Private Sub Command0_Click()

    'empty notes
    If IsNull(txtNotes.Value) Then
        MsgBox "No Criteria", vbInformation, "No Input."
        Exit Sub
    End If
    
    'search
    Filter = "[Notes] Like '*" & txtNotes.Value & "*'"
    FilterOn = True
    
    'count records
    If RecordsetClone.RecordCount = 0 Then
        MsgBox "Nothing Found."
        FilterOn = False
    End If
    
End Sub

如果您想在键入时搜索(过滤),请使用:

Private Sub txtNotes_Change()

    'search after x number of chars
    If Len(txtNotes.Text) <= 3 Then
        FilterOn = False
        Filter = vbNullString
    Else
        Filter = "[Notes] Like '*" & txtNotes.Text & "*'"
        FilterOn = True
    End If
    
End Sub

【讨论】:

  • 我都试过了。第一个在我按下搜索按钮后没有显示任何结果,第二个给我一个运行时错误 2185
  • 第二个示例与第一个示例无关,您必须按原样使用它,您提到的错误在这种情况下没有意义。另外,您确定在第一个示例中使用了正确的事件处理程序btnLookup_Click()
【解决方案2】:

听起来你想要一个自动完成的对象。将要在表格中自动填充的单词突出。使用窗体上的组合框控件来选择单词。这是一个指导视频,向您展示如何设置。

https://www.youtube.com/watch?v=ptRb8ffv4f0

如果您需要其他内容,请回帖。

【讨论】:

  • 你好,阿什,感谢您的回复。我设法使用 Kostas K. 代码示例。问题是我使用的文本框。我删除了它,创建了一个新的,代码开始工作了。
猜你喜欢
  • 2011-11-22
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多