rs.open "select 姓名 from empleror where 姓名='"& trim(text1.text) &" and 字段='"& tab3 &"' and 字段='"& tab2 &"'',cnn,adOpenDynamic, adLockPessimistic set datagrid.datasource=rs
dim tmp_name as strng dim tmp_sex as string dim tmp_job as string dim strsql as string dim tmprs as new adodb.recordset
if trim(text1.text)<>"" then tmp_name=" and 姓名='"& text1.text &"'" else tmp_name="" endif if trim(text2.text)<>"" then tmp_sex=" and 性别='"& text2.text &"'" else tmp_sex="" endif if trim(text3.text)<>"" then tmp_job=" and 职位='"& text3.text &"'" else tmp_job="" end ifstrsql=" select * from employee where 1=1 " & tmp_name & tmp_sex & tmp_jobif tmprs.state=adstateopen then tmprs.close tmprs.open strsql,conn,adopenkeyset,adlockreadonlyset datagrid.datasource=tmprsset tmprs=nothing
to (leftie) “strsql=" select * from employee where 1=1 " & tmp_name & tmp_sex & tmp_job” 一句我不太懂,能解释一下吗?
因为用户的输入条件是不固定的,有可能全部输入,也有可能一个都不输入 所以如果你把"where"写入到哪里就很难判断(特别是有四个以上的用户输入条件时),如果按照的我方法,用户输入条件则某一变量为" and ...",否则为"",这样连成的语句为: "select * from tablename where 1=1" 或 "select * from tablename where 1=1 and ... and ..."
set datagrid.datasource=rs
tab1 = Combo1.Text
tab2 = Combo2.Text
tab3 = Text3.Text
strSQL = "Select * From Empleror Where" & " " & tab1 & tab2 & tab3
Text2.Text = strSQLAdodc1.Recordset.Open strSQL
Adodc1.Refresh
Set DataGrid.DataSource = Adodc1.Recordset
DataGrid.Refresh
运行后提示错误:如果对象已经打开,则不允许应用程序所要求的操作。
另外,直接将SQL语句赋值给Adodc1.RecordSource属性,然后Adodc1.Refresh.还有Set DataGrid.DataSource=Adodc1即可,不需要指定.Recordset
set datagrid.datasource=rs
查询条件就是通过生成的,简单的说,就是要进行任意单条件查询。
dim tmp_sex as string
dim tmp_job as string
dim strsql as string
dim tmprs as new adodb.recordset
if trim(text1.text)<>"" then
tmp_name=" and 姓名='"& text1.text &"'"
else
tmp_name=""
endif
if trim(text2.text)<>"" then
tmp_sex=" and 性别='"& text2.text &"'"
else
tmp_sex=""
endif
if trim(text3.text)<>"" then
tmp_job=" and 职位='"& text3.text &"'"
else
tmp_job=""
end ifstrsql=" select * from employee where 1=1 " & tmp_name & tmp_sex & tmp_jobif tmprs.state=adstateopen then tmprs.close
tmprs.open strsql,conn,adopenkeyset,adlockreadonlyset datagrid.datasource=tmprsset tmprs=nothing
“strsql=" select * from employee where 1=1 " & tmp_name & tmp_sex & tmp_job”
一句我不太懂,能解释一下吗?
所以如果你把"where"写入到哪里就很难判断(特别是有四个以上的用户输入条件时),如果按照的我方法,用户输入条件则某一变量为" and ...",否则为"",这样连成的语句为:
"select * from tablename where 1=1" 或
"select * from tablename where 1=1 and ... and ..."
& combo2.text & "'" & text1.text & "'"
Adodc1.Refresh
Set DataGrid.DataSource = Adodc1.Recordset
DataGrid.Refresh