Results 1 to 20 of 22

Thread: Generating an email from VBA which pulls results from a query

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,297
    Location
    OK, I am going to try and go with a modified version of your code as you have doen a lot of work on the formatting etc

    Dim rec As Object, ahead(1 To 4) As String, aRow(1 To 4) As String, aBody() As String, lCnt As Long
    On errer goto errorcatch
    strQry = "PARAMETERS cboParam TEXT(255);" & " SELECT [LoanID], [Prior LoanID], [SRP Rate], [SRP Amount] " _
    & " FROM emailtable " & " WHERE [Seller Name:Refer to As] ='" & [cboParam] & "'"
    ' BIND PARAMETER
    ' qdef!cboParam = Me.Combo296
    ' OPEN RECORDSET
    Set rec = CurrentDb.OpenRecordset(SQL)
    If rec.recordcount<>0 then
        msgbox rec.[Seller Name:Refer to As]
    End if
    rec.close
    Exit sub
    ' Create the header row
    ahead(1) = "Loan ID"
    ahead(2) = "Prior Loan ID"
    ahead(3) = "SRP Rate"
    ahead(4) = "SRP Amount"
    lCnt = 1
    ReDim aBody(1 To lCnt)
    aBody(lCnt)  = "<HTML><body><table  border='2'><tr><th>" & Join(ahead,  "</th><th>") & "</th></tr>"
    If Not (rec.BOF And rec.EOF) Then
        Do While Not rec.EOF
            lCnt = lCnt + 1
            ReDim Preserve aBody(1 To lCnt)
            aRow(1) = rec("[LoanID]")
            aRow(2) = rec("[Prior LoanID]")
            aRow(3) = rec("[SRP Rate]")
            aRow(4) = rec("[SRP Amount]")
            aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>"
            rec.MoveNext
        Loop
    End If
    aBody(lCnt) = aBody(lCnt) & "</table></body></html>"
    Set objOutlook = CreateObject("Outlook.Application.16")
    Set objMail = objOutlook.CreateItem(0)
    With objMail
        .Display 'To display message
        .to = Me.Combo88
        .cc = Me.Combo282
        .Subject = "*SECURE* " & Me.Combo296 & " EPO Refund Request (" & Me.Combo212 & " " & Me.Combo284 & ")"
        .HTMLBody = "<p><font face=""calibri"" font-size:11pt;"">Greetings,</p>" _
        &  "<p>We recently acquired loans from " & Me.Combo296 & ",  some of which have paid in full and meet the criteria for early  prepayment _
        defined in the governing documents. We are requesting a  refund of the SRP amount detailed on the attached list.</p>" _
        & "<p>Please wire funds to the following instructions:</p>" _
        & "<ul>Bank Name: My Bank</ul>" _
        & "<ul>ABA: 123456</ul>" _
        & "<ul>Credit To: My Mortgage</ul>" _
        & "<ul>Acct: 54321</ul>" _
        & "<ul>Description: " & Combo296 & " EPO SRP Refund</ul>" _
        &  "<p>Thank you for the opportunity to service loans from " &  Me.Combo296 & "! We appreciate your partnership.</p>" _
        &  "<p>If you have any questions, please contact your Relationship  Manager, " & Me.Combo336 & " (Cc'd).</p>" _
        & "<p><br>Sincerely,</br>" _
        & "<br>Acquisitions</br>" _
        & "<br>email@me.com</br></p>"
    End With
    rec.Close
    Set rec = Nothing: Set qdef = Nothing: Set db = Nothing
    Exit Sub
    errorcatch:
    MsgBox Err.Description

    OK, try the code above, if it crashes let me know what the error description is.
    If it works it should just give you a message box with the name that you chose in the Combo.
    Last edited by Aussiebear; 05-02-2025 at 01:53 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •