查询一条记录,按所选择的字段输出。
2010-8-10编辑:feimeimei
有以下表单,想查询一条记录,按所选择的字段输出(字段名fieldname1,fieldname2...请按你表中的字段名指定):
input.htm
form method=Post action=test.asp name=form1
input type=text name=bianhao size=10
input type=checkbox name=c1 value=fieldname1
input type=checkbox name=c1 value=fieldname2
input type=submit value=提交
/form
对应的ASP文件如下,这个代码的通用性很强(数据库mymdb.mdb你自己指定,表名tablename你自己指定):
test.asp
%
Dim fieldnames,fields
fieldnames =
‘遍历表单checkbox,将选择的字段存入字符串变量filednames
For Each fieldname In Request
fieldnames = fieldnames , fieldname
Next
‘判断是否有选项
If fieldnames = Then
Response.Write centerh2你至少应选择一个查询字段!/h2/center
Response.End
End If
‘连接数据库,打开记录集Rs
StrSql = Select Mid From tablename Where bianhao=‘ Request ‘
StrConn = DRIVER=Microsoft Access Driver ;DBQ= Server.MapPath
Set Rs = Server.CreateObject
Rs.Open StrSql,StrConn,1,1
‘分解字符串变量filednames,按选择的字段输出结果
fields = Split
For i = 1 To UBound
Response.Write fields : Rs) br
Next
Rs.Close
%