Golom and PSV kindly helped in a previous thread to get some code working which automatically allocates clients/leads to a salesperson dependent on the salesperson with the least amount of clients/leads already allocated (and whether that salesperson is avialable). However, I needed to make sure that when the code is counting the number of leads/clients a salesperson already has it only counts ones with a 'status' of ACTIVE (which is stored as number 2). This is so that if a salesperson has hundreds of dead leads associated with them they are not counted - otherwise this salesperson will never come to the top of the list to get new cleints/leads allocated. The code directly below works beautifully but counts all leads - not just the ones with a status of ACTIVE.
Private Sub AllocateLeads_Click()
Dim db As DAO.Database
Dim LD As DAO.Recordset
Dim SP As DAO.Recordset
On Error GoTo BadNews
Set db = CurrentDb
' Get the Leads that do not have a Sales Person assigned.
Set LD = db.OpenRecordset( _
"Select ClientID " & _
"From Tbl_Client " & _
"Where Trim$(StaffAllocated & '') = ''", dbOpenSnapshot)
Do Until LD.EOF
' Find an available Sales Person with the fewest leads. If there's a tie then
' just arbitrarily pick one.
Set SP = db.OpenRecordset( _
"SELECT S.ID " & " FROM Tbl_Staff As S LEFT JOIN Tbl_Client As C" & _
" ON S.ID=C.StaffAllocated" & _
" WHERE S.Available=yes " & " GROUP BY S.ID" & _
" ORDER BY Count(C.StaffAllocated)")
' Assign this lead to the salesperson. The field StaffAllocated is
' updated to the sales person found in recordset SP. AND we are
' doing that to the Tbl_Client record for the client LD![ClientID]
Dim strID As String
strID = Replace(SP![ID], "'", "''")
db.Execute "UPDATE Tbl_Client" & _
" SET StaffAllocated='" & strID & "'" & _
",FirstContact='" & strID & "'" & _
"WHERE ClientID = " & LD![ClientID] & ";", dbFailOnError
SP.Close
Set SP = Nothing
LD.MoveNext
Loop
Exit_Sub:
MsgBox "All new leads have been allocated"
Set LD = Nothing
Set SP = Nothing
Set db = Nothing
Exit Sub
BadNews:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_Sub:
End Sub
We tried to change the code as follows but then the code allocated the same salesperson to every single new lead
Set SP = db.OpenRecordset( _
"SELECT S.ID FROM Tbl_Staff As S LEFT JOIN" & _
" (SELECT StaffAllocated FROM Tbl_Client WHERE status=2) As C" & _
" ON S.ID=C.StaffAllocated" & _
" WHERE S.Available=yes" & _
" GROUP BY S.ID" & _
" ORDER BY Count(C.StaffAllocated)")
Any help appreciated!
Private Sub AllocateLeads_Click()
Dim db As DAO.Database
Dim LD As DAO.Recordset
Dim SP As DAO.Recordset
On Error GoTo BadNews
Set db = CurrentDb
' Get the Leads that do not have a Sales Person assigned.
Set LD = db.OpenRecordset( _
"Select ClientID " & _
"From Tbl_Client " & _
"Where Trim$(StaffAllocated & '') = ''", dbOpenSnapshot)
Do Until LD.EOF
' Find an available Sales Person with the fewest leads. If there's a tie then
' just arbitrarily pick one.
Set SP = db.OpenRecordset( _
"SELECT S.ID " & " FROM Tbl_Staff As S LEFT JOIN Tbl_Client As C" & _
" ON S.ID=C.StaffAllocated" & _
" WHERE S.Available=yes " & " GROUP BY S.ID" & _
" ORDER BY Count(C.StaffAllocated)")
' Assign this lead to the salesperson. The field StaffAllocated is
' updated to the sales person found in recordset SP. AND we are
' doing that to the Tbl_Client record for the client LD![ClientID]
Dim strID As String
strID = Replace(SP![ID], "'", "''")
db.Execute "UPDATE Tbl_Client" & _
" SET StaffAllocated='" & strID & "'" & _
",FirstContact='" & strID & "'" & _
"WHERE ClientID = " & LD![ClientID] & ";", dbFailOnError
SP.Close
Set SP = Nothing
LD.MoveNext
Loop
Exit_Sub:
MsgBox "All new leads have been allocated"
Set LD = Nothing
Set SP = Nothing
Set db = Nothing
Exit Sub
BadNews:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_Sub:
End Sub
We tried to change the code as follows but then the code allocated the same salesperson to every single new lead
Set SP = db.OpenRecordset( _
"SELECT S.ID FROM Tbl_Staff As S LEFT JOIN" & _
" (SELECT StaffAllocated FROM Tbl_Client WHERE status=2) As C" & _
" ON S.ID=C.StaffAllocated" & _
" WHERE S.Available=yes" & _
" GROUP BY S.ID" & _
" ORDER BY Count(C.StaffAllocated)")
Any help appreciated!