前两天发了一条帖子,没有实例,今天补上!问题是:在查询同类数据库信息时,如何实现对同类数据库信息的随机查询。见实例:13个同龄(15岁)的同学,要“随机选取”10个同龄(15岁)的同学。谢谢各位老师!也顺便祝各位老师们新年愉快!见附件。作者: 玉树TMD临风 时间: 2014-1-16 19:39
Select TOP 10 * From TABLE Order BY Rnd(Len(姓名)) 试试作者: lynnwang 时间: 2014-1-16 21:23
看试简单,实际操作起来,虽然代码都很简单,但还真有点步骤。作者: lynnwang 时间: 2014-1-16 21:53
Private Sub Command4_Click()
Dim i As Long, cnt As Long, n As Long
Dim rst As DAO.Recordset
Const SelNumbers As Long = 5
Set rst = sfm学生表.Form.Recordset.Clone
cnt = rst.RecordCount
Randomize
For i = 1 To SelNumbers
n = cnt - (Int(cnt * Rnd) + rst.AbsolutePosition)
Debug.Print n
If n <> 0 Then
rst.Move n
End If
Debug.Print "随机记录ID"; rst.Fields(0).Value
Next
'将选出的“随机记录ID”组成SELECT查询语句条件,即可
rst.Close: Set rst = Nothing
End Sub作者: lynnwang 时间: 2014-1-16 22:01
这里主要应用了RECORDSET对象的Clone和MOVE方法
问题1用到的
'--------------------------------------------------
'Purpose: 检查指定的值是否在列表中
'Input:
' [in] FindVal: 需要查找的值
'Output: (Boolean)
'--------------------------------------------------
Public Function IsInList(ArrayList() As Long, ByVal FindVal As Long) As Boolean
Dim i As Long
For i = LBound(ArrayList) To (UBound(ArrayList))
If ArrayList(i) = FindVal Then IsInList = Not IsInList: Exit Function
Next i
End Function作者: 程研 时间: 2014-1-17 11:06
Private Sub Command4_Click()
Me.学生表_子窗体.Form.RecordSource = "Select TOP 5 * From 学生表 where 年龄=" & Me.Text2.Value & " Order BY Rnd(Len(姓名))"
Me.学生表_子窗体.Form.Requery
End Sub