View Full Version : If no results don't send


dark11984
09-03-2008, 10:10 PM
I have a code that emails a query as an excel spreadsheet.
I want to be able to add more code at the start to tell it: if no results in the query than don't open the email window.

This is what i have so far:

Private Sub Send_TAM_Button_Click()
On Error GoTo Err_Send_TAM_Button_Click

DoCmd.SendObject ObjectType:=acSendQuery, _
ObjectName:="Unauthorised DC's - TAM Query", _
Outputformat:="Microsoft Excel 97-10(*.xls)", _
To:="xyz@hotmail.com", _
Subject:="Unauthorised DC's - TAM", _
MessageText:="This email is self generated - Please arrnage for the attached DC's to be authorised and printed.", _
EditMessage:=True '

Exit_Send_TAM_Button_click:
Exit Sub
Err_Send_TAM_Button_Click:
MsgBox Err.Description
Resume Exit_Send_TAM_Button_click
End Sub

John Big Booty
09-03-2008, 10:22 PM
Try testing

Me.RecordSet.RecordCount = 0

Uncle Gizmo
09-03-2008, 10:27 PM
You can feed any "SELECT" SQL code into this procedure and it will count the records, returning zero if no records are returned by the SQL statement.

Public Function fProcessSQL(strSQL As String) As Byte
'Input a "SELECT" String that needs counting
'Three Possible Values Returned to fProcessSQL
'0 = No Matching Record
'1 = One Matching Record
'2 = Two or More Matching Records

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.ActiveConnection = CurrentProject.Connection
rs.CursorType = adOpenKeyset
rs.Open strSQL

Select Case rs.RecordCount
Case Is = 0 '0 = No Matching Records
fProcessSQL = 0
rs.Close
Exit Function
Case Is = 1 '1 = One Matching Record
fProcessSQL = 1
rs.Close
Exit Function
Case Is > 1 '2 = Two or more Matching Records
fProcessSQL = 2
rs.Close
Exit Function
Case Else 'A value other than an Interger was returned by the rs.RecordCount (Should Never happen)
'MsgBox "From Case Else" 'If you suspect a problem then activate this message box
fProcessSQL = 0 '0 = No matching record
rs.Close
Exit Function
End Select
End Function 'fProcessSQL

MStef
09-03-2008, 11:33 PM
Try this code:

If DCount("*", "queryName") = 0 Then
MsgBox "No Records"
Exit Sub
End If
DoCmd.SendObject .......................