Results 1 to 7 of 7
  1. #1
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105

    how to retrieve a value from a field by index?

    Hi,

    I have a random number generated based on a query's record count. I would then like to retrieve a field value from that query based on the index that is generated.



    The code below works and generates a random number based on the query's record count. But I do not know how to retrieve the EmployeeID field with the random index generated. I'm not sure if I need to open a recordset. I basically just want to go right to the record index that is generated and retrieve the EmployeeID, without having to loop through the records. Is this possible?

    Code:
    Private Sub btnRandomEmployeeName_Click()
        Dim randomEmployeeNum As Integer
        randomEmployeeNum = Int((EmployeeCount - 1 + 1) * Rnd + 1)
        MsgBox randomEmployeeNum
    End Sub
    
    Private Function EmployeeCount() As Integer
        EmployeeCount = DCount("[EmpID]", "qryActiveEmployees")
    End Function

  2. #2
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    From the horse's mouth!

    How to: Return a Random Record from a DAO Recordset
    Microsoft Access does not have a built-in mechanism for returning a random record from a set of records. This topic describes a sample user-defined function that you can use to return a random record.
    Function FindRandom (RecordSetName As String, Fieldname As String)

    Dim MyDB As Database
    Dim MyRS As Recordset
    Dim SpecificRecord As Long, i As Long, NumOfRecords As Long

    Set MyDB = CurrentDB()
    Set MyRS = MyDB.OpenRecordset(RecordSetName, dbOpenDynaset)
    On Error GoTo NoRecords
    MyRS.MoveLast
    NumOfRecords = MyRS.RecordCount
    SpecificRecord = Int(NumOfRecords * Rnd)
    If SpecificRecord = NumOfRecords Then
    SpecificRecord = SpecificRecord - 1
    End If
    MyRS.MoveFirst
    For i = 1 To SpecificRecord
    MyRS.MoveNext
    Next i
    FindRandom = MyRS(Fieldname)
    Exit Function

    NoRecords:
    If Err = 3021 Then
    MsgBox "There Are No Records In The Dynaset", 16, "Error"
    Else
    MsgBox "Error - " & Err & Chr$(13) & Chr$(10) & Error, _
    16, "Error"
    End If
    FindRandom = "No Records"
    Exit Function

    End Function



  3. #3
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105
    It works, thanks! I needed to change the following:

    Dim MyDB As DAO.Database
    Dim MyRS As DAO.Recordset

  4. #4
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105
    There is actually something about this function that is not random at all. Every time I close the program, and open it back up, it generates the same records. I remember something in VB about it going off the system time, so the only way it could repeat itself is if you call the function at exactly the same time every 24 hours, but what's going on here? All I have to do is close the program and open it back up and it will generate the exact same records as last time. Is there a variable or something I need to set it to ensure it's completely random?

  5. #5
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    It seems that even Microsoft doesn't know what it's doing. The sample code was taken straight from an MS web page but here are the notes related to Rnd:

    Remarks
    The Rnd function returns a value less than 1 but greater than or equal to zero.
    The value of number determines how Rnd generates a random number:
    For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.
    Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.
    I suggest therefore you place an Randomize statement before the use of Rnd

    Code:
    Randomize
    SpecificRecord = Int(NumOfRecords * Rnd)
    Sorry to mislead you.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  7. #7
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105
    Randomize worked, and thanks for the article. bookmarked

    Thanks!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Use DLookup to Retrieve Field Value from a Date?
    By Heatshiver in forum Programming
    Replies: 8
    Last Post: 04-06-2012, 10:09 AM
  2. Replies: 1
    Last Post: 03-09-2012, 07:43 PM
  3. Replies: 1
    Last Post: 05-17-2011, 05:19 AM
  4. How to retrieve field names in a table
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 12-05-2010, 09:09 PM
  5. Replies: 0
    Last Post: 06-03-2009, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums