多條件查詢 Access數(shù)據(jù)庫功能模塊講解 VBA代碼編程實例
Public filter_text As String? ? '定義為公共變量
Private Sub Command查詢_Click()
filter_text = ""
If Me.員工編號 <> "" Then
? ? If filter_text <> "" Then
? ? filter_text = filter_text & " and 員工編號 like '*" & Me.員工編號 & "*'"
? ? Else
? ? filter_text = "員工編號 like '*" & Me.員工編號 & "*'"
? ? End If
End If
If Me.部門 <> "" Then
? ? If filter_text <> "" Then
? ? filter_text = filter_text & " and 部門 like '*" & Me.部門 & "*'"
? ? Else
? ? filter_text = "部門 like '*" & Me.部門 & "*'"
? ? End If
End If
If Me.職位 <> "" Then
? ? If filter_text <> "" Then
? ? filter_text = filter_text & " and 職位 like '*" & Me.職位 & "*'"
? ? Else
? ? filter_text = "職位 like '*" & Me.職位 & "*'"
? ? End If
End If
If Me.姓名 <> "" Then
? ? If filter_text <> "" Then
? ? filter_text = filter_text & " and 姓名 like '*" & Me.姓名 & "*'"
? ? Else
? ? filter_text = "姓名 like '*" & Me.姓名 & "*'"
? ? End If
End If
If Me.銷售日期1 <> "" And Me.銷售日期2 <> "" Then
? ? If filter_text <> "" Then
? ? filter_text = filter_text & " and 銷售日期 between #" & Me.銷售日期1 & "# and #" & Me.銷售日期2 & "#"
? ? Else
? ? filter_text = "銷售日期 between #" & Me.銷售日期1 & "# and #" & Me.銷售日期2 & "#"
? ? End If
End If
If Me.銷售額1 <> "" And Me.銷售額2 <> "" Then
? ? If filter_text <> "" Then
? ? filter_text = filter_text & " and 銷售額 >= " & Me.銷售額1 & " and 銷售額<=" & Me.銷售額2
? ? Else
? ? filter_text = "銷售額 >= " & Me.銷售額1 & " and 銷售額<=" & Me.銷售額2
? ? End If
End If
'------------------------------篩選子窗體
If filter_text <> "" Then
Me.數(shù)據(jù)表子窗體.Form.Filter = filter_text
Me.數(shù)據(jù)表子窗體.Form.FilterOn = True
Else
Me.數(shù)據(jù)表子窗體.Form.FilterOn = False
End If
End Sub
Private Sub Command清空_Click()
員工編號.Value = ""
姓名.Value = ""
部門.Value = ""
職位.Value = ""
銷售日期1.Value = ""
銷售日期2.Value = ""
銷售額1.Value = ""
銷售額2.Value = ""
End Sub
Private Sub Command全部_Click()
Me.數(shù)據(jù)表子窗體.Form.FilterOn = False
End Sub