多個CheckBox製作SQL的WHERE組合
●拉三個CheckBox、三個TextBox與一個CommandButton。 ●簡單的CheckBox與SQL的WHERE子句處理方式:
Private Sub Check1_Click()
- Dim strSQL As String
- Dim IsFirst As Boolean
- IsFirst = True
- strSQL = "SELECT * FROM MyTable "
-
If Check1.Value = 1 Or Check2.Value = 1 Or Check3.Value = 1 Then
- strSQL = strSQL & "WHERE "
End If
-
If Check1.Value = 1 Then
- strSQL = strSQL & "MyName = '" & Text1.Text & "' "
- IsFirst = False
End If
-
If Check2.Value = 1 Then
-
If IsFirst = False Then
- strSQL = strSQL & "AND "
End If
- strSQL = strSQL & "MyDate = '" & Text2.Text & "' "
- IsFirst = False
End If
-
If Check3.Value = 1 Then
-
If IsFirst = False Then
- strSQL = strSQL & "AND "
End If
- strSQL = strSQL & "MyGender = '" & Text3.Text & "' "
- IsFirst = False
End If
- MsgBox strSQL
End Sub |
|