Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 7th, 2005, 12:09 AM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
Unhappy Email Body - Need help please

Using the code below, how would I include a report in the body of the email rather than as an attachment? I also need to know how to alter it to send one email form to only 70 address and then open up a new form with 70 more, until all email addresses are used.

Private Sub cmdEmail_Click()
On Error GoTo Err_Handler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTo As String, strCC As String, strBCC As String
Dim strSubject As String, strMessage As String
strTo = "recipient@someaddress.com" 'Address to send to (required)
strCC = "recipient@someaddress.com" 'Address to copy to (optional)
strSubject = "This is your Subject Line"
strMessage = "This is your Message Text"
Set db = CurrentDb
Set rs = db.OpenRecordset("qryName", dbOpenSnapshot)
Do Until rs.EOF
With rs
If Not IsNull(rs!Email) Then 'Make sure that an email address exists
strBCC = strBCC & rs!Email & "; "
End If
.MoveNext
End With
Loop
strBCC = Left(strBCC, Len(strBCC) - 2) 'Addresses to Blind copy to
Set db = Nothing
Set rs = Nothing
DoCmd.SendObject , , , strTo, strCC, strBCC, strSubject, strMessage
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number = 2501 Then 'In case the email was canceled
Resume Exit_Handler
Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_Handler
End If
End Sub

Reply With Quote
  #2  
Old September 7th, 2005, 08:44 AM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Hi Krisw538

Use the following function and code to get your email addresses limited to 70. I will look into making the body of the email your report when I have time later.

Function GetEmailAddress(rs As Recordset, _
intNumber As Integer, varAddress As Variant) As Boolean
varAddress = rs.GetRows(intNumber)
If intNumber > UBound(varAddress, 2) + 1 And Not rs.EOF Then
GetEmailAddress = False
Else
GetEmailAddress = True
End If
End Function

Code behind your email command button:

Private Sub cmdEmail_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTo As String, strCC As String, strBCC As String
Dim strSubject As String, strMessage As String
Dim intNumber As Integer, intRecord As Integer
Dim strSQL As String
strSQL = "SELECT Email FROM TableName " & _
"WHERE Email Is Not Null;"
strTo = "recipient@someaddress.com" 'Address to send to (required)
strCC = "recipient@someaddress.com" 'Address to copy to (optional)
strSubject = "This is your Subject Line"
strMessage = "This is your Message Text"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
rs.MoveLast
If rs.RecordCount > 70 Then
intNumber = 70
Else
intNumber = rs.RecordCount
End If
With rs
.MoveFirst
Do Until rs.EOF
If GetEmailAddress(rs, intNumber, varAddress) Then
For intRecord = 0 To UBound(varAddress, 2)
strBCC = strBCC & varAddress(0, intRecord) & "; "
Next intRecord
strBCC = Left(strBCC, Len(strBCC) - 2) 'Addresses to Blind copy to
On Error Resume Next 'Ignore error if email is cancelled
DoCmd.SendObject , , , strTo, strCC, strBCC, strSubject, strMessage
strBCC = " "
End If
Loop
End With
Set db = Nothing
Set rs = Nothing
End Sub

Note: The sql is using a field name Email, so use the field name and table name accordingly in your database

lwells

Reply With Quote
  #3  
Old September 7th, 2005, 10:57 AM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
Thank you. I'll look forward to the report in the body when you have a chance to think about it.

I put the code and function in and I get an error. While I understand the error, I don't know how to fix it. Is it because I put the source as qry_EmailPetwatchNotices instead of a table? That's the only thing I did differently.
Attached Images
File Type: jpg ErrorCode.JPG (83.9 KB, 147 views)

Reply With Quote
  #4  
Old September 7th, 2005, 12:37 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Setting the source to your query shouldn't have been a problem. I suspect it is in the Function call....where did you place the function?...in the forms Module or global module?

To send your report as the body of your email...save your report as a text file and then replace this part of your code:

strMessage = "This is your Message Text"

With this

Open "C:\Report.txt" For Input As #1
While Not EOF(1)
Line Input #1, TextLine
strMessage = strMessage & TextLine & vbCrLf
Wend
Close #1

Be sure to use the correct path and file name in the above

Let me know about the function call.

lwells

Reply With Quote
  #5  
Old September 7th, 2005, 05:09 PM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by lwells
I suspect it is in the Function call....where did you place the function?...in the forms Module or global module?

lwells


I put it in the forms module. I'm not sure where else to put it. It sounds stupid of me to say this but I'm not sure which module is the global module. I'm having one of those days where everything I do on this database today isn't working.

Reply With Quote
  #6  
Old September 7th, 2005, 05:27 PM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
What does "Textline" represent? It's giving me problems and I'm trying to work with it but am having trouble. I can't believe I'm asking such stupid questions.

Quote:
Originally Posted by lwells
Setting the source to your query shouldn't have been a problem. I suspect it is in the Function call....where did you place the function?...in the forms Module or global module?

To send your report as the body of your email...save your report as a text file and then replace this part of your code:

strMessage = "This is your Message Text"

With this

Open "C:\Report.txt" For Input As #1
While Not EOF(1)
Line Input #1, TextLine
strMessage = strMessage & TextLine & vbCrLf
Wend
Close #1

Be sure to use the correct path and file name in the above

Let me know about the function call.

lwells

Reply With Quote
  #7  
Old September 7th, 2005, 05:37 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Hmmm....with it working on my db it's a little puzzling...try this

Change this part:

If GetEmailAddress(rs, intNumber, varAddress) Then

to this

If GetEmailAddress(rs, intNumber, varAddress) = True Then

and add this snippet to the code after this

strBCC = " "

Else
Msgbox "GetEmailAddress is False"
Exit Do

End If

See if a message box pops up when you run the code.

Also did you compile the database?

lwells

Reply With Quote
  #8  
Old September 7th, 2005, 05:41 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
OOps....Textline is a variable to hold the first read line of the txt document

Add
Dim Textline As String

Also what version of Access are you using?

Reply With Quote
  #9  
Old September 7th, 2005, 07:37 PM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by lwells
OOps....Textline is a variable to hold the first read line of the txt document

Add
Dim Textline As String

Also what version of Access are you using?


I'm using 2000

I added the Dim and it went through, only it still puts the text file as an attachment instead of the body. I'm sorry to keep causing you work but I really need and appreciate any help you can give me.

Reply With Quote
  #10  
Old September 7th, 2005, 08:09 PM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
I get the same error message box and debug goes to this line:

If GetEmailAddress(rs, intNumber, varAddress) = True Then

This is what I have for the code part:

Private Sub Command7_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTo As String, strCC As String, strBCC As String
Dim strSubject As String, strMessage As String
Dim intNumber As Integer, intRecord As Integer
Dim strSQL As String
strSQL = "SELECT Email FROM qry_EmailPetwatchNotices " & _
"WHERE Email Is Not Null;"
strTo = "recipient@someaddress.com" 'Address to send to (required)
strCC = "recipient@someaddress.com" 'Address to copy to (optional)
strSubject = "Petwatch In Progress"
strMessage = ""
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
rs.MoveLast
If rs.RecordCount > 70 Then
intNumber = 70
Else
intNumber = rs.RecordCount
End If
With rs
.MoveFirst
Do Until rs.EOF
If GetEmailAddress(rs, intNumber, varAddress) = True Then
For intRecord = 0 To UBound(varAddress, 2)
strBCC = strBCC & varAddress(0, intRecord) & "; "
Next intRecord
strBCC = Left(strBCC, Len(strBCC) - 2) 'Addresses to Blind copy to
On Error Resume Next 'Ignore error if email is cancelled
DoCmd.SendObject , , , strTo, strCC, strBCC, strSubject, strMessage
strBCC = " "
Else
MsgBox "GetEmailAddress is False"
Exit Do
End If
Loop
End With
Set db = Nothing
Set rs = Nothing
End Sub


Oh, I compiled the modules and got the same result

Reply With Quote
  #11  
Old September 8th, 2005, 07:40 AM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
I was able to duplicate your error. Go to Tools/References and move the DAO object library as far up as it will go. You probably have the default Microsoft ActiveX Data Objects library above the DAO library object. Another work around would be to specify the DAO library here in this line of code:

Function GetEmailAddress(rs As DAO.Recordset, _
intNumber As Integer, varAddress As Variant) As Boolean

After you have this solved then we will work out the issue of your report in the email body instead of as an attachment.

lwells

Reply With Quote
  #12  
Old September 8th, 2005, 10:09 AM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
Got a different error this time.

I have the functions and codes on the module in the following order:

Function GetEmailAddress(rs As Recordset, _
intNumber As Integer, varAddress As Variant) As Boolean
varAddress = rs.GetRows(intNumber)
If intNumber > UBound(varAddress, 2) + 1 And Not rs.EOF Then
GetEmailAddress = False
Else
GetEmailAddress = True
End If
End Function

Function GetEmailAddress(rs As DAO.Recordset, _
intNumber As Integer, varAddress As Variant) As Boolean
End Function

Private Sub Command7_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTo As String, strCC As String, strBCC As String
Dim strSubject As String, strMessage As String
Dim intNumber As Integer, intRecord As Integer
Dim strSQL As String
strSQL = "SELECT Email FROM qry_EmailPetwatchNotices " & _
"WHERE Email Is Not Null;"
strTo = "recipient@someaddress.com" 'Address to send to (required)
strCC = "recipient@someaddress.com" 'Address to copy to (optional)
strSubject = "Petwatch In Progress"
strMessage = ""
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
rs.MoveLast
If rs.RecordCount > 70 Then
intNumber = 70
Else
intNumber = rs.RecordCount
End If
With rs
.MoveFirst
Do Until rs.EOF
If GetEmailAddress(rs, intNumber, varAddress) = True Then
For intRecord = 0 To UBound(varAddress, 2)
strBCC = strBCC & varAddress(0, intRecord) & "; "
Next intRecord
strBCC = Left(strBCC, Len(strBCC) - 2) 'Addresses to Blind copy to
On Error Resume Next 'Ignore error if email is cancelled
DoCmd.SendObject , , , strTo, strCC, strBCC, strSubject, strMessage
strBCC = " "
Else
MsgBox "GetEmailAddress is False"
Exit Do
End If
Loop
End With
Set db = Nothing
Set rs = Nothing
End Sub

I moved the DOA up as far as I could.

Here's the error
Attached Images
File Type: jpg untitled.JPG (23.6 KB, 229 views)

Reply With Quote
  #13  
Old September 8th, 2005, 10:27 AM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
YEAH!!! It works It works! I took the second function out after I figured out the error message. Your a genius!

Ok, I'm ready when you are to address the text in message body problem.

Thank you so much
Kris

Reply With Quote
  #14  
Old September 8th, 2005, 11:11 AM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Great, I will rewrite the code to include some error handling for you, but before I rewrite the code, lets work on the email body with your report.

Assuming you have already created your report with the information that you want to display as the body of the email, open your report in preview and using the tool bar select File/Export and then in the Save dialog box at the bottom for Save As Type select Text Files(*.txt;*.csv;*.tab;*.asc) from the drop down. Save the file to whatever directory you want and with whatever name you want the report name to be and then save.

Locate the full file path to this file and reply back with the full file path so I can code into the rewritten code for you. Example when saved to My Documents:

C:\Documents and Settings\user\My Documents\ReportName.txt

Besure to copy it as exactly how you have the file saved including the report name and extension.

lwells

Reply With Quote
  #15  
Old September 8th, 2005, 11:48 AM
krisw538 krisw538 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 24 krisw538 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 59 sec
Reputation Power: 0
Ok, here's what I have to date. I hate to keep throwing code out here but I tried to combine both issues and think I may have made a mess.


Function GetEmailAddress(rs As Recordset, _
intNumber As Integer, varAddress As Variant) As Boolean
varAddress = rs.GetRows(intNumber)
If intNumber > UBound(varAddress, 2) + 1 And Not rs.EOF Then
GetEmailAddress = False
Else
GetEmailAddress = True
End If
End Function


Private Sub EmailFlyer_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTo As String, strCC As String, strBCC As String
Dim strSubject As String, strMessage As String
Dim intNumber As Integer, intRecord As Integer
Dim strSQL As String
Dim Textline As String
strSQL = "SELECT Email FROM qry_EmailPetwatchNotices " & _
"WHERE Email Is Not Null;"
strTo = "" 'Address to send to (required)
strCC = "" 'Address to copy to (optional)
strSubject = "Animal Found"
Open "C:\Documents and Settings\Kris\My Documents\PETWATCH\Flyers\rpt_FlyerFound.txt" For Input As #1
While Not EOF(1)
Line Input #1, Textline
strMessage = strMessage & Textline & vbCrLf
Wend
Close #1
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
rs.MoveLast
If rs.RecordCount > 70 Then
intNumber = 70
Else
intNumber = rs.RecordCount
End If
With rs
.MoveFirst
Do Until rs.EOF
If GetEmailAddress(rs, intNumber, varAddress) = True Then
For intRecord = 0 To UBound(varAddress, 2)
strBCC = strBCC & varAddress(0, intRecord) & "; "
Next intRecord
strBCC = Left(strBCC, Len(strBCC) - 2) 'Addresses to Blind copy to
On Error Resume Next 'Ignore error if email is cancelled
DoCmd.SendObject , , , strTo, strCC, strBCC, strSubject, strMessage
strBCC = " "
Else
MsgBox "GetEmailAddress is False"
Exit Do
End If
Loop
End With
Set db = Nothing
Set rs = Nothing
Dim stDocName As String
stDocName = "rpt_FlyerFound"
DoCmd.SendObject acReport, stDocName, , strTo, strCC, strBCC, strSubject, strMessage
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number = 2501 Then 'In case the email was canceled
Resume Exit_Handler
Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_Handler
End If
End Sub

Reply With Quote
  #16  
Old September 8th, 2005, 12:07 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Hi Kris,

Okay replace the code behind the EmailFlyer_Click() with this code and you should be all set to go:

Private Sub EmailFlyer_Click()
On Error GoTo Err_Handler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTo As String, strCC As String, strBCC As String
Dim strSubject As String, strMessage As String
Dim intNumber As Integer, intRecord As Integer
Dim strSQL As String, TextLine As String

strSQL = "SELECT Email FROM qry_EmailPetwatchNotices " & _
"WHERE Email Is Not Null;"

strTo = "" 'Address to send to (required)
strCC = "" 'Address to copy to (optional)
strSubject = "Animal Found"