大家好我需要帮助我在这个微软访问的vba代码中提供帮助。我试图使用vba按照我设置的方式过滤表中的数据到目前为止每当某个事件发生时,用户在字段中输入一个值来过滤该数据列,例如"名字"或"姓氏"。我的问题是每当我尝试过滤多个字段时,它都不会保存我过滤的前一个字段中的过滤数据。例如,如果我过滤了名字的数据 比如詹姆斯我会得到一张桌子上的所有名字都是詹姆斯的人,但是当我在这个案例中过滤姓氏时我使用贝尔只会找到姓贝尔的人而不是寻找名字的人"詹姆斯贝尔"因为我已经指定了名字。
Private Sub txtFirstName_Click()
'adds and input box to prompt the user to input the field
first = InputBox("Input Last Name")
'uses the filter feature to pick data source you want to filter such as FirstName
Me.Filter = "FirstName = """ & first & """"
**If Me.Filter = "" Then
Me.Filter = "FirstName = """ & first & """"
Else
Me.Filter = Me.Filter & " AND FirstName = """ & first & """"
End If**
FilterOn = True
End Sub
Private Sub txtLastName_Click()
last = InputBox("Input Last Name")
Me.Filter = "LastName = """ & last & """"
**If Me.Filter = "" Then
Me.Filter = "LastName = """ & last & """"
Else
Me.Filter = Me.Filter & " AND LastName = """ & last & """"
End If**
FilterOn = True
End Sub
答案 0 :(得分:1)
您正在更换两个例程中的过滤器。相反,你想保留旧的过滤器并附加“AND ...”它。确保在AND周围包含空格。如果没有当前过滤器,则需要跳过AND。
Private Sub txtFirstName_Click()
'adds and input box to prompt the user to input the field
first = InputBox("Input Last Name")
'uses the filter feature to pick data source you want to filter such as FirstName
If Me.Filter = "" Then
Me.Filter = "FirstName = """ & first & """"
Else
Me.Filter = Me.Filter & " AND FirstName = """ & first & """"
End If
FilterOn = True
End Sub
和
Private Sub txtLastName_Click()
last = InputBox("Input Last Name")
If Me.Filter = "" Then
Me.Filter = "LastName = """ & last & """"
Else
Me.Filter = Me.Filter & " AND LastName = """ & last & """"
End If
FilterOn = True
End Sub