Sending group email.

psamu

Active Member
Joined
Jan 3, 2007
Messages
462
I have following code, It works, but only problem I have is when Fields (1) or Fields (3) is empty ( means no email address) then it throws error.
In ON CLICK event
Dim PassWord As String
PassWord = InputBox("Enter Password")
If PassWord = "Hello" Then
' Open Form



'...
Dim MyDb As DAO.Database
Dim rsEmail As DAO.Recordset
Dim sToName As String
Dim sCcName As String
Dim sSubject As String
Dim sMessageBody As String

Set MyDb = CurrentDb()
Set rsEmail = MyDb.OpenRecordset("Followup Email", dbOpenSnapshot)

With rsEmail
.MoveFirst
Do Until rsEmail.EOF
If IsNull(.Fields(0)) = False Then
sToName = .Fields(0) & ";" & .Fields(1)
sCcName = .Fields(2) & ";" & .Fields(3)
sSubject = "Reminder #: " & .Fields(4)
sMessageBody = "Reminder: Please check this item assigned to you showing as Overdue " & vbCrLf & _
"Received From: " & .Fields(5) & ", Due Date: " & .Fields(10) & ", " & .Fields(9) & vbCrLf & _
"Message: " & .Fields(7)

DoCmd.SendObject acSendNoObject, , , _
sToName, sCcName, , sSubject, sMessageBody, False, False
End If
.MoveNext
Loop
End With

Set MyDb = Nothing
Set rsEmail = Nothing
Else
MsgBox ("You're not authorized")
End If
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
So what do you want to have happen when this condition exists? If it is to email anyway, then I suggest wrapping an IF block around each recipient field:
Code:
If IsNull(rs.fields(1)) or rs.fields(1)="" then
   sToName = rs.Fields(0) 
Else
   sToName = rs.Fields(0) & ";" & rs.Fields(1)
End If

If IsNull(rs.Fields(3)) or rs.Fields(3)="" then
  sCcName = rs.Fields(2)
Else
  sCcName = .Fields(2) & ";" & .Fields(3)
End if

Your issue might be the trailing semicolon you'd get if there are no second names. If not, you should always state on what line the error occurs and what the message number and text is. Also, you should use code tags around your code. It enables you to add leading spaces as you probably would in your code module and makes it easier for us to read.

I don't actually test for null and empty string this way anymore. I have a UDF that returns true if either condition exists and I test for it by passing the value I want to check:

If noData(rs.fields(1)) Then...
 
Last edited:
Upvote 0
Thank you. Still I am getting error Run-time error '3021':
No current record.

Error pointing to .MoveFirst


With rsEmail
.MoveFirst
Do Until rsEmail.EOF
If IsNull(rs.Fields(1)) Or rs.Field(1) = "" Then
sToName = rs.Fields(0)
Else
sToName = rs.Fields(0) & ";" & rs.Fields(1)
End If
If IsNull(rs.Fields(3)) Or rs.Fields(3) = "" Then
sCcName = rs.Fields(2)
Else
sCcName = .Fields(2) & ";" & .Fields(3)
End If

sSubject = "Reminder #: " & .Fields(4)
sMessageBody = "Reminder: Please check this item assigned to you showing as Overdue " & vbCrLf & _
"Received From: " & .Fields(5) & ", Due Date: " & .Fields(10) & ", " & .Fields(9) & vbCrLf & _
"Message: " & .Fields(7)

DoCmd.SendObject acSendNoObject, , , _
sToName, sCcName, , sSubject, sMessageBody, False, False
'End If
.MoveNext
Loop
End With
 
Upvote 0
Thank you. Now the error is Run-time error '3021': No current record.

It is pointing to . MoveFirst
 
Upvote 0
Whoops. Sorry, I neglected to reference the proper recordset variable name. I should have put rsEmail, not just rs. I'm wondering if you ever had this working, because I don't think the email body code (html) works with vbCrLf. I think you have to use the html versions (<P> for example).

Also, you are not reclaimng memory reserved for your object by destroying it at the end of your procedure:
Set rsEmail=Nothing
Set myDb = Nothing
Do you know about error handling routines, because I don't see one here.

>
 
Last edited:
Upvote 0
Thanks. It was working before if I have both email address available. Now throwing error even both email available . I changed to rsEmail. Set MyDb = Nothing and Set rsEmail = Nothing. Already there
 
Upvote 0
You are not specific enough for me to help much. "both email address"? What, both - 'To' names? Both CC names?
It is not enough to say you changed code but are still getting an error. What error - same one? Same place?
If same error, maybe the query you use is not returning any records. Put this before the email code:

rsEmail.MoveLast
msgbox rsEmail.RecordCount

If the message box retruns zero (0) then there are no records, thus you cannot move first. It's good practice to exit your code at the beginning if there are no records to work with, otherwise you will get such errors unless you handle them in an error handling routine.
 
Upvote 0
Sorry I did not mention both email I mean To: Two emails and CC: Two emails. My query returns correct. Now if I have both ( TO: test@kk.com;test2@kk.com) and (CC:test3@kk.com;test4@kk.com) it will work. but when I have email ( TO: test@kk.com) and (CC:test3@kk.com) does not work. Thank you.
 
Upvote 0
does not work
Again, this means nothing to me - you have to assume I know nothing about what you see, because I don't. Does this mean there is still a 3021 error or what? If so, you probably need to ignore the rest, and I am just wasting time.

If not and you are saying it is sending email if you have TO:test@kk.com;test2@kk.com AND you have CC:test3@kk.com;test4@kk.com, what about
TO:test@kk.com;test2@kk.com but NO CC addresses? If it works in this situation, I'm wondering if you have extra spaces or characters at the end when you have one address. If you use Outlook directly, extra spaces after the last TO recipient are not a problem. Perhaps it is when doing it via code. To see what you have after only one recipient, make sure your query returns records with only one recipient and try this:

After this line "Message: " & .Fields(7) put debug.print "TO part:" & sToName & ": CC part: & sCcName & ":"
Then put a break on this line DoCmd.SendObject and make sure you have the immediate window visible in your VB editor. Then run your sub and look in the immediate window.
See if there are any spaces, semicolons or any other characters between the colons and the recipient addresses
(e.g. looking for TO part:test2@kk.com: not TO part:test2@kk.com : .

Remember to remove or comment out the debug line when it's not needed anymore.
 
Last edited:
Upvote 0
Thank you for your help. It worked out. It was my mistake. I commented out the "End if" below before.
With rsEmail
.MoveFirst
Do Until rsEmail.EOF
If IsNull(rs.Fields(1)) Or rs.Field(1) = "" Then
sToName = rs.Fields(0)
Else
sToName = rs.Fields(0) & ";" & rs.Fields(1)
End If
If IsNull(rs.Fields(3)) Or rs.Fields(3) = "" Then
sCcName = rs.Fields(2)
Else
sCcName = .Fields(2) & ";" & .Fields(3)
End If

sSubject = "Reminder #: " & .Fields(4)
sMessageBody = "Reminder: Please check this item assigned to you showing as Overdue " & vbCrLf & _
"Received From: " & .Fields(5) & ", Due Date: " & .Fields(10) & ", " & .Fields(9) & vbCrLf & _
"Message: " & .Fields(7)

DoCmd.SendObject acSendNoObject, , , _
sToName, sCcName, , sSubject, sMessageBody, False, False
'End If
.MoveNext
Loop
End With
 
Upvote 0

Forum statistics

Threads
1,221,840
Messages
6,162,311
Members
451,759
Latest member
damav78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top