0

Here is my updated code per @Parfait suggestion. It still isn't working, getting the following error:

Run-time error '3421' Data type conversion error

On the following line: Set rec = qdef.OpenRecordset(strQry)


Option Compare Database

Private Sub Command0_Click()

    Dim db As DAO.Database
    Dim qdef As DAO.QueryDef
    Dim rec As DAO.Recordset
    Dim olApp As Object
    Dim olItem As Variant
    Dim strQry As String
    Dim aHead(1 To 4) As String
    Dim aRow(1 To 4) As String
    Dim aBody() As String
    Dim lCnt As Long
       
    'Prepared Statement No Data
    
    strQry = "PARAMETERS cboParam TEXT(255);" _
        & " SELECT [Loan ID], [Prior Loan ID], [SRP Rate], [SRP Amount] " _
        & " FROM emailtable " _
        & " WHERE [Seller Name:Refer to As] = [cboParam]"
        
    
    Set db = CurrentDb
    Set qdef = db.CreateQueryDef("", strQry)
    
    ' BIND PARAMETER
    qdef!cboParam = Me.Combo296

    ' OPEN RECORDSET
    Set rec = qdef.OpenRecordset(strQry)
    
    '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("[Loan ID]")
            aRow(2) = rec("[Prior Loan ID]")
            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")
       Set objMail = objOutlook.CreateItem(0)
    With objMail
       .Display   'To display message
       .To = Me.Combo88
       .cc = Me.Combo282
       .Subject = "*SECURE* " & Me.Combo296 & " Refund Request (" & Me.Combo212 & " " & Me.Combo284 & ")"
       .HTMLBody = "<p><font face=""calibri"" style=""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: 1111111</ul>" _
       & "<ul>Credit To: ABC Mortgage</ul>" _
       & "<ul>Acct: 11111111111</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 protected]</br></p>"
       
    End With
    
    rec.Close
    Set rec = Nothing: Set qdef = Nothing: Set db = Nothing

End Sub

Any help would be greatly appreciated.

4
  • Where do you ever use aBody(lCnt)? It is not placed in HTMLBody. Commented Jan 12, 2021 at 1:09
  • For second error, do not include brackets in string literal of name: aRow(1) = rec("Loan ID") or aRow(1) = rec![Loan ID]. Commented Jan 12, 2021 at 1:25
  • This is code I got from a post somewhere and tried to replicate. I very well don't need it but I am learning so am not sure. Commented Jan 12, 2021 at 14:54
  • Please see above comment: do not include brackets in string literal of name. Commented Jan 12, 2021 at 22:33

2 Answers 2

1

Avoid concatenating VBA data to SQL even HTML strings. Instead, consider the industry standard of SQL parameterization.

Dim db DAO.Database, qdef As DAO.QueryDef, rec AS DAO.Recordset

' PREPARED STATEMENT (NO DATA)
strQry = "PARAMETERS cboParam TEXT(255);" _
        & " SELECT [Loan ID], [Prior Loan ID], [SRP Rate], [SRP Amount] " _
        & " FROM emailtable " _
        & " WHERE [Seller Name:Refer to As] = [cboParam]" 

Set db = CurrentDb 
Set qdef = db.CreateQueryDef("", strQry)

' BIND PARAMETER
qdef!cboParam = Me.Combo296 

' OPEN RECORDSET
Set rec = qdef.OpenRecordset()

... ' REST OF CODE USING rec

rec.Close
Set rec = Nothing: Set qdef = Nothing: Set db = Nothing

Also, consider saving the email message HTML markup as a text in the table or as text box on form with placeholders to be replaced with combo box values:

.HTMLBody = Replace(Replace(Me.EmailMessage, "placeholder1", Me.Combo296), 
                    "placeholder2", Me.Combo336)
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, I will give this a try. This is my first time taking a deep dive into VBA. I found some code on the internet I am trying to replicate for my needs. I will definitely give the parameterization a try.
In the .HTMLBody suggestion, if the combo boxes are already listed in the statement, I don't understand the place holder purpose.
How would I include my original code that will generate the table code for the email based on what I had in my original code? I am assuming the code I need will come after opening the recordset and before rec.close?
I would like to post my revised code for you to look at. I am now getting a data type conversion error. I feel that my code may be out of place. How can I post my revised code for your review?
Ok. I am not sure if it is related or not. I tried inserting my table code between the code blocks like you suggested and I am getting the error. I just wanted to make sure I inserted it correctly. If so, then I can troubleshoot the conversion error separately. I will edit my original post with new code.
|
0

I'm guessing (from your photo) that the data type of your [Seller Name:Refer to as] column is supposed to be string? In which case, your query is missing quotes to denote the string value in your comparison:

'Create each body row
strQry = "Select * from emailtable where [Seller Name:Refer to As] = """ & Me.Combo296 & """"
Set db = CurrentDb
Set rec = CurrentDb.OpenRecordset(strQry)

1 Comment

Can potentially break if combo box value contained quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.