Playing with Emails! (1 Viewer)

Miked13

Registered User.
Local time
Today, 04:17
Joined
Jul 17, 2017
Messages
22
Hello Everyone!

Today I am exploring the wonderful world of sending Emails automatically.

Please see code below:
Code:
Private Sub btnEnter_Click()
        If (DCount("*", "qryDuplicateRequest") = 0) Then
            DoCmd.GoToRecord acForm, "frmRequest", acNewRec
        End If
    DoCmd.Close acQuery, "qryDuplicateRequest"
    Me.Refresh
    Me.txtemail.Requery

        Dim varName As Variant
        Dim varCC As Variant
        Dim varSubject As Variant
        Dim varBody As Variant

            [B][U]varName = Me.txtemail.Value[/U][/B]
            varCC = "NFCCYCLECOUNTERS@unicarriersamericas.com, NFCPARTSHORTAGE@unicarriersamericas.com"
            varSubject = "IVR Request has been submitted."
            varBody = "Thank you for your request, your part has been added to the list."
        DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False

Me.txtEmail.value 's Control Source is "=[txtRequester].[column](1)".

the problem I am having is the email will pop up with NO To: person in the first line, but the CC: is working fine.(I have the same code working in another form, but doesn't have the special control source.)

Any ideas as to why this is happening and any way of working around this?

Thank you!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:17
Joined
Aug 30, 2003
Messages
36,124
I assume the textbox shows the email? You might go straight to the source:

varName = Me.[txtRequester].[column](1)
 

Miked13

Registered User.
Local time
Today, 04:17
Joined
Jul 17, 2017
Messages
22
Tried varName = Me.[txtRequester].[column](1), same thing. thanks!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:17
Joined
Aug 30, 2003
Messages
36,124
Are we sure that column reference pulls the correct data? Can you attach the db here?
 

Mark_

Longboard on the internet
Local time
Today, 02:17
Joined
Sep 12, 2017
Messages
2,111
Just a suggestion for debugging, add the line in RED below;

Code:
Private Sub btnEnter_Click()
        If (DCount("*", "qryDuplicateRequest") = 0) Then
            DoCmd.GoToRecord acForm, "frmRequest", acNewRec
        End If
    DoCmd.Close acQuery, "qryDuplicateRequest"
    Me.Refresh
    Me.txtemail.Requery

        Dim varName As Variant
        Dim varCC As Variant
        Dim varSubject As Variant
        Dim varBody As Variant

            varName = Me.txtemail.Value

            [COLOR="Red"]msgbox "EMail address is " & varName[/COLOR]

            varCC = "NFCCYCLECOUNTERS@unicarriersamericas.com, NFCPARTSHORTAGE@unicarriersamericas.com"
            varSubject = "IVR Request has been submitted."
            varBody = "Thank you for your request, your part has been added to the list."
        DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False

Please make sure you are really using what you think you are using for the email address.
 

Miked13

Registered User.
Local time
Today, 04:17
Joined
Jul 17, 2017
Messages
22
Yes Pbaldy, i have confirmed the email address is the correct data in that column(text box is visible for checking).

Thanks Mark, always forget about using message boxes for these things. It is pulling up blank. I think i know whats wrong, let me try a few things and get back to you all next week.

have a great weekend!
 

Mark_

Longboard on the internet
Local time
Today, 02:17
Joined
Sep 12, 2017
Messages
2,111
Just quickly,

Try replacing
varName = Me.txtemail.Value
with
varName = Me.txtemail

I remember running into an issue like this, but I forget if that was the exact problem.
 

Miked13

Registered User.
Local time
Today, 04:17
Joined
Jul 17, 2017
Messages
22
Sadly, i have yet to be victorious on my adventure.

I have changed to simply trying to pull data from the form through message boxes.

I have added this line for testing.

Msgbox "You have sucessfully added " & cboPartNumber.value & " to the list"

Everything works, but the value is not listed in the string :(
I have tried this with text boxes as well and still nothing.

Has anyone ever had this problem or any ideas as to what may be causing it?

Thanks again!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:17
Joined
Aug 30, 2003
Messages
36,124
The only time was when the control in question had not been committed (hadn't hit enter/tab/clicked elsewhere). Can you attach the db here?
 

Mark_

Longboard on the internet
Local time
Today, 02:17
Joined
Sep 12, 2017
Messages
2,111
Miked13,

I've recently had to build a little utility to send Emails to a list pulled from a file. I'll attach my code so you can see what I was doing.

Code:
Option Compare Database
'-------------------------------------------------------------------------
Private Sub Btn_Attachment_Click()
    'Use fileDialog to allow user to select an attachment to send.
    'Use Me.Lbl_Attachment.Caption to show file attached.
    
    Dim aoFileDialog As Office.FileDialog
    Dim varFile As Variant
    
    Me.Lbl_Attachment.Caption = ""
    
    Set aoFileDialog = Application.FileDialog(msoFileDialogFilePicker)
    
    With aoFileDialog
       .TITLE = "Select Attachment"
       .AllowMultiSelect = False
       .Filters.Clear
       If .Show = True Then
          Me.Lbl_Attachment.Caption = .SelectedItems(1)
       End If
    End With
End Sub
'-------------------------------------------------------------------------
Private Sub CboRecipients_AfterUpdate()
    'Allow user to select class from drop down. Put ID (2nd column) on screen
    'so when they complain its not working I can find out why.
    Me.L_ClassID.Caption = Me.CboRecipients.Column(1)
End Sub
'-------------------------------------------------------------------------
Private Sub Cmd_Send_Click()
   Dim asSQL As String
   Dim rs As DAO.Recordset
   Dim afError As Byte
   Dim asError As String
      
   afError = False
   asError = ""
   If Me.L_ClassID.Caption = "Class ID" Then ' If no class selected
      asError = " No class selected"
      afError = True
   End If
   If Nz(Me.TxtSubject, "") = "" Then ' Or no subject entered
      asError = asError & " No Subject"
      afError = True
   End If
   If Nz(Me.TxtMessage, "") = "" Then ' Or no message entered
      asError = asError & " No Message"
      afError = True
   End If
   
   If afError = True Then ' If we can't send an Email, complain and leave.
      MsgBox asError & Chr(13) & Chr(10) & "Cannot send Email yet"
      Exit Sub
   End If
   
   'Begin going through the students by class, matching selected class.
   asSQL = "SELECT T_Nursing.Email FROM T_Nursing WHERE T_Nursing.Class = " & Me.L_ClassID.Caption
   'MsgBox "SQL is " & asSQL
   Set rs = CurrentDb.OpenRecordset(asSQL)
   
   'Check to see if the recordset actually contains rows
   If Not (rs.EOF And rs.BOF) Then
       rs.MoveFirst 'Unnecessary in this case, but still a good habit
       Do Until rs.EOF = True
          Send_Email (rs("EMail")) 'Use the EMail field in the record set to send.
          'Move to the next record. Don't ever forget to do this.
          rs.MoveNext
       Loop
   Else
       MsgBox "There are no records in the recordset."
   End If

   'MsgBox "Finished looping through records."

   rs.Close 'Close the recordset
   Set rs = Nothing 'Clean up
End Sub
'-------------------------------------------------------------------------
Private Sub Send_Email(pvMailAddress As Variant)

    Dim EmailApp As Outlook.Application
    Dim NameSpace As Outlook.NameSpace
    Dim Folder As Outlook.Folder
    Dim EmailSend As Outlook.MailItem
          
    'MsgBox "Sending EMail to " & pvMailAddress
        
    'create email
    Set EmailApp = New Outlook.Application
    Set NameSpace = EmailApp.GetNamespace("MAPI")
    Set Folder = NameSpace.GetDefaultFolder(olFolderInbox)
    Set EmailSend = Folder.Items.Add(olMailItem)
    
    With EmailSend
        .To = pvMailAddress
        .Subject = Me.TxtSubject
        .HTMLBody = Me.TxtMessage
        .ReadReceiptRequested = False
        If Not IsNull(Dt_Send) Then
           .DeferredDeliveryTime = Me.Dt_Send
        End If
        If Me.Lbl_Attachment.Caption <> "" Then
           .Attachments.Add (Me.Lbl_Attachment.Caption)
        End If
        .Display
        '.Send
    End With
    
End Sub

Me.TxtSubject is the message Subject
Me.TxtBody is the body of the message
Me.Dt_Send is a DATE (Date and time) the message should be sent, optional.
I use the fileDialog to fill in the caption to a label so I can show the user what file they will send. Since I've already filled that in, I'm reusing.
I use a drop down to identify which class is being Emailed. As I've had problems in the past with users not being clear on WHO they want to receive the Emails, I show the ID so I can make sure who they really did select. As it is on the screen already I'm reusing it.

Hopefully you can use this for your own needs.
 

Miked13

Registered User.
Local time
Today, 04:17
Joined
Jul 17, 2017
Messages
22
(Solved)Playing with Emails!

Thank you all for the help. I solved the issue by using a hidden form with the values of the primary form. Still dont know why the code wasnt working tho.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:17
Joined
Aug 30, 2003
Messages
36,124
Glad you got it sorted. I don't think I've ever had that problem.
 

Miked13

Registered User.
Local time
Today, 04:17
Joined
Jul 17, 2017
Messages
22
PBaldy, Yeah, it was an odd one. Best way i can explain it, for some reason "me.txtbox.value" wouldn't not pull the value into an outlook email generator or message boxes. But it worked in showing up in a hidden form, so I used that. lol.
 

Users who are viewing this thread

Top Bottom