1,217
社区成员




'这类多条件不固定的查询条件可以用WHERE 1=1 的SQL拼接的方法来实现
'代码中尽量不要使用on error resume next
Private Sub Command7_Click()
dim strTmp1 as string
dim strTmp2 as string
dim strSql as string
On Error goto ErrHandle
Adodc2.CommandType = adCmdText
if check1.value=1 then
strTmp1=" AND 所属县区='"& trim(combo5.text) &"'"
else
strTmp1=""
end if
if check2.value=1 then
strTmp2=" AND 所属行业='"& trim(combo6.text) &"'"
else
strTmp2=""
end if
strSql=" SELECT * FROM qy WHERE 1=1 " & strTmp1 & strTmp2
Adodc2.RecordSource = strSql
Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
exit sub
ErrHandle:
msgbox "操作失败,错误原因为:" & err.description,48,"提示"
exit sub
end sub
Private Sub Command7_Click()
On Error Resume Next
Adodc2.CommandType = adCmdText
If Check1.value = 1 And Check2.value = 1 Then '
Adodc2.RecordSource = "select * from qy where 所属县区='" & trim(Combo5.Text) & "' and 所属行业= '" & trim(Combo6.Text) & "'"
Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
ElseIf Check1.value = 1 And Check2.value = 0 Then
Adodc2.RecordSource = "select * from qy where 所属县区='" & trim(Combo5.Text) & "' "
Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
ElseIf Check1.value = 0 And Check2.value = 1 Then '
Adodc2.RecordSource = "select * from qy where 所属行业='" & trim(Combo6.Text) & "'"
Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
Else
MsgBox "查询条件不完整。"
Adodc2.Refresh
End If
End Sub