Question: I am trying to write a piece of VBA Code that will actuate when I open my report titled ‘Workers’. The Code is supposed to select three employees randomly and then filter (exclude) one of the employees based on their ID number.
I was able to create a query, switch to SQL view and use that statement as the model for the code below. I understand that I can make a query in Access but I am trying to reduce the query clutter in my database while increasing my skills and knowledge of VBA/Code. I had success making a DELETE query and think that it must be possible to make something like this but I have a limited understanding.
I have read in the forum that you cannot use a SELECT query in VBA but there has to be some work around for the desired results.
Private Sub Report_Open(Cancel As Integer)
Dim strQuery As String
strQuery = "SELECT TOP 3 Workers.ID, Workers.FullName, Rnd([ID]) AS Expr1 FROM Workers WHERE (((Workers.ID)<>9)) ORDER BY Rnd([ID]);"
DoCmd.RunSQL strQuery
End Sub
Any help with this would be great. Please see attached file.