View Single Post ยป More
Old 06-24-2014, 10:06 PM   #2 (permalink)
Blackened
 
Status: Senior Member
Posts: 101



Default find a random record

If you move to an invalid record without having set the bookmark property (or moving to a valid record) then you'll get the 'no current record' error. It's kind of an annoying error because it'll seem like you're on a valid record, but it'll keep throwing the error until you close/reopen the form, or manually move to another record. Also, you need to use the randomize statement to keep from getting the same sequence over and over. (Technically, I don't think this code should be able to move to an invalid record, unless the recordset is empty, so the bookmark might not be necessary).



Private Sub RandomWOD_Click()
Randomize
Dim RecordNumber As Long
Dim bMark As Variant 'to store bookmark
RecordNumber = CLng(Rnd() * Me.Recordset.RecordCount)
With Me.Recordset
bMark = .Bookmark
DoCmd.GoToRecord , , acFirst
.Move RecordNumber
If .EOF Or .BOF Then
.Bookmark = bMark
End If
End With
End Sub
Blackened is offline   Reply With Quote
more..