Results 1 to 4 of 4
  1. #1
    doopml is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    2

    Emailing reports from access

    Hello All, I am trying to write code that will allow me to send emails in which the recipients are autopopulated based on the responses in combo boxes on a form. I have tried using the dLookup function with the Send Object function. Below is my code:

    Private Sub Command14_Click()
    On Error GoTo Err_Command14_Click

    Dim stDocName As String

    stDocName = "Open_Queries_Study_Site_Macro"
    DoCmd.RunMacro stDocName

    Exit_Command14_Click:
    Exit Sub

    Err_Command14_Click:
    MsgBox Err.Description
    Resume Exit_Command14_Click


    End Sub

    Private Sub Command14_Exit(Cancel As Integer)

    Dim stDocName As String
    Dim stToName As String
    Dim stCCName As String
    Dim stBCCName As String
    Dim stSubject As String
    Dim stMessage As String

    stDocName = "Open_Queries_by_Study_And_Site"

    stToName = DLookup("[TO]", "tblContacts", "[[Forms]![Select_Study_Site]![Protocol_Number]=&Me.Combo4&]" And "[[Forms]![Select_Study_Site]![Site_Number]=&Me.Combo6&]")
    stCCName = DLookup("[CC]", "tblContacts", "[[Forms]![Select_Study_Site]![Protocol_Number]=&Me.Combo4&]" And "[[Forms]![Select_Study_Site]![Site_Number]=&Me.Combo6&]")


    stBCCName = DLookup("[BCC]", "tblContacts", "[[Forms]![Select_Study_Site]![Protocol_Number]=&Me.Combo4&]" And "[[Forms]![Select_Study_Site]![Site_Number]=&Me.Combo6&]")
    stSubject = "Quotation Test"
    stMessage = "Attached is a report. This is a test."
    DoCmd.SendObject acSendReport, stDocName, acFormatPDF, stToName, stCCName, stBCCName, stSubject, stMessage

    End Sub


    So the first part is an event procedure where the user selects a protocol number and siite from the combo boxes on the form and when the command button is clicked a report is generated. The second part of the code is where I am struggling. I want the next procedure to run automatically and select the recipients of the email based on a table in the database (tblContacts) in which the email recipients correspond the what is selected on the form.

    when I try to run the code, i get a runtime error -13: mismatch type.

    The control source for the combo boxes come from a different table and query respectively (e.g. the first combo box is from the field "Protocol Number" from the table "Patients" and the second combo box is from a query that selects all sites that are conducting that study...same table though).

    But tblContacts isn't "related" to the form....but it also has fields for "Protocol Number" and "Site"

    So for example, when combo box 4 says ABC123, i would like the code to looko up in tblContacts the email addresses that are associated with ABC123, then when combo box 6 says 1234, I want the code to go through tblContacts and for all the email addresses associated with ABC123, find the email addresses associated with site 1234.


    Please help! Thanks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well, a recordset would be more efficient since you're getting multiple values, but see if this helps:

    DLookup Usage Samples
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    doopml is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    2
    I have no idea how to use record sets. How would I go about using that?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I happen to have this db open, so here's an example:

    Code:
    Public Function GetLastZone(lngCarNum As Long) As String
      Dim strSQL        As String
      Dim db            As DAO.Database
      Dim rs            As DAO.Recordset
    
      On Error GoTo ErrorHandler
    
      Set db = CurrentDb()
    
      strSQL = "SELECT Zone FROM tblLabels WHERE CarNum = " & lngCarNum
      Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
    
      If Not rs.EOF Then
        GetLastZone = rs!Zone
      Else
        GetLastZone = "U"
      End If
    
    ExitHandler:
      Set rs = Nothing
      Set db = Nothing
      Exit Function
    
    ErrorHandler:
      Select Case Err
        Case Else
          MsgBox Err.Description & " in GetLastZone"
          DoCmd.Hourglass False
          Resume ExitHandler
      End Select
    End Function
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. emailing in access 2010
    By griffin.92 in forum Forms
    Replies: 3
    Last Post: 06-04-2011, 11:30 AM
  2. Replies: 2
    Last Post: 05-20-2011, 10:18 AM
  3. Pop-up when emailing from Access
    By tgavin in forum Access
    Replies: 5
    Last Post: 05-02-2011, 10:56 AM
  4. EMailing Through Access
    By mamta in forum Programming
    Replies: 5
    Last Post: 02-15-2010, 01:07 PM
  5. Emailing forms or reports
    By dcecil in forum Reports
    Replies: 6
    Last Post: 12-16-2009, 07:57 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