|
这种三个条件的判断空值的情况已经很多了,4个条件以上的会更多?如何写语句,当条件值为空时不参加查询呢?
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Cnn As New ADODB.Connection
Dim Rst As New ADODB.Recordset
Dim strSQL As String
Cnn.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" & stpath & ";Jet OLEDB:Database Password=" & "2345"
If TextBox1.Value = "" And ComboBox1 = "" And ComboBox2 = "" Then
strSQL = "Select * from 营业数据 "
ElseIf TextBox1.Value = "" And ComboBox1 = "" And ComboBox2 <> "" Then
strSQL = "Select * from 营业数据 WHERE 营业数据.状态 = '" & ComboBox2.Value & "' "
ElseIf TextBox1.Value = "" And ComboBox1 <> "" And ComboBox2 = "" Then
strSQL = "Select * from 营业数据 WHERE 营业数据.区域 = '" & ComboBox1.Value & "' "
ElseIf TextBox1.Value <> "" And ComboBox1 = "" And ComboBox2 = "" Then
strSQL = "Select * from 营业数据 WHERE 营业数据.合同号 = '" & TextBox1.Value & "' "
ElseIf TextBox1.Value <> "" And ComboBox1 <> "" And ComboBox2 = "" Then
strSQL = "Select * from 营业数据 WHERE 营业数据.合同号 = '" & TextBox1.Value & "'AND 营业数据.区域 = '" & ComboBox1.Value & "' "
ElseIf TextBox1.Value <> "" And ComboBox1 = "" And ComboBox2 <> "" Then
strSQL = "Select * from 营业数据 WHERE 营业数据.合同号 = '" & TextBox1.Value & "'AND 营业数据.状态 = '" & ComboBox2.Value & "' "
ElseIf TextBox1.Value = "" And ComboBox1 <> "" And ComboBox2 <> "" Then
strSQL = "Select * from 营业数据 WHERE 营业数据.区域 = '" & ComboBox1.Value & "'AND 营业数据.状态 = '" & ComboBox2.Value & "' "
ElseIf TextBox1.Value <> "" And ComboBox1 <> "" And ComboBox2 <> "" Then
strSQL = "Select * from 营业数据 WHERE 营业数据.合同号 = '" & TextBox1.Value & "'AND 营业数据.区域 = '" & ComboBox1.Value & "'AND 营业数据.状态 = '" & ComboBox2.Value & "' "
End If
Rst.Open strSQL, Cnn
Sheet13.Range("A3:S20000").ClearContents
Sheet13.Cells(3, 1).CopyFromRecordset Rst
Sheet13.Cells.NumberFormatLocal = "G/通用格式"
Sheet13.Range("R:R").NumberFormatLocal = "yyyy-m-d"
Sheet13.Activate
Rst.Close
Set Rst = Nothing
Set Cnn = Nothing
Sheet13.Visible = True
'Sheet13.Range("A1").Select
Application.ScreenUpdating = True
Application.GoTo Sheet13.Range("A3")
Application.Calculation = xlCalculationAutomatic
End Sub
|
|