我在实验Recordset.Movenext,Recordset.Previous,Recorset.......等移动记录的时候遇到了困难.
我用Access做后台数据库,通过一个查询条件查询得一个记录集,我想用一个表格实现记录的逐条浏览(不是一组记录分页浏览). 即"共查到**条记录,现在是第*条",单击"Next"按钮后下一条记录的内容显示在表格中.....
我用如下代码实现(见最后)
发现若将Rst.Open "select * from 某表 where 从某个Form获取的查询条件"
改成 Rst.Open "select * from 某表"(相当于不做查询动作)后, 记录的浏览功能才得以实现.
否则总是出现" EOF或BOF为真 "的错误提示.可我明明可以从"某表"中 "select... where..."到好几个记录的!
总之如何将"记录的逐条浏览"和"select * from 某表 where 从某个Form获取的查询条件" 结合起来?
望予以指点为感!
杨利
2000/4/13
程序代码:
1@ LANGUAGE=VBScript
1<html>
2<head>
3<title></title>
4</head>
5<body bgcolor="#FFFFF0">
6<h3 align="center"><font color="#004080" face="隶书"><big>现在您可以编辑以下记录</big></font></h3>
7<!-- 在服务器上创建 Connection 和 Recordset 对象 -->
'创建并打开 Connection 对象。
Set cn=Server.CreateObject("ADODB.Connection")
cn.Open "DSN=数据库名"
'创建并打开 Recordset 对象。
Set Rst = Server.CreateObject("ADODB.Recordset")
Rst.ActiveConnection = cn
Rst.CursorType = adOpenKeyset
Rst.LockType = adLockOptimistic
Rst.Open "select * from 某表 where 性别='"&request.form("t1")&"'"(执行这句大有问题)
Rst.Open "select * from 某表 where 性别='男'"(执行这句有点问题)
Rst.Open "select * from hr_base"(执行这句没有问题)
' 检查 Request.Form 集合以查看所记录的任何移动。
If Not IsEmpty(Request.Form("MoveAmount")) Then
' 跟踪该会话的移动数目和方向。
Session("Moves") = Session("Moves") + Request.Form("MoveAmount")
Clicks = Session("Moves")
'移动到上一个已知位置。
Rst.Move CInt(Clicks)
'检查移动为 + 还是 - 并进行错误检查。
If CInt(Request.Form("MoveAmount")) = 1 Then
If Rst.EOF Then
Session("Moves") = Rst.RecordCount
Rst.MoveLast
End If
Rst.MoveNext
End If
If Request.Form("MoveAmount") < 1 Then
Rst.MovePrevious
End If
'检查有无单击 First Record 或 Last Record 命令按钮。
If Request.Form("MoveLast") = 3 Then
Rst.MoveLast
Session("Moves") = Rst.RecordCount
End If
If Request.Form("MoveFirst") = 2 Then
Rst.MoveFirst
Session("Moves") = 1
End If
End If
' 对 Move Button 单击组合进行错误检查。
If Rst.EOF Then
Session("Moves") = Rst.RecordCount
Rst.MoveLast
Response.Write "This is the Last Record"
End If
If Rst.BOF Then
Session("Moves") = 1
Rst.MoveFirst
Response.Write "This is the First Record"
End If
1<!-- 显示当前记录数目和记录集大小-->
2<h3 align="center"><font color="#004080" face="隶书">共查到</font><font color="#600060">```
3=Rst.RecordCount
4```</font><font color="#004080" face="隶书">条记录,当前为第</font>
5<font color="#600060">
If IsEmpty(Session("Moves")) Then
Session("Moves") =1
End If
Response.Write(Session("Moves") )
1</font><font color="#004080" face="隶书">条记录</font></h3>
2<hr align="center"/>
3<p align="center"> <input name="cmdFirst" style="font-family: 宋体" type="button" value="第一条"/><input name="cmdDown" type="button" value="上一条"/><input name="cmdUp" type="button" value="下一条"/><input name="cmdLast" type="button" value="末一条"/> </p>
4<p align="center"><b><font color="#000080" face="隶书" size="5">查询结果:</font></b></p>
5<table>
6(用于逐条显示记录的表格)
7</table>
8<!-- 使用隐含窗体字段将值发送到服务器-->
9<form action="" method="Post" name="Form">
10<input name="MoveAmount" type="hidden" value="0"/><input name="MoveLast" type="hidden" value="0"/><input name="MoveFirst" type="hidden" value="0"/>
11</form>
12</body>
13<script language="VBScript">
14Sub cmdDown_OnClick
15'在 Input Boxes 窗体和 Submit 窗体中设置值。
16Document.Form.MoveAmount.Value = -1
17Document.Form.Submit
18End Sub
19Sub cmdUp_OnClick
20Document.Form.MoveAmount.Value = 1
21Document.Form.Submit
22End Sub
23Sub cmdFirst_OnClick
24Document.Form.MoveFirst.Value = 2 Document.Form.Submit
25End Sub
26Sub cmdLast_OnClick
27Document.Form.MoveLast.Value =3
28Document.Form.Submit
29End Sub
30</script>
31</html>