MS访问过滤器连续形式

问题描述:

我有一个要求,使用输入或从组合框中选择的字符串过滤连续形式。下面是我试图用来捕获过滤器字符串的代码。会发生什么情况是,当文本被输入到列表中而不是在后面被捕获的字符串中时,反而会抛出错误,指示组合框是空的。MS访问过滤器连续形式

我在哪里可以放置此功能?我正在考虑将代码添加到Combobox_Selected事件中,但不会让用户能够输入任意关键字来进一步过滤表单的内容。

Private Sub txtUSPSKeySearch_Change() 
On Error GoTo Err_txtUSPSKeySearch_Change 
Dim searchStr As String 

       searchStr = txtUSPSKeySearch.Value 
        If (Not IsNull(searchStr) And Len(searchStr) > 1) Then 

        Else 

        ' Move the cursor to the end of the combo box. 
    Me.txtUSPSKeySearch.SetFocus 
    Me.txtUSPSKeySearch.SelStart = Len(Me.txtUSPSKeySearch.Value) 
       End If 

'Error Handling 
Exit_txtUSPSKeySearch_Change: 
    Exit Sub 
Err_txtUSPSKeySearch_Change: 
    If Err.Number = 5 Then 
     MsgBox "You must make a selection(s) from the list" _ 
       , , "Selection Required !" 
     Resume Exit_txtUSPSKeySearch_Change 
    Else 
     'Write out the error and exit the sub 
     MsgBox Err.Description 
     Resume Exit_txtUSPSKeySearch_Change 
    End If 
End Sub 

会发生什么事是,当文本输入到列表中,而不是字符串被夹在背部,一个错误,而不是抛出,表明组合框为空。

你宣称searchStr必须是字符串数据类型:

Dim searchStr As String 

txtUSPSKeySearch为null,则此任务将失败,因为NULL不是字符串数据类型。

searchStr = txtUSPSKeySearch.Value 

您可以使用Nz()功能给searchStr一个空字符串时txtUSPSKeySearch为Null:

searchStr = Nz(Me.txtUSPSKeySearch.Value, vbNullString) 

然后你可以改变从这个If声明...

If (Not IsNull(searchStr) And Len(searchStr) > 1) Then 

对此...

If Len(searchStr) = 0 Then 

你正在从txtUSPSKeySearch更改事件中完成所有这些。考虑使用before update事件来验证这个值,并且after update事件可以做任何其他的事情(应用过滤条件?)你想对一个有效的值做什么。