我是访问和vba的新手。现在我的第一个项目出了问题。我创建了一个表和一个带有关键字搜索的表单。我的关键字搜索工作正常,但如果我也使用来自一起访问的标准过滤器,我只能使用一列中的过滤器,因为所有其他过滤器值都不可用。我已经创建了一个msgbox来查看输出和访问,在表名之前给我一个下划线。
Public Sub btnSearch_Click()
Dim sql As String
sqlQuery = " brands.name LIKE '*" & Me.txtKeywords & "*' " _
& " OR brands.ID LIKE '*" & Me.txtKeywords & "*' "
sql = "SELECT brands.* " _
& " FROM brands " _
& " WHERE " & sqlQuery
Me.sfrmBrands.Form.RecordSource = sql
Me.sfrmBrands.Form.Requery
End Sub
和输出
MsgBox (Me.sfrmBrands.Form.Filter)
输出:([_brands].[name]="Test")
但我需要([brands].[name]="Test")
答案 0 :(得分:1)
应该是:
sqlQuery = " brands.name LIKE '*" & Me.txtKeywords & "*' " & _
" OR brands.ID LIKE '*" & Me.txtKeywords & "*' "
此外,您所做的只是设置子窗体的RecordSource,使其过滤器设置保持不变,因此您的消息框将只显示此过滤器。
设置过滤器:
Dim sql As String
Dim Filter As String
sql = "SELECT * FROM brands"
Filter = "[name] LIKE '*" & Me.txtKeywords & "*' " & _
"OR [ID] LIKE '*" & Me.txtKeywords & "*'"
Me.sfrmBrands.Form.RecordSource = sql
Me.sfrmBrands.Form.Filter = Filter
Me.sfrmBrands.Form.FilterOn = True
编辑:过滤前过滤:
Me.sfrmBrands.Form.RecordSource = sql
' and perhaps:
Me.sfrmBrands.Form.FilterOn = True
答案 1 :(得分:0)
我发现了问题,但我没有解决方法。
如果我按关键字搜索
Me.sfrmBrands.Form.RecordSource = sql
将表名从查询名称更改为子表单名称
还有其他解决方案
Me.sfrmBrands.Form.RecordSource = sql