Solved

Access - VB to auto email and text - Erroring

Posted on 2014-12-09
7
161 Views
Last Modified: 2014-12-09
Hello Experts....
I have a VB code to automatically email and text the Manager information at the end of any shift.  However, I'm getting an Error about Object no longer being set .  Sometimes it happens in the Email Section of the code and sometimes it's in the Texting portion of the code.  (don't know why sometimes it's one and sometimes it's the other...)

The first sending set starts at line 57

I suspect it hasn't sent the first one and is getting the second but that doesn't make sense why that would cause it to error.

Anyway the code I have is:
Private Sub SendMngrEmail()
Dim strSQL As String, strSQL2 As String, strSQL3 As String
Dim rs As DAO.Recordset, rs2 As DAO.Recordset, rs3 As DAO.Recordset
Dim a As String, b As String, c As String, d As String
Dim z As String, y1 As String, y2 As String, y3 As String, y4 As String, y5 As String
Dim q As String, r As String
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant

   On Error GoTo ProcError:

strSQL3 = "SELECT CompDBANameID, CompID, CompDBAEmailNotify, CompDBATextNotify FROM qry_CompDBANames"
Set rs3 = CurrentDb.OpenRecordset(strSQL3, dbOpenDynaset)
If rs3.RecordCount > 0 Then
    rs3.MoveLast
    rs3.MoveFirst
    With rs3
        If .RecordCount = 1 Then
            q = !CompDBAEmailNotify
            r = !CompDBATextNotify
        Else
            MsgBox "There are more than one possible DBA Company Names selected in the Code.  Ask the system administrator to fix this issue.", vbOKOnly, "Multiple DBA selected"
            q = !CompDBAEmailNotify & " FIX THE SELECT OF DBA NAME - MULTIPLES"
            r = !CompDBATextNotify & " FIX THE SELECT OF DBA NAME - MULTIPLES"
        End If
    End With
End If

    y1 = "AmtIn " & Format(Forms![frm_ShiftEndReportingNew]![sfrm_LVLInfo_DetailCtl].Form![sfrm_LVLInfo_ShiftEndNew].Form![sfrm_LVLInfoDetails].Form![txtInputAmtIn], "Currency")
    y2 = "AmtOut " & Format(Forms![frm_ShiftEndReportingNew]![sfrm_LVLInfo_DetailCtl].Form![sfrm_LVLInfo_ShiftEndNew].Form![sfrm_LVLInfoDetails].Form![txtInputAmtOut], "Currency")
    y3 = "NetAmt " & Format(Forms![frm_ShiftEndReportingNew]![sfrm_LVLInfo_DetailCtl].Form![sfrm_LVLInfo_ShiftEndNew].Form![sfrm_LVLInfoDetails].Form![txtInputNetAmt], "Currency")
    y4 = "AmtVal " & Format(Forms![frm_ShiftEndReportingNew]![sfrm_LVLInfo_DetailCtl].Form![sfrm_LVLInfo_ShiftEndNew].Form![sfrm_LVLInfoDetails].Form![txtInputAmtVal], "Currency")
    y5 = "Cash " & Format(Nz(GetcurShiftEndCash, 0), "Currency")
    
strbody = y1 & vbNewLine & y2 & vbNewLine & y3 & vbNewLine & y4 & vbNewLine & y5

' ******* Setup Email and Text Info to send
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

        iConf.Load -1    ' CDO Source Defaults
        Set Flds = iConf.Fields
        With Flds
        
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = "2"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.xyzabc.net"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "joejoe@xyzmail.com"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "abc123xyz"
            .Update
            
        End With

' **** Send Shift End Emails
' ***** Set Email Addresses to receive notice
    strSQL = "SELECT ShiftEndEmailNotifyID, ShiftEndEmailNotifyInactive, NotifyEmail, NickName, CompName " & _
    "FROM qry_ShiftEndEmailNotify " & _
    "WHERE ShiftEndEmailNotifyInactive=" & False
    
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
' ***** END Set Email Addresses to receive notice

If rs.RecordCount > 0 Then
    Do Until rs.EOF
        With rs
            a = joejoe@xyzmail.com" From
            b = rs!NotifyEmail 'To
            c = "Shift End - " & z & " - " & rs!CompName ' Subject
            d = strbody ' Message
            
            With iMsg
                Set .Configuration = iConf
                .To = b 
'                .CC = ""
'                .BCC = ""
                .From = "joejoe@xyzmail.com"
                .Subject = q 
                .TextBody = strbody
                .Send
            End With
            .MoveNext
        End With
    Loop
End If
' **** END Shift End Emails

' ***** Set Text Addresses to receive notice
    strSQL2 = "SELECT ShiftEndTextNotifyID, ShiftEndTextNotifyInactive, NotifyTextAddr, NickName, CompName " & _
    "FROM qry_ShiftEndTextNotify " & _
    "WHERE ShiftEndTextNotifyInactive=" & False
    
    Set rs2 = CurrentDb.OpenRecordset(strSQL2, dbOpenDynaset)
' ***** END Set Text Addresses to receive notice

' **** Send Shift End Text Messages
If rs2.RecordCount > 0 Then
    Do Until rs2.EOF
        With rs2
            a ="joejoe@xyzmail.com" 'From
            b = rs2!NotifyTextAddr 'To
            c = "Shift End - " & z & " - " & rs2!CompName ' Subject
            d = strbody ' Message
            
            With iMsg
                Set .Configuration = iConf
                .To = b 
               ' .CC = ""
               ' .BCC = ""
                 .From = a
                .Subject = r '"New figures"
                .TextBody = strbody 'strbody
                .Send
            End With
            .MoveNext
        End With
    Loop
End If
' **** END Shift End Text Messages

rs.Close
Set rs = Nothing
rs2.Close
Set rs2 = Nothing
rs3.Close
Set rs3 = Nothing

ProcError:
MsgBox "Error in Email Send"
Exit Sub
   
End Sub

Open in new window

0
Comment
Question by:wlwebb
  • 5
  • 2
7 Comments
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 40489736
Try simplifying the code:
Set it up to send a simple static text email and a simple static text message, ...

Something roughly like this pseudo code:

Dim strStaticEmailMsg as string
Dim strStaticTextMsg as string
strStaticEmailMsg ="Generic email text"
strStaticTextMsg ="Generic Text message text"
'Your code to send an email
'Your code to send a text

If this works, ...you can gradually add back in all your other coding (loops, ...etc) and see when this issue starts to occur.

Also try commenting out our error handling, to see exactly what line is failing.

...Lets also see if anther expert can spot something more specific...
0
 

Author Comment

by:wlwebb
ID: 40490179
Thanks... I'll give it a try
0
 

Author Comment

by:wlwebb
ID: 40490221
I copied all of the "Send Shift End Emails section .... I get the following error on the .Send line...

Run-time error '-2147220958 (80040222)
The pickup directory path is required and was not specified.

Private Sub cmdTestMngrEmail_Click()
Dim strSQL1 As String, strSQL2 As String, strSQL3 As String
Dim strSQL4 As String
Dim rs1 As DAO.Recordset, rs2 As DAO.Recordset, rs3 As DAO.Recordset
Dim rs4 As DAO.Recordset
Dim a As String, b As String, c As String, d As String
Dim z As String, y1 As String, y2 As String, y3 As String, y4 As String, y5 As String
Dim q As String, r As String
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Dim Cmpy As Long
Dim strText As String
Dim strEmail As String

strText = "Test Café Text"
strEmail = "Test Café Email"

' ***** GET EMAIL & TEXTING Info ****
Cmpy = GetlngMyCompID()

strSQL4 = "SELECT CompEmailAddrID, CompID, CompEmailAddr, CompEmailInactive, SendFromEmailAcct, TextFromEmailAcct, SendFromEmailAcctNbr, TextFromEmailAcctNbr, SendFromEmailAcctPW, SendFromTextAcctPW FROM sCtl_CompEmailAddresses WHERE CompID=" & Cmpy
Set rs4 = CurrentDb.OpenRecordset(strSQL4, dbOpenDynaset)

If rs4.RecordCount > 0 Then
    
Else
    MsgBox "The Company's Email and/or Texting defaults have not been defined." & vbNewLine & vbNewLine & "Please consult the System Administrator.", vbCritical + vbOKOnly, "Default Company Email & Text not defined!"
    Exit Sub
End If

' ******* Setup Email and Text Info to send
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

        iConf.Load -1    ' CDO Source Defaults
        Set Flds = iConf.Fields
        With Flds
        
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = "1"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.xyzmail.net"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "joejoe@xyz123mail.com"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "testing123"
            .Update
            
        End With

' **** Send Shift End Emails
' ***** Set Email Addresses to receive notice
    strSQL1 = "SELECT ShiftEndEmailNotifyID, ShiftEndEmailNotifyInactive, NotifyEmail, NickName, CompName " & _
    "FROM qry_ShiftEndEmailNotify " & _
    "WHERE ShiftEndEmailNotifyInactive=" & False
    
    Set rs1 = CurrentDb.OpenRecordset(strSQL1, dbOpenDynaset)
' ***** END Set Email Addresses to receive notice

If rs1.RecordCount > 0 Then
    Do Until rs1.EOF
        With rs1
            a = "joejoe@xyz123mail.com" ' From
            b = rs1!NotifyEmail 'To
            c = "Shift End - " & z & " - " & rs1!CompName ' Subject
            d = strbody ' Message
            
            With iMsg
                Set .Configuration = iConf
                .To = b
'                .CC = ""
'                .BCC = ""
                .FROM = a
                .Subject = q '"New figures"
                .TextBody = strText 'strbody
                .Send
            End With
            .MoveNext
        End With
    Loop
End If
' **** END Shift End Emails

' ***** Set Text Addresses to receive notice
    strSQL2 = "SELECT ShiftEndTextNotifyID, ShiftEndTextNotifyInactive, NotifyTextAddr, NickName, CompName " & _
    "FROM qry_ShiftEndTextNotify " & _
    "WHERE ShiftEndTextNotifyInactive=" & False
    
    Set rs2 = CurrentDb.OpenRecordset(strSQL2, dbOpenDynaset)
' ***** END Set Text Addresses to receive notice

' **** Send Shift End Text Messages
If rs2.RecordCount > 0 Then
    Do Until rs2.EOF
        With rs2
            a = "joejoe@xyz123mail.com" ' From
            b = rs2!NotifyTextAddr 'To
            c = "Shift End - " & z & " - " & rs2!CompName ' Subject
            d = strbody ' Message
            
            With iMsg
                Set .Configuration = iConf
                .To = b
                '.CC = ""
                '.BCC = ""
                .FROM = a
                .Subject = r '"New figures"
                .TextBody = strEmail 'strbody
                .Send
            End With
            .MoveNext
        End With
    Loop
End If
' **** END Shift End Text Messages

End Sub

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:wlwebb
ID: 40490226
OOPS.... I think the line 41 should be a "2"...
0
 

Author Comment

by:wlwebb
ID: 40490248
Ok.... Now that I've got that found another problem with it...... Has to do with a variable

The text string that is being pulled from the DB has quotations in the Name (yes I need the quotations)  so that it interprets the string coming in as

""xyz info""

Is there a way to allow the quotations to come in???????????
0
 

Author Closing Comment

by:wlwebb
ID: 40490266
Thanks Jeff.... I'll post a new question for the Variable issue
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40490540
ok
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now