ASP+vbscript写的万能查询表达式生成器

来源:岁月联盟 编辑:exp 时间:2005-11-25
<!--查询器2.0
使用方法:
在<head></head>之间加以下代码,(代码自编),以解决多库公共连接字段或有重复字段名时的处理(得到字段名的全称)
<script language="vbscript">
<!-- 用于获得公共字段的全称的函数
function allFld(fld)
if fld="工具编号" then
allfld="工具请领单.工具编号"
else
allfld=fld
end if
end function</script>
在要查询的列表后面加:<!--#include file="seeker.asp"-->
在sql语句后右加:if request("s1")<>"" then sql=sql&" and "&request("s1"),这一句就是与你的程序连接上的关键。
接下来就是你的调试,轻松就OK!

作者:湖北十堰市东风有限公司刃量具厂装备管理部 肖述治
邮箱:haohia@sina.com 电话:0719-8243634
-->

<form method="POST" name="exprform" action=<%=request.servervariables("document_name")%> >
<p align="right">
<input type="text" name="txtGo" size="3">
<input type="button" name="btnGo" value="转到">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
第<input type="text" name="txtpage" size="3" value=<%=request("page")%>>页,
共<input type="text" name="TxtMaxPage" size="3" value=<%=rs.pagecount%>>页&nbsp;
<%if page>1 then%>
<input type="button" name="firstpage" value="第一页">
<input type="button" name="prepage" value="上一页">
<%end if%>
<%if page<>rs.pagecount then%>
<input type="button" name="nextpage" value="下一页">
<input type="button" name="lastpage" value="最后一页">
<%end if%>
<table height="91" ><tr><td>
<script language="vbscript">
<!----脚本:表达式生成器
dim f,exp
set f=document.exprform
if trim(f.txtpage.value)="" then f.txtpage.value=1 '---预置页号,当第一次访问页面时用
if cint(f.txtpage.value)>cint(f.txtmaxpage.value) then f.txtpage.value=f.txtmaxpage.value'---当条件改变时,由于页数可能更改,强行改变页号
sub btnGo_onclick '---转到指定的页面事件
if f.txtGo.value="" then msgbox "没有指定页数":exit sub
if not isNumeric(f.txtGo.value) then msgbox "不是数值":exit sub
if cint(f.txtGo.value)>cint(f.txtmaxpage.value) or cint(f.txtGo.value)<1 then msgbox "页号超出范围":exit sub
f.action=f.action&"?page="&f.txtGo.value
f.submit
end sub
sub firstpage_onclick '---第一页按钮事件
f.action=f.action&"?page=1"
f.submit
end sub
sub prepage_onclick '---上一页按钮事件
f.action=f.action&"?page="&(f.txtpage.value-1)
f.submit
end sub
sub nextpage_onclick '---下一页按钮事件
f.action=f.action&"?page="&(f.txtpage.value+1)
f.submit
end sub
sub lastpage_onclick '---最后一页按钮事件
f.action=f.action&"?page="&(f.txtmaxpage.value)
f.submit
end sub

sub fld_onchange'---------字段改变事件
f.sx.value=f.fld.options(f.fld.selectedIndex).name'---将字段的类型存入文本框sx(name里是从服务器获得的)
' f.s1.value=f.fld.options(f.fld.selectedIndex).value
f.opt.style.display=""
f.valu.style.display=""
f.BtnAnd.style.display=""
f.BtnOr.style.display=""
f.BtnEnd.style.display=""
end sub
sub fld_onBlur
call fld_onchange
end sub
sub opt_onchange '---操作符改变事件
end sub
sub valu_onBlur '---失去焦点事件
if f.sx.value=135 and (not isdate(f.valu.value)) then
msgbox "取值框输入的日期格式不正确,请重新输入",vbExclamation,"操作错误!!!"
end if
if (f.sx.value=2 or f.sx.value=3 or f.sx.value=4 or f.sx.value=5) and (not IsNumeric(f.valu.value)) then
msgbox "取值框输入的不是数值,请重新输入",vbExclamation,"操作错误!!!"
exit sub
end if
end sub
sub s1_onchange'---文本条件框改变事件
f.tj.style.display=""
end sub
sub BtnAnd_onclick
if valid()=true then
exp=exp&exp0()&" and "
call viewExpr
end if
end sub
sub BtnOr_onclick
if valid()=true then
exp=exp&exp0()&" or "
call viewExpr
end if
end sub
sub BtnEnd_onclick
if valid()=true then
exp=exp&exp0()&SortExpr()
f.fld.style.display="none"
f.opt.style.display="none"
f.valu.style.display="none"
f.btnand.style.display="none"
f.btnor.style.display="none"
f.btnend.style.display="none"
f.tj.style.display=""
call viewExpr
end if
end sub
function valid() '-------验证输入值的正确性
if f.sx.value=135 and (not isdate(f.valu.value)) then
msgbox "取值框输入的日期格式不正确,请重新输入",vbExclamation,"操作错误!!!"
valid=false
exit function
end if
if (f.sx.value=2 or f.sx.value=3 or f.sx.value=4 or f.sx.value=5) and (not IsNumeric(f.valu.value)) then
msgbox "取值框输入的不是数值,请重新输入",vbExclamation,"操作错误!!!"
valid=false
exit function
end if
if trim(f.valu.value)="" then
msgbox "取值框不能为空!",vbInformation,"操作错误!!!"
valid=false
exit function
end if
valid=true
end function
function exp0()'---生成表达式段落
select case f.sx.value '---根据字段属性选择生成表达式
case 202'---字符型字段
exp0=allFld(f.fld.value)&f.opt.value&"'"&f.valu.value&"'" '---allFld()为自定义函数,以获得完整字段名
case 2,3,4,5'---数值型字段
exp0=allFld(f.fld.value)&f.opt.value&f.valu.value
case 135'---日期型字段
exp0=allFld(f.fld.value)&f.opt.value&"#"&f.valu.value&"#"
case else
exp0=allFld(f.fld.value)&f.opt.value&"'"&f.valu.value&"'"
end select
end function
function SortExpr()'---排序表达式
if trim(f.fldsort.value)<>"无" then
SortExpr=" Order by "&allFld(f.fldsort.value)&" "&f.udSort.value
else
SortExpr=""
end if
end function
sub viewExpr'---在条件框显示表达式
f.s1.value=exp
end sub
sub reset_onClick'---清除查询条件
exp=""
f.s1.value=""
call viewExpr
f.fld.style.display=""
f.tj.style.display="none"
end sub
sub tj_onclick'---提交查询条件
f.submit
end sub
sub allbtn_onClick'---清除条件,显示全部记录
exp=""
f.s1.value=""
f.submit
end sub
sub btnHelp_onClick'---使用说明
content="-----------------------使用说明---------------------"
content=content+vbcrlf+"  查询表达式生成器是按SQL语言规范设计,用户只需按一定的操作程序即可自动生成标准的SQL语句,并能被服务器接受。"
content=content+vbcrlf+"条件表达式操作步骤:"
content=content+vbcrlf+"1、从字段下拉框选取字段。这时运算符下拉框、取值框、操作符按钮显示出来;"
content=content+vbcrlf+"2、从运算符下拉框选择恰当的运算符"
content=content+vbcrlf+"3、在取值框输入要查找的字符或数字或日期"
content=content+vbcrlf+"4、若要并列条件请按“而且”,若要任选条件请按“或者”,完成了条件请按“结束”"
content=content+vbcrlf+"5、当按下“结束”按钮后,在条件文本框下部弹出“提交表达式”,若要重写请按“重置”,检查无误后即可按下提交。"
content=content+vbcrlf+"6、当生成的表达式达不到要求时可以直接对条件文本框内的表达式进行修改,改完后按“提交表达式”发送到服务器端"
content=content+vbcrlf+"排序的操作:"
content=content+vbcrlf+"1、排序表达式必须接在条件表达式的后面,但在操作时可以在条件表达式设置之前预先设置,生成器会自动将排序表达式放在条件表达式的后面"
content=content+vbcrlf+"2、如果只排序,不要查询条件时,请设一个伪条件:字段不等于一个不可能的值,按下“结束”按钮"
content=content+vbcrlf+"3、按“提交表达式”,完成排序"
content=content+vbcrlf+"4、看完“使用说明”后请按“重置”"
f.s1.value=content
end sub
-->
</script>

查询表达式生成器:</td></tr>
<tr><td>
<table border=1 name="SeeWin" style="display" bordercolorlight="#000000" cellspacing="1" bordercolordark="#FFFFFF"><tr><td bgcolor="#C0C0C0">
<select size="1" name="fld" value=<%=request.form("fld")%>>
<% if request.form("fld")<>"" then response.write "<option selected name=sel>"&request.form("fld")&"</option>"
For Each opt In rs.Fields
zdm=opt.name
zdlx=rs(zdm).type
response.write "<option value="&zdm&" name="&zdlx&">"&zdm&"</option>"
next
%>
</select>


<input type="text" size=5 name="sx" style="display:none">


<select size="1" name="opt" style="display:none" value=<%=request.form("opt")%>>
<option value="=" selected>等于</option>
<option value="=">等于</option>
<option value="<>">不等于</option>
<option value=">">大于</option>
<option value=">=">大于或等于</option>
<option value="<">小于</option>
<option value="<=">小于或等于</option>
<option value=" Like ">匹配</option>
</select>

<input type="text" name="valu" size="26" style="display:none" onFocus="jscript:this.value='';" value=<%=request.form("valu")%> >
<input type="button" name="BtnAnd" style="display:none" value="而且">
<input type="button" name="BtnOr" style="display:none" value="或者">
<input type="button" name="BtnEnd" style="display:none" value="结束">
</td></tr>



<tr><td bgcolor="#E0E8F0"><!--表达式显示区-->
<textarea rows="3" cols="80" name="S1" style="display"><%=request.form("s1")%></textarea><br>
</td>
<tr><td>
<p align="right"><!--排序区--> 排序按<select size="1" name="fldsort">
<option value="无" selected>(无)</option>
<%
For Each opt In rs.Fields
zdm=opt.name
response.write "<option value="&zdm&">"&zdm&"</option>"
next
%>
</select>
<select size="1" name="udSort"><option value="" selected>从小到大</option><option value=" desc">从大到小</option></select>
</td>
<tr><td><!--提交重置区-->
<input type="button" value="使用说明" name="btnhelp">
<input type="button" value="重置" name="reset">
<input type="button" value="全部记录" name="AllBtn">
<input type="button" value="提交表达式" name="tj" style="display:none"><br>
</td></tr>

</table>
</td></tr></form></table>