Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371

    Struggling With Calculating Recordset Fields !!!

    Hi Guy's I am struggling to work out how to add calculated fields to recordset results!



    Is this better setup in the query statement or setup within the loop, I think i know where i am going wrong, the DMin, DAvg and DMax require a table source instsead of rs.field perhaps ???

    I can't for the life of me set this up when i should be able to, should be simple, just can't see it!!!!

    Trying to add Min, Avg, Max And a division to recordset email:

    Note: full event not added, all are Dimmed per name

    Code:
        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;"
    
    
    
    
        Set rs = CurrentDb.OpenRecordset(strSQL)
    
    
        Do While Not rs.EOF
        
        dblMin = DMin(rs.Fields("Ratio"), , "[Ratio] > " & intMin)
        dblMax = DMax(rs.Fields("Ratio"), , "[Ratio] < " & intMax)
        dblAvg = DAvg(rs.Fields("Ratio"), , "[Ratio] > " & intMin & " And [Ratio] < " & intMax)
        dblAvgCarbs = DAvg(rs.Fields("Carbs"))
        dblRatio dblAvgCarbs / dblAvg
        
        
        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
        
       Debug.Print rsbody

  2. #2
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    I Might have this guy's, will come back and update soon

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Why aren't you using the table?

    I have never seen anyone try this.?

    The docs say you need a domain. I would not say a recordset was a domain.?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    No, i am struggling to do this what should be simple!!!!

    trying calc fields in query so i can write it to SQL, getting data type mismatch error

    all number fields are set to Number in Double format

    Click image for larger version. 

Name:	Capture.JPG 
Views:	21 
Size:	56.3 KB 
ID:	50390

    Just tried adding [ ] in the first couple of criterias as i didn't see that error but still data type mistach

    Am i doing this calculated fields correct (obviously not) lol

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    Am i doing this calculated fields correct (obviously not) lol
    No - to get a record to return, it would need to have a ratio which was equal to the minimum , maximum and and average ratio - plus your carbs and insulin criteria as well. So might work if your tblData contained just one record or multiple records with exactly the same values.

    It looks to me like your whole approach is wrong. Suggest explain what you are trying to do, provide some example data and the result required from that example data and what you want to do with it subsequently (display in a form or report, edit the raw data, whatever).

  6. #6
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Ahh thanks guy's, you are suggesting add fields called MinRatio, AvgRatio etc etc, then run an update query with the calculations ?

    so there would be no criteria's in the select query then add to SQL ?

  7. #7
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    you are suggesting add fields called MinRatio, AvgRatio etc etc, then run an update query with the calculations ?
    I'm not suggesting anything since it is not clear what you are trying to do

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    I just use the Avg(), Sum() etc in queries, daily, weekly and monthly.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    Possibly. Without knowing what you trying to do, I'm not prepared to guess. Try it and see if it meets your requirements. You probably need two queries one an aggregate to get your min/max etc and another based on that for your calculations. Just be aware every time you add, delete or modify a record then min/max etc is likely to change, so storing the values seems a futile exercise since you will need to recalculate every time.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Dave,
    They would not be criteria but actual fields in the query.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hi CJ London, and WGM, yes data will change as this is to do with fluctuating blood sugar levels, I have tried setup a query (post 4) to have this query as an SQL statement in my recordset, this calculation will differ when a new enter is added into blood sugar test reading

    I think i have the syntax wrong in post 4 ?

    I just can't remember how to do calculations in a self made query field

    Thanks for your help

  12. #12
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    I can then set the recordset to these self made fields MinRatio, AvgRatio, MaxRatio, AvgCarbs and Insulin Req

    The ratio based on carbs eaten as well as readings will keep changing the ratios, i do have the calculations on text boxes on a form, but i don't think i can integrate text boxes into an SQL recordset.

    These are the formulas i am trying to add to query field (post4)

    Code:
    dblMin = DMin(rs.Fields("Ratio"), , "[Ratio] > " & intMin)
    dblMax = DMax(rs.Fields("Ratio"), , "[Ratio] < " & intMax)
    dblAvg = DAvg(rs.Fields("Ratio"), , "[Ratio] > " & intMin & " And [Ratio] < " & intMax)
    dblAvgCarbs = DAvg(rs.Fields("Carbs"))
    dblRatio dblAvgCarbs / dblAvg
    intMin is 1
    intMax is 20

    so i know these would need to be changed in the query calc

  13. #13
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    I mean no offense but... do the results of these queries have the potential to harm patients???? If so then I think (again I mean no offense) you need to hire a professional for this application... There are a lot of mistakes and fundamental misunderstanding here that need to be addressed.

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    It is for his use only @kd2017
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    So use the table fields?
    You cannot just make stuff up.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 03-29-2022, 09:53 AM
  2. Query not calculating all fields
    By koncreat in forum Queries
    Replies: 2
    Last Post: 09-27-2018, 02:50 PM
  3. Calculating a two date fields! Help please
    By Enigma in forum Access
    Replies: 3
    Last Post: 01-29-2017, 11:32 AM
  4. Calculating subform fields
    By Tvanduzee in forum Forms
    Replies: 25
    Last Post: 09-17-2012, 06:42 AM
  5. Replies: 0
    Last Post: 03-09-2012, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums