这样的查询代码应该怎么写?
条件一:temtext2.txt温度上限() temtext2.txt温度下限()
条件二:pretext2.txt压力上限() pretext3.txt压力下限()
条件三:flowtext2.txt流量上限() flowtext3.txt流量下限()
条件四:datacombo 介质
四个条件可以单独,两两,三个或者全选查询
这样的代码该怎么写?
谢谢
[解决办法]
dim where_tem as string
dim where_pre as string
dim where_flow as string
dim where_media as string
dim strsql as string
'温度条件
if trim(temtext2.text)= " " and trim(temtext3.text)= " " then
where_tem= " "
end if
if trim(temtext2.text) <> " " and trim(temtext3.text)= " " then
where_tem= " and 温度字段> = "& temtext2.text & " "
end if
if trim(temtext2.text)= " " and trim(temtext3.text) <> " " then
where_tem= " and 温度字段 <= "& temtext3.text & " "
end if
if trim(temtext2.text) <> " " and trim(temtext3.text) <> " " then
where_tem= " and 温度字段 between "& temtext2.text & " and "& temtext3.text & " "
end if
'压力条件
if trim(pretext2.text)= " " and trim(pretext3.text)= " " then
where_pre= " "
end if
if trim(pretext2.text) <> " " and trim(pretext3.text)= " " then
where_pre= " and 压力字段> = "& pretext2.text & " "
end if
if trim(pretext2.text)= " " and trim(pretext3.text) <> " " then
where_pre= " and 压力字段 <= "& pretext3.text & " "
end if
if trim(pretext2.text) <> " " and trim(pretext3.text) <> " " then
where_pre= " and 压力字段 between "& pretext2.text & " and "& pretext3.text & " "
end if
'流量条件
if trim(flowtext2.text)= " " and trim(flowtext3.text)= " " then
where_flow= " "
end if
if trim(flowtext2.text) <> " " and trim(flowtext3.text)= " " then
where_flow= " and 流量字段> = "& flowtext2.text & " "
end if
if trim(flowtext2.text)= " " and trim(flowtext3.text) <> " " then
where_flow= " and 流量字段 <= "& flowtext3.text & " "
end if
if trim(flowtext2.text) <> " " and trim(flowtext3.text) <> " " then
where_flow= " and 流量字段 between "& flowtext2.text & " and "& flowtext3.text & " "
end if
'介质条件
if trim(datacombo.text)= " " then
where_media= " "
else
where_media= " and 介质字段= ' "& trim(datacombo.text) & " ' "
end if
'构造SQL查询语句
strsql= " select * from tablename where 1=1 " & where_tem & where_pre & where_flow & where_media
[解决办法]
Private Sub Command1_Click()
Dim strSql As String
If Check1.Value = vbChecked Then
strSql = "温度 between " & txtTem1.Text & " and " & txtTem2.Text & " and "
End If
If Check2.Value = vbChecked Then
strSql = strSql & "压力 between " & txtPre1.Text & " and " & txtPre2.Text & " and "
End If
If Check3.Value = vbChecked Then
strSql = strSql & "流量 between " & txtFlow1.Text & " and " & txtFlow2.Text & " and "
End If
If Check4.Value = vbChecked Then
strSql = strSql & "介质 = ' " & datacombo.Text & " ' and "
End If
strSql = "select * from tablename where " & strSql & "1=1 "
' ...
End Sub