查询条件的连接问题,请教

okcat 2004-10-18 11:51:12
窗体上有六个标签,标签分别为数据库中记录的各个字段。标签为控件数组。相对应的还有六个文本框,也是控件数组,分别放查找条件,还有一个命令按钮。点击进行查询。即从图书信息表中查找图书信息.
我是这样想的,查询的时候首先判断六个文本框中是否有查询条件,若有则用AND连接起来。然后再查询显示在网格控件中


运行时提示from子句语法错误。头都大了。又是哪里的错啊。
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
Dim a As Integer
For i = 0 To 5
If Text1(i) <> "" Then
a = a + 1               判断条件个数
If a = 1 Then
ztj = "Label1(i).Caption ='" & Text1(i).Text & "'"   第一个条件不需要“AND”

Else
ztj = ztj & "and " & Label1(i).Caption & "=" & Text1(i).Text  二个或二个以上用AND                                      连接
End If
End If
Next i

Set rs = con.Execute("select * from 图书信息表 where" & ztj)
End Select
End Sub



...全文
105 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
饮水需思源 2004-10-18
  • 打赏
  • 举报
回复
'提供一思路:
dim strsql as string
dim temp1 as string
dim temp2 as string
dim temp3 as string

if text1.text="" then
temp1=""
else
temp1=" and 字段1='"& text1.text &"'"
end if
if text2.text="" then
temp2=""
else
temp2=" and 字段2='"& text2.text &"'"
end if
if text3.text="" then
temp3=""
else
temp3=" and 字段3='"& text3.text &"'"
end if

'组合查询条件
strsql=" select * from tablename where 1=1 " & temp1 & temp2 & temp3
if rs.state=adstateopen then rs.close
rs.open strsql,conn,adopenkeyset,adlockreadonly
set mshflexgrid1.datasource=rs
rs.close
set rs=nothing
supertarzan 2004-10-18
  • 打赏
  • 举报
回复
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
Dim a As Integer
For i = 0 To 5
If Text1(i) <> "" Then
a = a + 1
If a = 1 Then
ztj = Label1(i).Caption "='" & Text1(i).Text & "'"  'Label1(i).Caption不该也在
'双引号内
Else
ztj = ztj & " and " & Label1(i).Caption & "=" & Text1(i).Text  'and前面要留一个空格
End If
End If
Next i

Set rs = con.Execute("select * from 图书信息表 where " & ztj) 'where后面应该有个空格
End Select
End Sub
supertarzan 2004-10-18
  • 打赏
  • 举报
回复
请检查你的语法
wz19761022 2004-10-18
  • 打赏
  • 举报
回复
看看你的数据类型是否匹配?
自己看一下所生成的语句是否能在查询分析器里执行.

1,217

社区成员

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

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