vba - 在 Access 中使用文本框进行 VBA 搜索
问题描述
我对 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
解决方案
尝试这个:
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
推荐阅读
- sql-server - HOWTO:知道哪些记录被恢复到 SQL Server 表中
- java - 为什么我在 Vaadin / SpringBoot 应用程序中使用 @Autowired Repository 获得 NullPointerException?
- algorithm - 反向 LinkedList - 链表实现
- regex - How to remove Unicode characters in Snowflake?
- javascript - 查找文件夹中是否存在图片,如果不存在则渲染库存图像
- git - 可视化 git 分支依赖
- c# - How can I access the .zip file ML.NET creates, when I'm publishing to Google Cloud?
- jboss - 无法在 CDI @RequestScoped 托管 Bean 中注入 ExternalContext 或 RequestParam
- docker - How to use httpie and jq within docker?
- android - 如何在代码中使用 JAR?在 Android Studio 中很容易,但在 IntelliJ IDEA 中不起作用