查询(在线等)

put2001_ruan 2003-04-02 07:42:30
有三个文本方框:text1,text2,text3
一个数据库含以下字段:会员名字,会员卡号,会员地址
我想进行三个方框的或查询!(既可以用会员名字查询,或会员卡号,或两者结合,或三个结合查询)
Dim cnn1 As ADODB.Connection
Dim rst1 As ADODB.Recordset

Dim strcnn As String
strcnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\project\公司\db2.mdb;Persist Security Info=False"
Set cnn1 = New ADODB.Connection
Set rst1 = New ADODB.Recordset

cnn1.Open strcnn

Private Sub Command1_Click()

'怎么进行三个方框的或查询啊?

end sub
...全文
11 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
macwong 2003-04-03
支持楼上的:)
回复
xinjiana 2003-04-03
楼上的方法最好,

推荐使用
回复
饮水需思源 2003-04-03
dim strsql as string
dim tmp_text1 as string
dim tmp_text2 as string
dim tmp_text3 as string

if trim(text1.text)="" then
tmp_text1=""
else
tmp_text1=" and 会员姓名='"& trim(text1.text) &"'"
endif
if trim(text2.text)="" then
tmp_text2=""
else
tmp_text2=" and 会员卡号='"& trim(text2.text) &"'"
endif
if trim(text3.text)="" then
tmp_text3=""
else
tmp_text3=" and 会员地址='"& trim(text3.text) &"'"
endif

strsql=" select * from tablename where 1=1 " & tmp_text1 & tmp_text2 & tmp_text3

if rs.state=adstateopen then rs.close
rs.open strsql,conn,3,3
回复
jin888aaaa 2003-04-03
用数组好了
回复
lsftest 2003-04-02
关键是如何构造那句SQL语句吧,试试这个:
假设:
TEXT1:会员名字,NAME
TEXT2:会员卡号,ID(数字类型,如是字符类型就改相应的TEXT2SQL)
TEXT3:会员地址,ADDRESS
--------------------------------------

DIM SQL AS STRING
DIM TEXT1SQL AS STRING
DIM TEXT2SQL AS STRING
DIM TEXT3SQL AS STRING

SQL=“SELECT * FROM TABLE ”

IF TEXT1.TEXT<>"" THEN
TEXT1SQL=" WHERE NAME='"+TEXT1.TEXT+"' "
ELSE
TEXT1SQL=""
ENDIF

IF TEXT2.TEXT<>"" THEN

IF TEXT1.TEXT<>"" THEN
TEXT2SQL=" AND ID="+text2.text
else
text2sql=" where id="+text2.text
endif

else
text2sql=""
endif

if text3.text<>"" then

if text1.text<>"" or text2.text<>"" then
text3sql=" and address='"+text3.text+"'"
else
text3sql=" where address='"+text3.text+"'"
endif

else

text3sql=""

endif

sql=sql+text1sql+text2sql+text3sql+" ;"

这个SQL是三个文件框内容的任意组合。
回复
Wang_xiaoni 2003-04-02
比较赞成newcar
回复
heipifeng 2003-04-02
dragon525()
比较赞成
回复
newcar 2003-04-02
Private sub Command_click()
Dim sqlStr as string
sqlStr=""
IF TEXT1.TEXT<>"" THEN
SQLSTR=SQLSTR+IIf(SQLSTR="","select * from Tab where field1='" & text1.text & "'",SQLSTR+" and field1='" & text2.text & "'")
ENDIF
IF TEXT2.TEXT<>"" THEN
SQLSTR=SQLSTR+IIf(SQLSTR="","select * from Tab where field2='" & text2.text & "'",SQLSTR+" and field2='" & text2.text & "'")
ENDIF
IF TEXT3.TEXT<>"" THEN
SQLSTR=SQLSTR+IIf(SQLSTR="","select * from Tab where field3='" & text3.text & "'",SQLSTR+" and field3='" & text3.text & "'")
ENDIF

………………‘执行查询操作
End sub
回复
dragon525 2003-04-02
Private sub Command_click()
Dim sqlStr as string
sqlStr="select * from Tab"
If text1.text<>"" then
sqlStr=sqlStr & "where field1="'" & text1.text & "'"
Else
Exit sub
End if

If text2.text<>"" then
If text1.text<>"" then
sqlStr= sqlStr & "and Field2="'" & text2.text & "'"
Else
sqlStr=sqlStr & "where Field2="'" & text2.text & "'"
End if
End if

If text3.text<>"" then
If text1.text<>"" or text2.text<>"" then
sqlStr=sqlStr & "and Field3="'" & text3.text & "'"
Eles
sqlStr=sqlStr & "where Field3="'" & text3.text & "'"
End if
End if

………………‘执行查询操作
End sub


随手写的,没有测试。试试看。(思路是对的)
回复
put2001_ruan 2003-04-02
up
回复
相关推荐
发帖

1187

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2003-04-02 07:42
社区公告
暂无公告