While Not rs.EOF
BodyStr = BodyStr & rs.Fields(0) & "| " & rs.Fields(1) & "| " & rs.Fields(2) & "| " & rs.Fields(3) & "| " & rs.Fields(4) & "| " & rs.Fields(5) & "| " & rs.Fields(6) & "| " & rs.Fields(7) & "|" & rs.Fields(8) & "|" & rs.Fields("MthFoodTtl") & vbCrLf ' this loads the data from the recordset to the emails iteratively 0 is items ID and 8 is tdate
rs.MoveNext
Wend
If BodyStr <> "" Then BodyStr = "Please find below your monthly statement" & vbCrLf & vbCrLf & "ID " & "" & "|" & "Breakfast" & "|" & "Lunch" & "|" & "Desert" & "|" & "Desert Wine" & "|" & "Table Wine" & "|" & "Bar " & "|" & "Cellar " & "|" & "Date" & " |" & "Total" & vbCrLf & BodyStr
emailbodfunc = BodyStr
Set rs = Nothing
Set db = Nothing
End Function
While Not rs.EOF
BodyStr = BodyStr & rs.Fields(0) & "| " & rs.Fields(1) & "| " & rs.Fields(2) & "| " & rs.Fields(3) & "| " & rs.Fields(4) & "| " & rs.Fields(5) & "| " & rs.Fields(6) & "| " & rs.Fields(7) & "|" & rs.Fields(8) & "|" & rs.Fields("MthFoodTtl") & vbCrLf
(CJ CODE)body2=body2 & "....
rs.MoveNext
Wend
(CJ CODE)bodystr=body & body2
If BodyStr <> "" Then BodyStr = "Please find below your monthly statement" & vbCrLf & vbCrLf & "ID " & "" & "|" & "Breakfast" & "|" & "Lunch" & "|" & "Desert" & "|" & "Desert Wine" & "|" & "Table Wine" & "|" & "Bar " & "|" & "Cellar " & "|" & "Date" & " |" & "Total" & vbCrLf & BodyStr
emailbodfunc = BodyStr
Set rs = Nothing
Set db = Nothing
End Function
is it possible to create another string like BodyStr (BodyStrII?) and have it's data (created by another loop through rs function
Option Explicit
Option Compare Database
Public Function emailbodfunc(MemID As Long) As String ' this function issues an sql query which loads its result into a recordset the loop below the query formats the results and loads them into bodystr
Dim rs As Recordset
Dim db As Database
Dim email As String
Dim BodyStr As String
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT ID,bfast,lunch,dwine,bar,desert,cellar,twine,tdate , (Select sum(bfast+lunch+desert) From Sales as tmp WHERE membersID=sales.membersID AND tdate Between #11/1/2012# And #11/30/2012#) as MthFoodTtl FROM sales WHERE membersID=" & MemID & " AND tdate Between #11/1/2012# And #11/30/2012#")
BodyStr = ""
While Not rs.EOF
BodyStr = BodyStr & rs.Fields(0) & "| " & rs.Fields(1) & "| " & rs.Fields(2) & "| " & rs.Fields(3) & "| " & rs.Fields(4) & "| " & rs.Fields(5) & "| " & rs.Fields(6) & "| " & rs.Fields(7) & "|" & rs.Fields(8) & "|" & rs.Fields("MthFoodTtl") & vbCrLf
rs.MoveNext
Wend
If BodyStr <> "" Then BodyStr = "Please find below your monthly statement" & vbCrLf & vbCrLf & "ID " & "" & "|" & "Breakfast" & "|" & "Lunch" & "|" & "Desert" & "|" & "Desert Wine" & "|" & "Table Wine" & "|" & "Bar " & "|" & "Cellar " & "|" & "Date" & " |" & "Total" & vbCrLf & BodyStr 'add a header line to the body
emailbodfunc = BodyStr
Set rs = Nothing
Set db = Nothing
End Function
Public Sub emailaddressfunc()
Dim rs As Recordset
Dim db As Database
Dim BodyStr As String
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT Firstname, Surname, emailaddress,ID FROM members;")
While Not rs.EOF
BodyStr = emailbodfunc(rs.Fields("ID"))
If BodyStr <> "" Then DoCmd.SendObject acSendNoObject, Null, Null, rs.Fields("emailaddress"), Null, Null, "Monthly Statement", BodyStr, 1
rs.MoveNext
Wend
Set rs = Nothing
Set db = Nothing
End Sub
(Select sum(bfast+lunch+desert) From Sales as tmp WHERE membersID=sales.membersID AND tdate Between #11/1/2012# And #11/30/2012#) as MthFoodTtl
Dim BodyStr As String
[COLOR=red]Dim Ttl as Double
[/COLOR]
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT ID,bfast,lunch,dwine,bar,desert,cellar,twine,tdate , (Select sum(bfast+lunch+desert) From Sales as tmp WHERE membersID=sales.membersID AND tdate Between #11/1/2012# And #11/30/2012#) as MthFoodTtl FROM sales WHERE membersID=" & MemID & " AND tdate Between #11/1/2012# And #11/30/2012#")
BodyStr = ""
While Not rs.EOF
[COLOR=green]'remove [B][I]& "|" & rs.Fields("MthFoodTtl")[/I][/B] from this line if not required[/COLOR]
BodyStr = BodyStr & rs.Fields(0) & "| " & rs.Fields(1) & "| " & rs.Fields(2) & "| " & rs.Fields(3) & "| " & rs.Fields(4) & "| " & rs.Fields(5) & "| " & rs.Fields(6) & "| [COLOR=black]" & rs.Fields(7) & "|" & rs.Fields(8) & vbCrLf
[/COLOR][COLOR=red]Ttl=Rs.Fields("MthFoodTtl")[/COLOR]
rs.MoveNext
Wend
[COLOR=green]'add additional bit to the end of this line (in red)
[/COLOR]If BodyStr <> "" Then BodyStr = "Please find below your monthly statement" & vbCrLf & vbCrLf & "ID " & "" & "|" & "Breakfast" & "|" & "Lunch" & "|" & "Desert" & "|" & "Desert Wine" & "|" & "Table Wine" & "|" & "Bar " & "|" & "Cellar " & "|" & "Date" & " |" & "Total" & vbCrLf [COLOR=red][COLOR=black]& BodyStr[/COLOR] & vbCrLf[COLOR=#000000] [/COLOR]& "Total Food " & Ttl & vbCrLf[COLOR=#000000] [/COLOR] [/COLOR]'add a header [COLOR=red]and total[/COLOR] line to the body
emailbodfunc = BodyStr
BodyStr & [COLOR=red]space(10-len(rs.Fields(0)) &[/COLOR] rs.fields(0) & "|" & [COLOR=red]space(10-len(rs.Fields(1)) &[/COLOR] rs.Fields(1) & "|" etc
then the final line above would become
& BodyStr[COLOR=#ff0000] & vbCrLf[/COLOR][COLOR=#000000] [/COLOR][COLOR=#ff0000]& "Total Food" & Space(60-len(Ttl)) & Ttl & vbCrLf[/COLOR][COLOR=#000000] [/COLOR][COLOR=#ff0000] [/COLOR]
Is the figure you want in the existing recordset?
i.e.
If so then modify your code as follows:
This is a slightly different solution to the one originally suggested but will do the job.Code:Dim BodyStr As String [COLOR=red]Dim Ttl as Double [/COLOR] Set db = CurrentDb Set rs = db.OpenRecordset("SELECT ID,bfast,lunch,dwine,bar,desert,cellar,twine,tdate , (Select sum(bfast+lunch+desert) From Sales as tmp WHERE membersID=sales.membersID AND tdate Between #11/1/2012# And #11/30/2012#) as MthFoodTtl FROM sales WHERE membersID=" & MemID & " AND tdate Between #11/1/2012# And #11/30/2012#") BodyStr = "" While Not rs.EOF [COLOR=green]'remove [B][I]& "|" & rs.Fields("MthFoodTtl")[/I][/B] from this line if not required[/COLOR] BodyStr = BodyStr & rs.Fields(0) & "| " & rs.Fields(1) & "| " & rs.Fields(2) & "| " & rs.Fields(3) & "| " & rs.Fields(4) & "| " & rs.Fields(5) & "| " & rs.Fields(6) & "| [COLOR=black]" & rs.Fields(7) & "|" & rs.Fields(8) & vbCrLf [/COLOR][COLOR=red]Ttl=Rs.Fields("MthFoodTtl")[/COLOR] rs.MoveNext Wend [COLOR=green]'add additional bit to the end of this line (in red) [/COLOR]If BodyStr <> "" Then BodyStr = "Please find below your monthly statement" & vbCrLf & vbCrLf & "ID " & "" & "|" & "Breakfast" & "|" & "Lunch" & "|" & "Desert" & "|" & "Desert Wine" & "|" & "Table Wine" & "|" & "Bar " & "|" & "Cellar " & "|" & "Date" & " |" & "Total" & vbCrLf [COLOR=red][COLOR=black]& BodyStr[/COLOR] & vbCrLf& "Total Food " & Ttl & vbCrLf [/COLOR]'add a header [COLOR=red]and total[/COLOR] line to the body emailbodfunc = BodyStr
To make your formatting a bit better I would suggest you look at using the Space function in the following way which will effectively right justify the values. Change the 10 Value to whatever is required for each column
Code:BodyStr & [COLOR=red]space(10-len(rs.Fields(0)) &[/COLOR] rs.fields(0) & "|" & [COLOR=red]space(10-len(rs.Fields(1)) &[/COLOR] rs.Fields(1) & "|" etc then the final line above would become & BodyStr[COLOR=#ff0000] & vbCrLf[/COLOR][COLOR=#ff0000]& "Total Food" & Space(60-len(Ttl)) & Ttl & vbCrLf[/COLOR]
BodyStr = BodyStr & space(10-len(rs.Fields(0)) & rs.Fields(0) & "|" & rs.Fields(1) & "| " & rs.Fields(2) & "| " & rs.Fields(5) & "| " & rs.Fields(3) & "| " & rs.Fields(7) & "| " & rs.Fields(4) & "| " & rs.Fields(6) & "|" & rs.Fields(8) & vbCrLf
(Select sum(bfast+lunch+desert)
space(10-len(rs.Fields(0)))
(Select sum(bfast+lunch+desert)
(Select sum(nz(bfast)+nz(lunch)+nz(desert))
BodyStr = BodyStr & Space(50 - Len(rs.Fields(0))) & rs.Fields(0) & "|" & rs.Fields(1) & "
What might be a simpler solution is to do your nz'ds in your query
Set rs = db.OpenRecordset("SELECT ID, bfast,lunch,dwine,bar,desert,cellar,twine,tdate, (Select sum(NZ(bfast)+NZ(lunch)+NZ(dwine)+NZ(bar)+NZ(desert)+NZ(cellar)+NZ(twine)) From Sales as tmp WHERE MemberID=Sales.MemberID AND tdate Between #9/1/2012# And #9/30/2012#) as MthTtl FROM Sales WHERE MemberID=" & MemID & " AND tdate Between #11/1/2012# And #11/30/2012#")
Set rs = db.OpenRecordset("SELECT ID, bfast,lunch,dwine,bar,desert,cellar,twine,tdate, (Select sum(NZ(bfast)+NZ(lunch)+NZ(dwine)+NZ(bar)+NZ(desert)+NZ(cellar)+NZ(twine)) From Sales as tmp WHERE MemberID=Sales.MemberID AND tdate Between #11/1/2012# And #11/30/2012#) as MthTtl
[COLOR=red], [FirstName] & " " & [LastName] AS Salutation[/COLOR] FROM Sales [COLOR=red]INNER JOIN MEMBERS ON Members.MemberID=Sales.MembersID [/COLOR]WHERE [COLOR=red]Sales.[/COLOR]MemberID=" & MemID & " AND tdate Between #11/1/2012# And #11/30/2012#")
[COLOR=red]rs.movefirst[/COLOR]
[COLOR=black]If BodyStr <> "" Then BodyStr = [COLOR=red]"Dear " & rs.fields("Salutation") & "," & vbCrLf & vbCrLf &[/COLOR] "Please find below your monthly statement" & vbCrLf & ......[/COLOR]
Set rs = db.OpenRecordset("SELECT ID, bfast,lunch,dwine,bar,desert,cellar,twine,tdate, (Select sum(NZ(bfast)+NZ(lunch)+NZ(dwine)+NZ(bar)+NZ(desert)+NZ(cellar)+NZ(twine)) From Sales as tmp WHERE MemberID=Sales.MemberID AND tdate Between #11/1/2012# And #11/30/2012#) as MthTtl, Firstname & " " & Surname AS Salutation FROM Sales INNER JOIN Members ON Members.MemberID=Items.StaffID WHERE Sales.MemberID=" & MemID & " AND tdate Between #11/1/2012# And #11/30/2012#")