很久没用access了,只是看到经常碰到有人问如何返回随机记录的问题,所以就贴了上来,随便看看。
<!–#include virtual="/adovbs.inc"–>
<%
dim objconn
dim objrst
dim strsql
dim strconnection
dim str
dim str1
dim cnt
dim cnt1
dim rndmax
dim rndnumber
strconnection="driver={microsoft access driver (*.mdb)};dbq=" & server.mappath("/testdb.mdb")
strsql = "select id from tblquestions"
set objconn = server.createobject("adodb.connection")
set objrst = server.createobject("adodb.recordset")
objconn.open strconnection
set objrst.activeconnection = objconn
objrst.locktype = adlockoptimistic
objrst.cursortype = adopenkeyset
objrst.open strsql
objrst.movelast
cnt = objrst.recordcount
cnt1 = cnt
rndmax = cnt
if cint(request.form("maxnumber")) < cnt then
cnt1 = cint(request.form("maxnumber"))
end if
str = ","
str1 = ","
do until cnt1 = 0
randomize
rndnumber = int(rnd * rndmax)
if (instr(1, str1, "," & rndnumber & "," ) = 0) then
str1 = str1 & rndnumber & ","
cnt1 = cnt1 – 1
objrst.movefirst
objrst.move rndnumber
str = str & objrst("id") & ","
end if
loop
objrst.close
set objrst = nothing
sql = "select * from tblquestions where (((instr(1," & str & ",(, & [id] & ,)))<>0)) "
set objrst = server.createobject("adodb.recordset")
set objrst.activeconnection = objconn
objrst.locktype = adlockoptimistic
objrst.cursortype = adopenkeyset
objrst.open sql
%>
…display the records returned…
<%
objrst.close
set objrst = nothing
objconn.close
set objconn = nothing
%>
