Random Selection of Message to Display

  • Thread starter Thread starter ake
  • Start date Start date
A

ake

Please help me, I want to randomly display a message that is stored in a
table once the Startup form pops out. Please help me do that
 
For one reason or another I couldn't get your question out of my head last
night and so I created a simple little function to pull a random record
field. It basically determine the number or records in a table then generate
a random number based on that value and pull the data.

Function GetRndRec()
On Error GoTo Error_Handler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim tblName As String 'Table to pull random record from
Dim iRecCount As Long 'Number of record in the table
Dim iRndRecNum As Integer

tblName = "YourTableName"
Set db = CurrentDb()
Set rs = db.OpenRecordset(tblName, dbOpenSnapshot, dbReadOnly, dbReadOnly)

If rs.RecordCount <> 0 Then 'ensure there are records in the table
before proceeding
With rs
rs.MoveLast 'move to the end to ensure accurate recordcount
value
iRecCount = rs.RecordCount
iRndRecNum = Int((iRecCount - 1 + 1) * Rnd + 1) 'Get Random Rec
Number to use
rs.MoveFirst
.Move CLng(iRndRecNum)
GetRndRec = ![YourFieldName]
End With
End If

'Cleanup
rs.Close
Set rs = Nothing
Set db = Nothing

If Err.Number = 0 Then Exit Function

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: GetRndRec" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Function
End Function
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 
Hi Daniel, Thanks,I am just starting to learn about access programming this
looks so complicated to me that I can't get myself start to work on it but I
will surely try and will let you know. I hope you get your sleep back. I owe
you one :-)

Daniel Pineault said:
For one reason or another I couldn't get your question out of my head last
night and so I created a simple little function to pull a random record
field. It basically determine the number or records in a table then generate
a random number based on that value and pull the data.

Function GetRndRec()
On Error GoTo Error_Handler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim tblName As String 'Table to pull random record from
Dim iRecCount As Long 'Number of record in the table
Dim iRndRecNum As Integer

tblName = "YourTableName"
Set db = CurrentDb()
Set rs = db.OpenRecordset(tblName, dbOpenSnapshot, dbReadOnly, dbReadOnly)

If rs.RecordCount <> 0 Then 'ensure there are records in the table
before proceeding
With rs
rs.MoveLast 'move to the end to ensure accurate recordcount
value
iRecCount = rs.RecordCount
iRndRecNum = Int((iRecCount - 1 + 1) * Rnd + 1) 'Get Random Rec
Number to use
rs.MoveFirst
.Move CLng(iRndRecNum)
GetRndRec = ![YourFieldName]
End With
End If

'Cleanup
rs.Close
Set rs = Nothing
Set db = Nothing

If Err.Number = 0 Then Exit Function

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: GetRndRec" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Function
End Function
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.



ake said:
Please help me, I want to randomly display a message that is stored in a
table once the Startup form pops out. Please help me do that
 
Back
Top