Conditional Function (1 Viewer)

alexfwalker81

Member
Local time
Today, 10:31
Joined
Feb 26, 2016
Messages
93
I'm using the function below to send email messages internally, and it works really well. However, I don't know the syntax to add into this which would operate like; IF Query12345 is empty THEN don't send the email ELSE send the email.

How would I amend this?


Public Function SendEmailAWALKE()

Dim mail As CDO.MESSAGE
Dim config As CDO.Configuration

Set mail = CreateObject("CDO.Message")
Set config = CreateObject("CDO.Configuration")

config.Fields(cdoSendUsingMethod).Value = cdoSendUsingPort
config.Fields(cdoSMTPServer).Value = "10.0.0.XXX"
config.Fields(cdoSMTPServerPort).Value = 25
config.Fields.Update

Set mail.Configuration = config

With mail
.To = "aXXXXXXr@vXXXXXXd.com"
.From = "pipeline@vXXXXXXd.com"
.Subject = "AWALKE Your Callbacks for Today"
.TextBody = "Callback list attached."

.AddAttachment "\\XXXXX\databases\PipeLine\Emails\AWALKE.xlsx"

.Send
End With

Set config = Nothing
Set mail = Nothing

End Function
 

alexfwalker81

Member
Local time
Today, 10:31
Joined
Feb 26, 2016
Messages
93
You would test Query12345 with a DCount (https://www.techonthenet.com/access/functions/domain/dcount.php). I would put that test around whatever code calls SendEmailAWALKE(), not in SendEmailAWALKE itself:

Code:
Public Function CallingFunction()
  ...
  If (Dcount(...)>0) Then SendEmailAWALKE()
...

End Function
Nice one. I think I can slot that in somewhere!
 

alexfwalker81

Member
Local time
Today, 10:31
Joined
Feb 26, 2016
Messages
93

Gasman

Enthusiastic Amateur
Local time
Today, 18:31
Joined
Sep 21, 2011
Messages
14,272
Put the >0 outside of the DCount() function.

FWIW you can just copy an paste a picture here now.

To save others having to use the link

1599898255208.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:31
Joined
May 7, 2009
Messages
19,237
Code:
Public Function SendEmailAWALKE()


    Dim mail As CDO.MESSAGE
    Dim config As CDO.Configuration

    If DCount("1", "Query12345") > 0 Then
    
        Set mail = CreateObject("CDO.Message")
        Set config = CreateObject("CDO.Configuration")

        config.Fields(cdoSendUsingMethod).Value = cdoSendUsingPort
        config.Fields(cdoSMTPServer).Value = "10.0.0.XXX"
        config.Fields(cdoSMTPServerPort).Value = 25
        config.Fields.Update

        Set mail.Configuration = config

        With mail
            .To = "aXXXXXXr@vXXXXXXd.com"
            .From = "pipeline@vXXXXXXd.com"
            .Subject = "AWALKE Your Callbacks for Today"
            .TextBody = "Callback list attached."

            .AddAttachment "\\XXXXX\databases\PipeLine\Emails\AWALKE.xlsx"

            .Send
        End With

        Set config = Nothing
        Set mail = Nothing
    
    End If
End Function
 

alexfwalker81

Member
Local time
Today, 10:31
Joined
Feb 26, 2016
Messages
93
Code:
Public Function SendEmailAWALKE()


    Dim mail As CDO.MESSAGE
    Dim config As CDO.Configuration

    If DCount("1", "Query12345") > 0 Then
   
        Set mail = CreateObject("CDO.Message")
        Set config = CreateObject("CDO.Configuration")

        config.Fields(cdoSendUsingMethod).Value = cdoSendUsingPort
        config.Fields(cdoSMTPServer).Value = "10.0.0.XXX"
        config.Fields(cdoSMTPServerPort).Value = 25
        config.Fields.Update

        Set mail.Configuration = config

        With mail
            .To = "aXXXXXXr@vXXXXXXd.com"
            .From = "pipeline@vXXXXXXd.com"
            .Subject = "AWALKE Your Callbacks for Today"
            .TextBody = "Callback list attached."

            .AddAttachment "\\XXXXX\databases\PipeLine\Emails\AWALKE.xlsx"

            .Send
        End With

        Set config = Nothing
        Set mail = Nothing
   
    End If
End Function
So simple - thank you, this worked brilliantly. Also just tried wrapping the .AddAttachment in an If statement to see if I could conditionally turn the attachments on and off. Also worked!
 

Users who are viewing this thread

Top Bottom