Unless I've overlooked something I feel like this has gone off the rails... I've been wrong before
Take a look at this, I took it back to something closer to the code in your original post. Obviously it may not run as is but I've highlighted the parts I'd like to draw your attention to.
Code:
strBoxStart = "<script><table width='auto';style='text-align:left;border:1px solid black;font-family:calibri;border-collapse:collapse;padding:10px'><tr style='background:white;mso-highlight:blue' ctx.shadowblur;20; ctx.shadowcolor;blue></script>"
strBoxEnd = "</tr></table>"
strHTML = "<HTML><Body><table border='3' width=auto'><font color='blue' size='3' face='Times New Roman'><tr><th>DATE</th><th>TIME</th><th>READING</th>" & _
"<th>DOSAGE</th><th>MIN RATIO</th><th>MAX RATIO</th><th>AVG RATIO</th><th>AVG CARBS</th><th>RATIO</th></tr>"
strBody = strHTML
strSQL = "SELECT tblData.TestDATE, tblData.TestTIME, tblData.TestREADING, tblData.TestDOSAGE, tblData.Ratio, tblData.Carbs, tblData.DosageCalc, tblData.TimeOrder " _
& "From tblData " _
& "ORDER BY tblData.TimeOrder DESC;"
strFS = "<font size='3' face='Arial' style=text-align=center; vertical-align=middle>"
strFE = "</font>"
Set rs = CurrentDb.OpenRecordset(strSQL)
dblMin = DMin("Ratio", "tblData", "[Ratio] > " & intMin) '<--- You didn't need to run these domain functions against the recordset, (in this context) they are run independent of it
dblMax = DMax("Ratio", "tblData", "[Ratio] < " & intMax)
dblAvg = DAvg("Ratio", "tblData", "[Ratio] > " & intMin & " And [Ratio] < " & intMax)
dblAvgCarbs = DAvg("Carbs", "tblData")
dblRatio = dblAvgCarbs / dblAvg '<--- your original post left out an equal sign here
Do While Not rs.EOF
strBody = strBody & "<tr>" & _
"<td style='background-color:#F5F5F5'>" & strFS & Format(rs.Fields("TestDATE"), "dddd-dd-mmm-yyyy") & strFE & "</td>" & _
"<td style='background-color:#F8F8FF'>" & strFS & rs.Fields("TestTIME") & strFE & "</td>" & _
"<td style='background-color:#F5F5F5'>" & strFS & rs.Fields("TestREADING") & strFE & "</td>" & _
"<td style='background-color:#F8F8FF'>" & strFS & rs.Fields("TestDOASGE") & strFE & "</td>" & _
"<td style='background-color:#F5F5F5'>" & strFS & Left(dblMin, 4) & strFE & "</td>" & _
"<td style='background-color:#F8F8FF'>" & strFS & Left(dblMax, 4) & strFE & "</td>" & _
"<td style='background-color:#F5F5F5'>" & strFS & Left(dblAvg, 4) & strFE & "</td>" & _
"<td style='background-color:#F8F8FF'>" & strFS & Left(dblAvgCarbs, 4) & strFE & "</td>" & _
"<td style='background-color:#F5F5F5'>" & strFS & Left(dblRatio, 4) & strFE & "</tr>" & "|"
'
rs.MoveNext
Loop
Set olItem = myApp.CreateItem(olMailItem)
Set olAccount = myApp.Session.Accounts.Item(1)
With olItem
.To = "me"
.subject = "Blood Sugar Data"
.HTMLBody = strBoxStart & "<br>" & Replace(strBody, "|", "<br>") & "<br>" & strBoxEnd
.SendUsingAccount = olAccount
.Display
End With
The SQL you posted in #21 above is quite inefficient
Code:
SELECT tblData.TestDATE,
tblData.TestTIME,
tblData.TestREADING,
tblData.TestDOSAGE,
DMin("Ratio","tblData","[Ratio] > 1") AS MinRatio,
DMax("Ratio","tblData","[Ratio] < 20") AS MaxRatio,
DAvg("Ratio","tblData","[Ratio]") AS AvgRatio,
DAvg("Carbs","tblData","[Ratio]") AS AvgCarbs,
[AvgCarbs]\[AvgRatio] AS [Carb_Ratio]
FROM tblData;
It calls DMin(), DMax(), and DAvg(), which are VBA functions, from the SQL engine once on every single row of tblData returning the same value each time when all you needed to do was call them once.