您需要使用正在编辑的控件的.Text 属性和其他控件的.Value 属性创建一个动态构建行源的小函数。您构建的 SQL 将使用 LIKE 和通配符。该过程将类似于:
Private Sub sFindData(strDocument As String, dtmDate As Date, strPlatform As String)
On Error GoTo E_Handle
Dim strSQL As String
If Len(strDocument) > 0 Then
strSQL = strSQL & " AND DocumentNumber LIKE '*" & strDocument & "*' "
End If
If (IsDate(dtmDate)) And (dtmDate <> #12/31/2099#) Then
strSQL = strSQL & " AND DocumentDate=" & Format(Me!txtDate, "\#mm\/dd\/yyyy\#")
End If
If Len(strPlatform) > 0 Then
strSQL = strSQL & " AND Platform LIKE '*" & strPlatform & "*' "
End If
If Left(strSQL, 4) = " AND" Then
strSQL = " WHERE " & Mid(strSQL, 5)
End If
strSQL = "SELECT DocumentNumber, DocumentDate, Platform " _
& " FROM tblDocument " _
& strSQL _
& " ORDER BY DocumentDate, DocumentNumber, Platform;"
Me!lstSearch.RowSource = strSQL
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "sFindData", vbOKOnly + vbCritical, "Error: & err.n"
Resume sExit
End Sub
然后这将被调用如下:
Private Sub txtDate_AfterUpdate()
Call sFindData(Nz(Me!txtDocument, ""), Nz(Me!txtDate, #12/31/2099#), Nz(Me!txtPlatform, ""))
End Sub
Private Sub txtDocument_Change()
Call sFindData(Nz(Me!txtDocument.Text, ""), Nz(Me!txtDate, #12/31/2099#), Nz(Me!txtPlatform, ""))
End Sub
Private Sub txtPlatform_Change()
Call sFindData(Nz(Me!txtDocument, ""), Nz(Me!txtDate, #12/31/2099#), Nz(Me!txtPlatform.Text, ""))
End Sub
请注意,我没有在 DocumentDate 字段中包含部分匹配项,因为这没有任何意义。
问候,