Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273

    E-Mail query results

    I hope this question has an easy answer, though I haven't had many lately that do.
    Using VBA, how do I add the results of a query to the body of my e-mail. I already have a sentence as part of the body in the ".HTMLBody = " section of the code, but also want to add the list of names from the query below this sentence. The query has 5 fields, but I only want 3 of them to show up on the E-Mail if possible.

    Thanks for the assistance.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I would build the query you want to attach to the email and not rely on filtering a query before you send it.

    here's a link to how to do it.

    http://www.fmsinc.com/microsoftacces...endobject.html

  3. #3
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273
    Sorry, I'm still lost. I'm using the following code;

    Dim mess_body As String
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Dim ExpDt As Date

    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    ExpDt = Now()
    ExpDt = Format(ExpDt, "mmmm yyyy")
    Expire = ExpDt

    With MailOutLook
    .BodyFormat = olFormatRichText
    .To = ""
    .Subject = "Permit Expiration Notice"
    .HTMLBody = " The following employee's driving permits have expired, or will expire on the first of " & Expire & ". Please contact Operations to schedule a renewal date." 'qryExpire
    .Display
    End With

    Exit Sub
    email_error:
    MsgBox "An error has occurred. " & vbCrLf & "The error is: " & Err.Description
    Resume Error_out
    Error_out:

    End Sub

    The query (qryExpire) already comes up with the list of names I want in the body of the e-mail, I'm just not sure how to add it after the sentences that are already going into the body.
    One other thing I haven't figured out yet... the date format shows the current month & year, but I need it to show the following month. I've tried adding "+1" in a few different ways, but it isn't working.

    Thanks for your help.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Ok, let's say your query has two fields PersonFN and PersonLN (first land last name)

    Code:
    .Subject = "Permit Expiration Notice"
    .HTMLBody = " The following employee's driving permits have expired, or will expire on the first of " & Expire & ". Please contact Operations to schedule a renewal date." 'qryExpire
    .Display
    This is your existing information

    I would add a variable to your procedure

    Dim sBody as string
    Dim db as Database
    Dim rst as recordset

    then when you get to this section of code:

    Code:
    .Subject = "Permit Expiration Notice"
    set db = currentdb
    set rst = db.openrecordset("qryExpire")
    rst.movefirst
    do while rst.eof <> true
          sbody = sbody & rst.fields("PersonLN") & ", " & vbcrlf rst.fields("PersonFN")
          rst.movenext
    loop
    rst.close
    set db= nothing
    .HTMLBody = " The following employee's driving permits have expired, or will expire on the first of " & Expire & ". Please contact Operations to schedule a renewal date." & vbcrlf & sbody
    .Display

  5. #5
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273
    Thanks for the reply.
    I entered the code that you suggested, and I'm getting a syntax error on the looping line that starts with sBody = sBody & rst.fields... Here's what I entered...

    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Dim sBody As String
    Dim db As Database
    Dim rst As Recordset

    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)

    ExpirationDate = Date
    ExpirationDate = ExpirationDate + 29
    Expire = Format(ExpirationDate, "mmmm yyyy")

    With MailOutLook
    .BodyFormat = olFormatRichText
    .To = ""
    .Subject = "Permit Expiration Notice"
    Set db = CurrentDb
    Set rst = db.OpenRecordset("qryExpire")
    rst.MoveFirst
    Do While rst.EOF <> True
    sBody = sBody & rst.Fields("LName") & ", " & vbcrlf rst.Fields ("FName")
    rst.MoveNext
    Loop
    rst.Close
    Set db = Nothing

    .HTMLBody = " The following employee's driving permits have expired, or will expire on the first of " & Expire & ". Please contact Operations to schedule a renewal date." & vbCrLf & sBody
    .Display
    End With

    Exit Sub
    email_error:
    MsgBox "An error has occurred. " & vbCrLf & "The error is: " & Err.Description
    Resume Error_out
    Error_out:

    End Sub

    Also, if possible, I would like to add a third field to that line (Cmpny).
    Lastly, I think I have the date part working now. I know it looks funny, but this message won't be e-mailed out until a week or so into the month, so the following month should always show up correctly.

    Thanks again for your help.

  6. #6
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273
    OK, I got part of this figured out. I changed that line of code to look like this...

    sBody = sBody & rst.Fields(1) & ", " & rst.Fields(2) & " " & rst.Fields(3) & vbCrLf
    Also changed the body line to this...

    .HTMLBody = " The following employee's driving permits have expired, or will expire on the first of " & Expire & ". Please contact Operations to schedule a renewal date." & vbCrLf & sBody


    Now the whole body looks like one big sentence. Isn't the vbCrLf supposed to force a new line? I'll keep playing with it, but any guidance is appreciated.

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    try chr(13)chr(10) instead of vbcrlf

  8. #8
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273
    That line of code now looks like this...

    sBody = sBody & rst.Fields(1) & ", " & rst.Fields(2) & " " & rst.Fields(3) & chr(13)chr(10)

    Still getting a syntax error on that line. Did I do this wrong?

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    sBody = sBody & rst.Fields(1) & ", " & rst.Fields(2) & " " & rst.Fields(3) & chr(13) & chr(10)

  10. #10
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273
    Not getting any errors this time, but the e-mail is still all clumped together.

  11. #11
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Can you post a sample database, with some fake data in it. All my tests correctly generate the sbody.

  12. #12
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273
    Attachment 4352

    I hope this worked. I checked it again after deleting & changing alot of the data, & it's still doing the same thing. I hope you have better luck with it than I have.

    Thanks again!

  13. #13
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I missed it on your original post but you're using an HTML format so instead of chr(10)chr(13) or vbcrlf just add the text

    & "<BR>" wherever you want a new line to start

  14. #14
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273
    That did the trick, thanks so much. Just too much code out there that I don't understand. I hate to keep bothering you guys with my problems, but you really are the best source of information I have found. Thank you for the work you do.

    PS...
    I did find one more thing. I added a few spaces between the name & the company in the code, but only one space shows up in the e-mail. There must be another trick out there that baffles me.
    Thanks for the help!

  15. #15
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    HTML code for a forced non compression of a blank space is

    &nbsp;

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2012, 07:24 AM
  2. Replies: 11
    Last Post: 09-12-2011, 11:30 AM
  3. Replies: 3
    Last Post: 02-21-2011, 09:57 AM
  4. Using a pass-through query in mail merge
    By ss1289 in forum Access
    Replies: 0
    Last Post: 09-17-2010, 08:58 AM
  5. E-mail query
    By Ize in forum Queries
    Replies: 1
    Last Post: 12-09-2009, 09:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums