Results 1 to 7 of 7
  1. #1
    tdoolittle is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2014
    Posts
    18

    Loop Through Query, Send Multiple Emails

    I've been looking everywhere for help on this, I think the issue is I've read so much that I've now confused myself and trapped myself from seeing the answer.



    What I'm dealing with right now is a Query [qry_TodaysTracking] and a Report [rpt_Fedex_Email]. At the end of the day, all of our shipments have been loaded into the database and I have my query [qry_TodaysTracking] show each tracking number that was generated today.

    From here I have a report laid out like a FedEx clearance instruction letter with a field that is populated by [qry_TodaysTracking]![TrackingNumber]. What the report is currently/obviously doing is giving 6 pages, each page with one of the unique tracking numbers for today. Currently I'm using the following to at least mail this report out to the required individuals through a button on a form I have:

    Code:
    Private Sub btn_FedExEmail_Click()
        DoCmd.SendObject _
        acSendReport, _
        "rpt_Fedex_Email", _
        acFormatPDF, _
        "test@fedex.com;test@fedex.com", _
        "test@fedex.com", _
        , _
        "Clearance Instructions - " & Date, "Thanks", _
        True _
    End Sub
    However, going forward I am no longer allowed to submit a multipage PDF to our FedEx reps and they are requiring an individual PDF/email for each tracking number we have. So what I need is code that will relay my first tracking number on the [qry_TodaysTracking], make a PDF, send it, then move the next tracking number and repeat. In my mind and using my limited knowledge this would require a loop. But researching is only confusing me. Could someone assist me in breaking this down?

    Thanks guys!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Open a recordset object and loop through the records. Within the loop, open report filtered to TrackingNumber from record, send report, close report, move to next record, repeat. Fairly common topic in forum. https://www.accessforums.net/access/...ect-37936.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    tdoolittle is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2014
    Posts
    18
    Well I tried using those resources. Seems to be the same as most research I've been doing, everyone posts tons of code up but I don't find anything exactly explaining what the code is doing. I'm at this point right now:

    Code:
    Private Sub Command33_Click()
    Dim rs As DAO.Recordset
    stCaption = "Clearance Instructions - " & Format(Now(), " mm-dd-yyyy")
    myPath = "X:\SHIPPING - FEDEX\FedEx Letters\"
    stTo = "doolittle@projectorsupercenter.com"
    stEmailMessage = "Please see the attached clearance instructions."
    stSubject = "Clearance Instructions - " & Date
    stReport = "rpt_FedEx_Email"
    Set rs = CurrentDb.OpenRecordset("SELECT TrackingNumber FROM qry_NonTRANS_Tracking_Today")
    DoCmd.OpenReport "rpt_FedEx_Email", acViewPreview
    While Not rs.EOF
    DoCmd.SendObject acSendReport, , acFormatPDF, stTo, , , stSubject, stEmailMessage, True, ""
    DoCmd.OutputTo acOutputReport, , acFormatPDF, myPath & stCaption & ".pdf", False, , , acExportQualityPrint
    rs.MoveNext
    Wend
    DoCmd.Close acReport, stReport, acSaveNo
    rs.Close
    End Sub
    I guess I'm getting somewhere as I have two tracking numbers currently coming up in the query I'm referencing and my on click event is prompting me to send two emails. However each email contains one large PDF, with two pages, each page referencing one of the tracking numbers. Now I just need to figure out how to break it apart so each email contains a PDF with one page referencing its own tracking number. I have a feeling I simply need to move something referencing the RecordSet around? I'm assuming "rs" stands for recordset.

    Getting somewhere which is promising though! I'll keep following the rabbit hole of links and see where it takes me

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Yes, need to open and close the report within the loop and open it filtered to TrackingNumber of the current recordset record.

    Is TrackingNumber a text data type? Try:

    Private Sub Command33_Click()
    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT TrackingNumber FROM qry_NonTRANS_Tracking_Today")
    stCaption = "Clearance Instructions - " & Format(Now(), " mm-dd-yyyy")
    myPath = "X:\SHIPPING - FEDEX\FedEx Letters\"
    stTo = "doolittle@projectorsupercenter.com"
    stEmailMessage = "Please see the attached clearance instructions."
    stSubject = "Clearance Instructions - " & Date
    stReport = "rpt_FedEx_Email"
    While Not rs.EOF
    DoCmd.OpenReport "rpt_FedEx_Email", acViewPreview, , "TrackingNumber='" & rs!TrackingNumber & "'"
    DoCmd.SendObject acSendReport, , acFormatPDF, stTo, , , stSubject, stEmailMessage, True, ""
    DoCmd.OutputTo acOutputReport, , acFormatPDF, myPath & stCaption & ".pdf", False, , , acExportQualityPrint
    DoCmd.Close acReport, stReport, acSaveNo
    rs.MoveNext
    Wend
    rs.Close
    End Sub
    Last edited by June7; 05-12-2014 at 08:50 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    tdoolittle is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2014
    Posts
    18
    Thanks for coming back, very much appreciated! I have been playing around with this for a while and tried what you have offered but I always get a PDF report showing a two-paged report each page with its own tracking number but get two separate emails, just both trying to send the same two-paged report. For the life of me I can not get it to send two separate one page reports each with their own tracking number.

    Right now the command button is located on a form which contains a subform showing the query which supplies the two tracking numbers. The button click as you know sends the report which is bound to a query. The report has two fields, one which shows today's date and one which pulls directly from the bound query the [TrackingNumber], which correct, is a text data type. The query itself is pulling three columns of data. One is the [TrackingNumber] (text data) one is a [TRANS] (yes/no data) and one is [Date] (date). The [TRANS] has a criteria of "False" set, as I want to only see tracking which is not checked as TRANS and the [Date], for testing reasons as I have no new data in the database yet, has its criteria set to =Date()-60, therefore pulling the two tracking numbers that were loaded within the last 60 days. (Have yet to add all my new data until I get everything working)

    I've been playing around with the binding of the report and where the report is grabbing data from, I don't know if this would make a difference? I've tried the way above, I've also tried having the report still bound to the query and having the tracking number field reference the direct subform of where the button resides ([Forms]![frm_Overview]![frm_Overview_NonTRANS_Tracking_Today].Form![TrackingNumber]), this results in a two paged report, each page only referencing the first tracking number so I got myself further away from the goal

    Sorry if this is a lot of information, it's mostly me thinking out loud trying to walk myself through this!

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Maybe my code will work now. I did a minor edit of previous post. Was missing a comma for one of the unused arguments of the OpenReport method.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    tdoolittle is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2014
    Posts
    18
    Oh man, it worked! Ahhhhhhhhhhhhhhhhhhhh joyous days! Thank you very much for your assistance, incredibly appreciated! I'm amazed at what this forum has helped me accomplish after knowing literally nothing about Access or VBA. Thanks so much!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 12-18-2013, 02:49 PM
  2. Programming Access to Send Emails?
    By BMW150 in forum Access
    Replies: 8
    Last Post: 09-17-2013, 06:14 PM
  3. Send Emails Automatically
    By cbrsix in forum Programming
    Replies: 10
    Last Post: 10-19-2012, 10:52 AM
  4. Replies: 1
    Last Post: 06-26-2012, 09:37 AM
  5. Replies: 2
    Last Post: 04-27-2012, 10:48 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