帮忙

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

ztj = ztj & "and" & tz & "=" & '& " & Trim(ty) & "&'&"
上面这两句条件连接的语句该如何写。
        
       

Private Sub Form_Load()
Set con = New ADODB.Connection
str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\桌面\图书管理系统\图书管理系统.mdb;Persist Security Info=False"
con.Open str
Set rs = con.Execute("select * from 图书信息表 ")
End Sub



Private Sub Command1_Click
Dim a As Integer
For i = 0 To 5
If Text1(i) <> "" Then
a = a + 1
If a = 1 Then
tz = Label1(i).Caption      记录字段的名称
ty = Text1(i).Text        字段的值
ztj = tz & "="&'"&trim(ty)&"'   连接起来,字段=值
Else
tz = Label1(i).Caption
ty = Text1(i).Text
ztj = ztj & "and" & tz & "=" & '& " & Trim(ty) & "&'&"
End If
End If
Next i
End Select
q = "select * from 图书信息表 where"
Set rs = con.Execute(q & ztj)
Set mg1.DataSource = rs

...全文
93 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
okcat 2004-10-18
  • 打赏
  • 举报
回复
up
okcat 2004-10-18
  • 打赏
  • 举报
回复
up
xiaoxiaoP 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 & "'"  


Else
ztj = ztj & "and " & Label1(i).Caption & "=" & "'" & Text1(i).Text &"'"  

End If
End If
Next i

Set rs = con.Execute("select * from 图书信息表 where" & ztj)
End Select
End Sub
okcat 2004-10-17
  • 打赏
  • 举报
回复
不行啊,我改了一下,改为如下。可还是总提示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


itcoco 2004-10-16
  • 打赏
  • 举报
回复
dim ss as string
dim sql as string
if len(text1(1))>0 then ss= Label1.caption & text1(1).text
if len(text1(2))>0 and ss<>"" then ss="and" & Label2.caption & text1(2).text
elseif ss=""
ss=Label2.caption & text1(2).text
end if
......
if len(text1(6))>0 and ss<>'' then ss="and " & Label6.caption & text1(6).text

sql="select * from 图书信息表 where" & ss

1,217

社区成员

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

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