View Full Version : [Access 2k3] Macro for sending mail if data ?


Jean-Didier
08-05-2010, 06:58 AM
Hello

I need you help please.


i have made an unmatched query and in my macro, i would like the database send me an email with an excel file, ONLY if there are some data in the query.
If the query is empty, no email

i've found this code but i dont know where i can put this code.. in a module, in a macro ?


Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordSet("QueryName")

rs.MoveLast
rs.MoveFirst

If rs.RecordCount = 0 Then
Exit Sub
Else
' Put your sendobject code here
End If


I'm a beginner in access, thank you for your help

Jean-Didier (and sorry for my very bad english ...)

vbaInet
08-05-2010, 08:25 AM
Use DCount() to count how many records are in the query. If it's not 0 then e-mail.

Here's a link on DCount():

http://www.techonthenet.com/access/functions/domain/dcount.php

Jean-Didier
08-05-2010, 09:19 AM
Hello

It's OK.. But do you know if it's possible to use multiple crieria for this Dcount ?

Something Like This :
And(Dcount("*","Tab_ST_PDE")<1),Dcount("*","Qry_Incoherence")=0)

Only if this 2 conditions are True, the macro can run
I've try this code but it's not working....

vbaInet
08-05-2010, 09:22 AM
No point checking if DCount() is less than 1. See what the help files explains.

The structure is:

If DCount(...) <> 0 AND DCount()<> 0 then

That's how both would look.