2012-08-16 59 views
1

我有一個要求,使用輸入或從組合框中選擇的字符串過濾連續形式。下面是我試圖用來捕獲過濾器字符串的代碼。會發生什麼情況是,當文本被輸入到列表中而不是在後面被捕獲的字符串中時,反而會拋出錯誤,指示組合框是空的。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 

回答

0

會發生什麼事是,當文本輸入到列表中,而不是字符串被夾在背部,一個錯誤,而不是拋出,表明組合框爲空。

你宣稱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事件可以做任何其他的事情(應用過濾條件?)你想對一個有效的值做什麼。