设置一个查询窗体如下:
□编号:□‘textbox
□名称:□'textbox
□地址:□,textbox
□性质:□,combobox
...............
............前面是check,后面是textbox,我的问题是,当前面的check选中后,在后面的textbox 输入查询条件,可以任意组合查询条件,该怎么写SQL语句,不知道下面这样写为什么没有用呢。if check1.value=1 or check2.value=1 or check3.value=1 then
rs.open"select * from table where 编号='"+text1.text+"'and 名称='"+text2.text+"' and 地址='"+text3.text+"' and 性质='"+combo1.text+"'"
endif
为什么查询不出来呢。是不是哪里有错呀,当要任意组合查询时,该怎么办呢。
□编号:□‘textbox
□名称:□'textbox
□地址:□,textbox
□性质:□,combobox
...............
............前面是check,后面是textbox,我的问题是,当前面的check选中后,在后面的textbox 输入查询条件,可以任意组合查询条件,该怎么写SQL语句,不知道下面这样写为什么没有用呢。if check1.value=1 or check2.value=1 or check3.value=1 then
rs.open"select * from table where 编号='"+text1.text+"'and 名称='"+text2.text+"' and 地址='"+text3.text+"' and 性质='"+combo1.text+"'"
endif
为什么查询不出来呢。是不是哪里有错呀,当要任意组合查询时,该怎么办呢。
解决方案 »
- vb工程里有多与两个类,在其中一个类A里调用类B的时候类B的OnStartPage没有被执行
- 寻工具条按钮:添加、编辑、删除、刷新、查找、过滤 关闭、提交等图标
- 怎么让FORM窗体成为MIDFORM窗体?
- VB如何动态加载第三方的DLL控件?
- 用inet控件下载文本文件的问题
- 是VB的问题还是软件的问题?小弟快要跳楼了
- ??请问各位::如何实现象WINDOWS一样打开一个文件进行阅读呢???
- 紧急求救:用data report作报表的时候,如何设置参数,动态打印?
- 请教高手!!!!急!!!急!!!
- 请问如何使程序运行后WIN桌面上的图标和下面的任务栏能自动隐藏?
- 请问如何在VB中关闭指定的网页,急!!!
- 有没有谁有物品出入库的程序,在线等!
strsql="select * from table where 编号='" & text1.text &"'"
if check2.value=1 then
strsql=strsql+"and 名称='"&text2.text &"'"
elseif check3.value=1 then
strsql=strsql+"and 地址='"&text3.text &"'"
elseif combo1.text<>empty then
strsql=strsql+"and 性质='" & combo1.text &"'"
endif
rs.open strsql,连接,游标类型,锁类型
if check2.value=1 then
strsql=strsql+"and 名称='"&text2.text &"'"
elseif check3.value=1 then
strsql=strsql+"and 地址='"&text3.text &"'"
elseif combo1.text<>empty then
strsql=strsql+"and 性质='" & combo1.text &"'"
endif
==========================*//每次只能选择一个来组合,不能实现楼主的要求的修改为:
dim strsql as string
strsql="select * from table where "if check1.value=1 then
strsql=strsql & "编号='" & text1.text &"'"
end ifif check2.value=1 then
strsql=strsql & " and 名称='"&text2.text &"'"
end ifif check3.value=1 then
strsql=strsql & " and 地址='"&text3.text &"'"
end ifif combo1.text<>"" then
strsql=strsql & " and 性质='" & combo1.text &"'"
endifrs.open strsql,3,3
你这样写,如果我第一次没有选择编号呢,比如说我第一次选名称,那SQLSTRING不用成了"and 名称=''& text2.text &''"嘛,那rs.open sqlstring,不就出错了吗-----------------------那倒底该怎么解决呢,可惜我没分。。大家帮忙吧
然后再按qiqunet(瑞旗·广东)方法,就没有你所说的问题了
strsql="select * from table where "if check1.value=1 then strsql=strsql & "编号='" & text1.text &"'"if check2.value=1 then strsql=strsql & " and 名称='"&text2.text &"'"if check3.value=1 then strsql=strsql & " and 地址='"&text3.text &"'"if combo1.text<>"" then strsql=strsql & " and 性质='" & combo1.text &"'"'下面这三种方法,可根据你的需要选其一
if strsql<>"select * from table where " then rs.open strsql,3,3'或者换成(选择所有数据)
if strsql="select * from table where " then strsql="select * from table where 1=1"
rs.open strsql,3,3'或者换成(什么数据也不选,不过选择这个数据的表结构形式)
if strsql="select * from table where " then strsql="select * from table where 1=0"
rs.open strsql,3,3
strsql = "select * from table where"
LenStr = Len(strsql)
If check1.Value = 1 Then strsql = strsql & " 编号='" & text1.Text & "'"
If check2.Value = 1 Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 名称='" & text2.Text & "'"
Else
strsql = strsql & " 名称='" & text2.Text & "'"
End If
End If
If check2.Value = 1 Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 地址='" & text3.Text & "'"
Else
strsql = strsql & " 地址='" & text3.Text & "'"
End If
End If
If check3.Value = 1 Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 性质='" & combo1.Text & "'"
Else
strsql = strsql & " 性质='" & combo1.Text & "'"
End If
End If
If combo1.Text <> "" Then
If Len(strsql) = LenStr Then
strsql = strsql & " and 名称='" & text2.Text & "'"
Else
strsql = strsql & " 名称='" & text2.Text & "'"
End If
End If'下面这三种方法,可根据你的需要选其一'如果用户没有选择就执行这段代码,那么就不做任何操作或结合“Msgbox”告诉用户要选择一个条件才能查询。
If Len(strsql) <> LenStr Then rs.open strsql, 3, 3'或者换成(选择所有数据)
If Len(strsql) = LenStr Then strsql = "select * from table where 1=1"
rs.open strsql, 3, 3'或者换成(什么数据也不选,不过选择这个数据的表结构形式)
If Len(strsql) = LenStr Then strsql = "select * from table where 1=0"
rs.open strsql, 3, 3
'上面又错了,多了一个并且错了位!Dim strsql As String, LenStr As Integer
strsql = "select * from table where"
LenStr = Len(strsql)
If check1.Value = 1 and text1<>"" Then strsql = strsql & " 编号='" & text1.Text & "'"
If check2.Value = 1 and text2<>"" Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 名称='" & text2.Text & "'"
Else
strsql = strsql & " 名称='" & text2.Text & "'"
End If
End If
If check3.Value = 1 and text3<>"" Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 地址='" & text3.Text & "'"
Else
strsql = strsql & " 地址='" & text3.Text & "'"
End If
End If
If combo1.Text <> "" Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 性质='" & combo1.Text & "'"
Else
strsql = strsql & " 性质='" & combo1.Text & "'"
End If
End If'下面这三种方法,可根据你的需要选其一'如果用户没有选择就执行这段代码,那么就不做任何操作或结合“Msgbox”告诉用户要选择一个条件才能查询。
If Len(strsql) <> LenStr Then rs.open strsql, 3, 3'或者换成(选择所有数据)
If Len(strsql) = LenStr Then strsql = "select * from table where 1=1"
rs.open strsql, 3, 3'或者换成(什么数据也不选,不过选择这个数据的表结构形式)
If Len(strsql) = LenStr Then strsql = "select * from table where 1=0"
rs.open strsql, 3, 3
然后使用控件数组。让check也成为数组,从上至下依次排列。
然后:
dim x as integer '循环变量
dim sel as string '保存select语句
sel="select * from xxx where " ’写出select语句头
for x=0 to text控件数组最大下标
if check1(x).value=1 then sel=sel & check1(x).caption & "='" & text1(x).text & "' and " '判断是否选中,选中后添加 “字段名='值' and ”这个字符串
next x
for x=text控件数组最大下标+1 to check控件数组最大下标+text控件数组最大下标
if check1(x).value=1 then sel=sel & check1(x).caption & "='" & combobox1(x).text & "' and " '同上
next x
之后将sel字符串的后4个字符去掉,就是你想要的.要去掉最后添加的" and"
Private Sub Command1_Click()
Dim first As Boolean
first = False
Dim sqlid, sqlname, sqladdress, sqlpro, sqlstr As StringIf Check1.Value = 1 Then
sqlid = " where 编号='" + Trim(Text1.Text) + "' "
first = True
Else
sqlid = ""
End If
If Check2.Value = 1 Then
If first = False Then
sqlname = " where 名称='" + Trim(Text2.Text) + "'"
first = True
Else
sqlname = " and 名称='" + Trim(Text2.Text) + "'"
End If
Else
sqlname = ""
End If
If Check3.Value = 1 Then
If first = False Then
sqladdress = " where 地址='" + Trim(Text3.Text) + "'"
first = True
Else
sqladdress = " and 地址='" + Trim(Text3.Text) + "'"
End If
Else
sqladdress = ""
End If
If Check4.Value = 1 Then
If first = False Then
sqlpro = " where 性质='" + Trim(Text4.Text) + "'"
first = True
Else
sqlpro = " and 性质='" + Trim(Text4.Text) + "'"
End If
Else
sqlpro = ""
End Ifsqlstr = "select * from table " + sqlid + sqlname + sqladdress + sqlpro + "; "
Print sqlstr
End Sub上面是一个简单的例子,可以得到四个条件的任意组合sql查询语句。。实际编程时还要检测被选中的textbox中是否有输入内容
If Check4.Value = 1 Then
If first = False Then
sqlpro = " where 性质='" + Trim(combo1.Text) + "'"
first = True
Else
sqlpro = " and 性质='" + Trim(combo1.Text) + "'"
End If
Else
sqlpro = ""
End If
Private Sub Command1_Click()
Dim strsql As String
strsql = "select * from table where"
If check1.Value = 1 Then strsql = strsql & " 编号='" & text1.Text & "' and "
If check2.Value = 1 Then strsql = strsql & " 名称='" & text2.Text & "' and "
If check3.Value = 1 Then strsql = strsql & " 地址='" & text3.Text & "' and "
If combo1.Text <> "" Then strsql = strsql & " 性质='" & combo1.Text & "' and "
If Right(strsql, 4) = "and " Then
strsql = Left(strsql, Len(strsql) - 5) '删除最后面的字符“ and ”
rs.open strsql, 3, 3
Else
MsgBox "你没有选择任何的条件。"
End If
End Sub
strsql = "select * from table where"
LenStr = Len(strsql)
If check1.Value = 1 and text1<>"" Then strsql = strsql & " 编号='" & text1.Text & "'"
If check2.Value = 1 and text2<>"" Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 名称='" & text2.Text & "'"
Else
strsql = strsql & " 名称='" & text2.Text & "'"
End If
End If
If check3.Value = 1 and text3<>"" Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 地址='" & text3.Text & "'"
Else
strsql = strsql & " 地址='" & text3.Text & "'"
End If
End If
If combo1.Text <> "" Then
If Len(strsql) <> LenStr Then
strsql = strsql & " and 性质='" & combo1.Text & "'"
Else
strsql = strsql & " 性质='" & combo1.Text & "'"
End If
End If'下面这三种方法,可根据你的需要选其一'如果用户没有选择就执行这段代码,那么就不做任何操作或结合“Msgbox”告诉用户要选择一个条件才能查询。
If Len(strsql) <> LenStr Then rs.open strsql, 3, 3'或者换成(选择所有数据)
If Len(strsql) = LenStr Then strsql = "select * from table where 1=1"
rs.open strsql, 3, 3'或者换成(什么数据也不选,不过选择这个数据的表结构形式)
If Len(strsql) = LenStr Then strsql = "select * from table where 1=0"
rs.open strsql, 3, 3
=================================================================
各位大哥,昨晚按瑞旗.广东上面的办法做了一下,以现遗漏了一个问题,当条件成立时,where和and 中间没有空格,导出SQL语法错误,因此下面的语句
strsql = strsql & " and 性质='" & combo1.Text & "'"要更改成
strsql = strsql &" "& " and 性质='" & combo1.Text & "'"好像才行。不过呢,问题已经解决了,谢谢各位大哥相助,可惜我分不够,只有10郁闷