Public Function GetRandomChat(strtblChats As String, strAgent As String) As Long
[COLOR="green"] 'This takes the chat table as a string and the agent name as parameters for the function.[/COLOR]
Dim rs As DAO.Recordset
Dim last As Long
Dim sSql As String
sSql = "SELECT * FROM " & strtblChats & " WHERE Agent = '" & strAgent & "' ;"
Debug.Print sSql
[COLOR="green"] 'Open a snapshot of the records in the table based on the query above[/COLOR]
Set rs = DBEngine(0)(0).OpenRecordset(sSql, dbOpenSnapshot)
rs.MoveLast
rs.MoveFirst
last = rs.RecordCount
[COLOR="green"] 'Using the number of records from the record count - get a random number between the first and last record[/COLOR]
rs.Move (RandomNumber(1, last))
[COLOR="green"] 'Go to that record[/COLOR]
[COLOR="green"] 'Store the chat Id as the functions return value[/COLOR]
GetRandomChat = rs(0)
rs.Close
Set rs = Nothing
End Function
[COLOR="green"]'Code gets a random chat from the database.[/COLOR]
Private Sub buttonGetChat_Click()
Dim rs As DAO.Recordset
Dim strSQL As String
strSQL = "SELECT [ChatID],[Agent],[DateTime],[Duration] Where Chat_ID = (GetRandomChat('tblChats' , " & Chr(34) & Me.cboAgent.Column(0) & Chr(34) & ") ;"
Debug.Print strSQL
[COLOR="Green"] ' The string above should get a single record based on the chat ID returned from the function
' Using that recordset populate your controls.[/COLOR]
Set rs = DBEngine(0)(0).OpenRecordset(strSQL, dbOpenSnapshot)
Me![cboChatID] = rs(0)
Me![cboAgent2] = rs(1)
Me![cboChatDate] = rs(2)
Me![cboDuration] = rs(3)
rs.Close
Set rs = Nothing
End Sub