主题:  关于随机显示记录!!!

凡仔

职务:普通成员
等级:1
金币:0.0
发贴:66
注册:2003/1/10 5:51:46
#12003/3/19 19:32:56
这是一段在记录集中随机取记录的代码,是一个插件中得到的 MX575688_MoveToRandomMX.mxp

<%
' Moving to random record - Steven Jones' Extension
If Not(记录集名称.bof and 记录集名称.eof) Then
' reset the cursor to the beginning
If (记录集名称.CursorType > 0) Then
记录集名称.MoveFirst
Else
记录集名称.Requery
End If

记录集名称_totalrn = -1
记录集名称_totalrn = 记录集名称.RecordCount ' ony works on some recordsets, but much faster
If (记录集名称_totalrn = -1) Then ' and if it didn't work, we still have to count the records.

' count the total records by iterating through the recordset
记录集名称_totalrn=0
While (Not 记录集名称.EOF)
记录集名称_totalrn = 记录集名称_totalrn + 1
记录集名称.MoveNext
Wend

' reset the cursor to the beginning
If (记录集名称.CursorType > 0) Then
记录集名称.MoveFirst
Else
记录集名称.Requery
End If

End If

' now do final adjustments, and move to the random record
记录集名称_totalrn = 记录集名称_totalrn - 1
If 记录集名称_totalrn > 0 Then
Randomize
记录集名称.Move Int((记录集名称_totalrn + 1) * Rnd)
End If
End If
' all done; you should always check for an empty recordset before displaying data
%>




这段代码是在面包的技巧文摘里找到的,试过了,有用,但偶有重复记录

而现在我只想显示一条记录,所以没有重复的现象,但是,永远都是第一条记录或最后一条记录,不会变化了,请问这要怎么解决呀~:)



5D荣誉斑竹

职务:普通成员
等级:2
金币:10.0
发贴:710
注册:2001/10/24 18:34:42
#22003/3/20 0:17:44
如果你使用MSSQL数据库,直接使用 order by NewID() 就可以实现随机排序了



凡仔

职务:普通成员
等级:1
金币:0.0
发贴:66
注册:2003/1/10 5:51:46
#32003/3/20 3:02:30
问题是我用的是acc呀~~~

能不能帮忙解决上面单条记录随机显示的问题~~~:)

好多谢你呀~



5D荣誉斑竹

职务:普通成员
等级:2
金币:10.0
发贴:710
注册:2001/10/24 18:34:42
#42003/3/20 9:00:13
那就很麻烦了,首先你可以使用一个随机数(可以用Rnd函数)作为ID去取出数据,但是有可能取到相同的数据,所以你还要把已经取过的数据做一个筛选。

随机数:
Dim intRndID
Randomize '初始化随机数生成器
intRndID = Int((6 * Rnd) + 1) ' 产生 1 到 6 之间的随机数,具体范围可以自己改

取数据:
select * from Tb where ID="&intRndID&" and NOT(ID IN (已经选择的ID列表))



凡仔

职务:普通成员
等级:1
金币:0.0
发贴:66
注册:2003/1/10 5:51:46
#52003/3/20 16:53:41
and NOT(ID IN (已经选择的ID列表))

这句什么意思呀~~~:)不好意思~



5D荣誉斑竹

职务:普通成员
等级:2
金币:10.0
发贴:710
注册:2001/10/24 18:34:42
#62003/3/20 18:56:23
比如说,你已经选择了ID有1、3和5,那把这些ID形成一个字符串:

1,3,5

注意:一定要用“,”(不是中文的逗号)分开

这个就是上面的“已经选择的ID列表”