I am doing something very similar in an access database and thought
this post was very helpful, but I am having some trouble.
I created the Function as a public function and then tried to call the
fMsgBody as the text part of my email and I recieved a compile error:
Expected array message. I can get the email portion to work if I don't
call the fMsgBody() function... so obviously my error is in the
fMsgBody portion.
I have :
Public Function fMsgBody() As String
Dim rsDue As DAO.Recordset
Dim strList As String '---place to dump all the names in the
Query
Set rsDue = DBEngine(0)(0). OpenRecordset("[Query]![Mail IACUC
Registrations]")
Do Until rsDue.EOF
strList = rsDue.Fields("P rotocol Number") & vbCrLf
rsDue.MoveNext
Loop
fMsgBody = "You have indicated this person will work on the
following protocols:" & vbCrLf & strList
rsDue.Close
Set rsDue = Nothing
End Function
'and the mail portion
Private Sub IACUC_CHANGE_Cl ick()
Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Applica tion
Dim MyMail As Outlook.MailIte m
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObjec t
Dim MyBody As TextStream
Dim MyBodyText As String
Dim fMsgBody As String
Dim strList As String
Set fso = New FileSystemObjec t
' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Applica tion
' Set up the database and query connections
Set db = CurrentDb()
' This creates the e-mail
Set MyMail = MyOutlook.Creat eItem(olMailIte m)
' This addresses it
MyMail.To = [Forms]![Personnel Form2]![Department Contact
Email]
'This gives it a subject
MyMail.Subject = "Current IACUC Approval of Change Form"
'This gives it the body
MyMail.Body = fMsgBody()
'MyMail.Body = "Dear Dr. " & [Forms]![Personnel
Form2]![Department Name] & vbCrLf & vbCrLf & "Attached is the Request
for IACUC Approval of Change to an Animal Care and Use Protocol
(amendment) form. Please fill one out for each of the currently
approved IACUC protocol(s) on which you have indicated you wish to add
" & [Forms]![Personnel Form2]![First Name] & [Forms]![Personnel
Form2]![Last Name] & ". Please edit the amendment form(s), complete
Section A and be sure to list relevant training and experience
information. Print a hard copy and return the original signed and
dated form(s) (including training and experience information) to me via
interoffice mail or in person. Please note: new personnel may NOT
participate on any IACUC protocol until approval from the IACUC is
received." & vbCrLf & vbCrLf & "Thank you for your attention to this
regulatory matter." & vbCrLf & vbCrLf & "Marsha Arnall" & vbCrLf &
"Research Regulations Specialist" & fMsgBody
'send an attachment
MyMail.Attachme nts.Add "X:\IACUC Administration\ IACUC Forms
for Researchers\090 4 Updated Amendment - Request for Change to Protocol
form.doc", olByValue, 1, "My Displayname"
So... can I use the acess query in the openrecordset? Or do I need to
write the query in vba (it is a pretty complicate sql statement) How
do I incorporate the sql into the vba?
Thanks,
Jessica
pi********@hotm ail.com wrote:
Okay, how about the SQL for the query? Is this where the four fields
are coming from?
If so, your message body function is pretty simple... once you get this
working, you could make fMsgBody churn out a formatted table... but
that's later.
Function fMsgBody() As String
'"IMO Number", "SBMA Number", "Date of Issue", "Due Date",and "Vessel
Name"
dim rsDue as dao.recordset
dim strList as string '---place to dump all the names in the
query
set rsDue=DBEngine( 0)(0).OpenRecor dset("qryDueMai ls")
do until rsDue.EOF
strList=rsDue.F ields("IMO Number") & vbtab & _
rsDue.Fields("S BMA Number") & vbtab & _
rsDue.Fields("D ate of Issue") & vbtab & _
rsDue.Fields("D ue Date") & vbtab & _
rsDue.Fields("V essel Name") & vbcrlf
rsDue.MoveNext
loop
fMsgBody="The following accounts are due:" & vbcrlf & strList
rsDue.Close
set rsDue=Nothing
End Function
Then just use fMsgBody instead of your normal message text. It'll
build the message for you. Then you just assign the result of this
function to the .TEXT or .BODY property of the Message (in Lyle's
code).
Of course, if you don't understand some VB, this is very likely going
to blow right over your head.
I hope this helps
Pieter