这样的查询代码应该怎么写?

yyyttqq 2007-05-17 10:32:13
条件一:temtext2.txt温度上限() temtext2.txt温度下限()
条件二:pretext2.txt压力上限() pretext3.txt压力下限()
条件三:flowtext2.txt流量上限() flowtext3.txt流量下限()
条件四:datacombo 介质
四个条件可以单独,两两,三个或者全选查询
这样的代码该怎么写?
谢谢
...全文
294 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyyttqq 2007-05-18
  • 打赏
  • 举报
回复
strSql = "select * from tablename where " & strSql & "1=1"
' ...
这里看的不是太懂,是像leftie写的那样 strsql=" select * from tablename where 1=1 " & where_tem & where_pre & where_flow & where_media 么?
fj182 2007-05-18
  • 打赏
  • 举报
回复
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
浅蓝马 2007-05-18
  • 打赏
  • 举报
回复
奇怪了,我怎么看不懂?
yyyttqq 2007-05-18
  • 打赏
  • 举报
回复
我的结构是这样的
check1 条件一
check2 条件二
check3 条件三
check4 datacombo
应该怎么写那?
刚学习vb,不是很懂,能说的明白些么?
非常感谢
yyyttqq 2007-05-18
  • 打赏
  • 举报
回复
发帖的时候说得不是很明白。就是一个条件中上限和下限是同时存在的,不能一个单独存在
饮水需思源 2007-05-18
  • 打赏
  • 举报
回复
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


1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧