Question : Help with recordset source...

I am writing code that puts the the contents of a form into an email. I can get the data from my parent form into the email no problem, I am having trouble getting the right data from my child form into my email.

The attached code snippet works okay except that ALL of the records in my tblQuoteLine are included in the email. I only want records from tblQuoteLine that relate to the parent data in tblQuote.

Parent form = frmQuote (underlying data tblQuote)
Child Form = frmQuoteLine (underlying data tblQuoteLine)

I have never used a recordset before but I'm sure my problem is with the source element of the recordset I am using.

Can anyone help me with the right source...?

My tables are linked with quoteid.



Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
Private Sub btnEmailQuote_Click()
 
' Set Up Carriage Returns
    Const cR = vbCrLf
    Const cRR = vbCrLf & vbCrLf
    
' Set Up Quote Subject
    Dim strQuoteSubject As String
    strQuoteSubject = "Quote # " & Me.quoteid & ": " & Me.quotedescription
    
' Set Up Quote Introduction
    Dim strQuoteIntro As String
    strQuoteIntro = "Thank you for your recent enquiry relating to the purchase of " & Me.quotedescription & ", I hope the following is of interest:" & cRR
        
' Set Up Quote Body
    
    Dim strQuoteBody As String
    Dim rstQuote As ADODB.Recordset
    'Dim strQuoteBody As String
    
    Set rstQuote = New ADODB.Recordset
    rstQuote.Open Me.frmQuoteLine.Form.RecordSource, CurrentProject.Connection
    
If Not (rstQuote.EOF And rstQuote.BOF) Then
  Do Until rstQuote.EOF
    strQuoteBody = rstQuote("quotelinedescription") & ": " & rstQuote("quotelineqty") & vbCrLf & strQuoteBody
    rstQuote.MoveNext
  Loop
End If
 
' Dim strIn As String
 
' Set Up Quote Footer
    Dim strQuoteFooter As String
    strQuoteFooter = "Price Ex VAT: " & Format(Me.txtItemTotalExVAT, "Currency") & cRR & "Delivery Ex VAT: " & Format(Me.quotedelivery, "Currency") & cRR & "Total Ex VAT: " & Format(Me.txtTotalExVAT, "Currency") & cRR & "VAT Amount: " & Format(Me.txtVAT, "Currency") & cRR & "Grand Total: " & Format(Me.txtTotal, "Currency") & cRR
    
' Set Up Quote Terms 1
    Dim strQuoteTerms1 As String
    strQuoteTerms1 = "Quote Notes:" & cR & "===========" & cR & Me.quotenotes & cRR
 
' Set Up Quote Terms 2
    Dim strQuoteTerms2 As String
    strQuoteTerms2 = "Quote Terms:" & cR & "===========" & cR & Me.quoteterms & cRR
 
' Combine The Quote Elements
    Dim strWholeQuote As String
    strWholeQuote = strQuoteIntro & strQuoteBody & strQuoteFooter & strQuoteTerms1 & strQuoteTerms2
 
' Send The Quote By Email
    DoCmd.SendObject acSendNoObject, subject:=strQuoteSubject, cc:="[email protected]", messagetext:=strWholeQuote, EditMessage:=True
    
'SET UP [email protected] in EXCHANGE
 
End Sub
Open in New Window Select All

Answer : Help with recordset source...

In lines 24 to 29 your code moves through the entire recordset. What you need is to print only the related records. To avoid changing your code too much you could check for the UNIQUE ID of the record on the master form as you move through the recordset. I've included a code sample where you should replace quoteID with the name of the fileds that link your master and child forms (Link Child Fields and Link Master Fields from the properties of your subform object).
1:
2:
3:
4:
5:
6:
7:
8:
If Not (rstQuote.EOF And rstQuote.BOF) Then
  Do Until rstQuote.EOF
    If rstQuote("quoteID") = Me.quoteID Then
        strQuoteBody = rstQuote("quotelinedescription") & ": " & rstQuote("quotelineqty") & vbCrLf & strQuoteBody
    End If
    rstQuote.MoveNext
  Loop
End If
Open in New Window Select All
Random Solutions  
 
programming4us programming4us