Can anyone tell me why this error handling won't work? I am trying to error the code out if nothing is selected in my listbox (AcctList). It gives me an invalid call procedure when nothing is selected and the error number is 5. Everything works perfect if I select an item.
Code:
Private Sub Command757_Click()
On Error GoTo Err_Command757_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM qrySendAcctEmployees"
'Build the IN string by looping through the listbox
For i = 0 To AcctList.ListCount - 1
If AcctList.Selected(i) Then
If AcctList.Column(0, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & AcctList.Column(0, i) & "',"
End If
Next i
On Error GoTo Err_Command757_Click
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [EID] in " & _
"(" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qrySendAcctEmployees2"
Set qdef = MyDB.CreateQueryDef("qrySendAcctEmployees2", strSQL)
'Open the query, built using the IN clause to set the criteria
'DoCmd.OpenQuery "qrySendAcctEmployees2", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.AcctList.ItemsSelected
Me.AcctList.Selected(varItem) = False
Next varItem
DoCmd.SendObject acQuery, "qrySendAcctEmployees2", "MicrosoftExcelBiff8(*.xls)", "", "", "", "Termed Employee(s) to be Processed", "Please process the attached employees for termination", True, ""
Exit_Command757_Click:
Exit Sub
Err_Command757_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list" _
, , "Selection Required !"
Resume Exit_Command757_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
'MsgBox Error$
Resume Exit_Command757_Click
End If
End Sub