Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2019
    Posts
    1,103

    Requiring query expression that outputs whole numbers in format ".0"

    Experts,



    I need help tweaking a query to calculate an AVG output in numeric format with 1 decimal place, including for whole numbers.

    Attached: ACCDB & illustration.

    Issues:
    ✅ Query_Average_1_Text – Outputs {3.0, 4.0, 5.0} but as text.
    ❌ Query_Average_2_Numeric – Outputs {3, 4, 5} as numeric but without decimals for whole numbers.

    Goal: Modify Query_Average_2_Numeric so SCORE remains numeric AND displays whole numbers as {3.0, 4.0, 5.0}.

    I've tried multiple formats but none worked. Please provide only the actual solution (expression).

    Thank you,
    EEH
    Attached Thumbnails Attached Thumbnails SampleData.png  
    Attached Files Attached Files

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Tom,
    You can set the format in the Properties window of the column in query design view:
    Click image for larger version. 

Name:	Screenshot 2025-03-04 073043.jpg 
Views:	37 
Size:	105.4 KB 
ID:	52810
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Join Date
    Feb 2019
    Posts
    1,103
    Vlad -- thank you for the response. For background purposes, I actually did use the suggested query properties to get to the desired format.

    However, I use VBA (via form) to re-create the query based on different user inputs. Thus, each time when the query is re-generated, the query lost the "Fixed" (1 decimal) settings/properties. Hence, I'd like to include the correct expression into my VBA code to resolve this issue.

    Given this forum is focused on "queries" (not programming), I simply chose to provide a query (vs. VBA) example. Anyhow, below is the VBA element (with slightly different format and field references). That's from my actual application (vs. using simplified demo version).

    Code:
    strSQL = "SELECT Q100_Scoring_Input.TIER_1_ID_H, Q100_Scoring_Input.HYPOTHESES_H, " & _
                                 "ROUND(Avg(Q100_Scoring_Input.[" & strModel & "_SCORE]), 1) AS [AVG_" & strModel & "_SCORE] " & _
                                 "FROM Q100_Scoring_Input " & _
                                 "GROUP BY Q100_Scoring_Input.TIER_1_ID_H, Q100_Scoring_Input.HYPOTHESES_H " & _
                                 "ORDER BY ROUND(Avg(Q100_Scoring_Input.[" & strModel & "_SCORE]), 1) DESC;"
    Any thoughts/recommendation with tweaking it to output the desired format?

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Hi Tom,
    How do you use the strSQL? Do you save it as a query? You can manipulate the format property of a querydef using DAO from VBA, here is one example:
    https://www.access-programmers.co.uk...gh-vba.319253/
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    Perhaps surround the format function with the cDbl or eval function

  6. #6
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180

  7. #7
    Join Date
    Feb 2019
    Posts
    1,103
    Thus far, it seems like all methods are either a) string with whole numbers shown as, e.g., {3.0, 4.0, 5.0} OR b) numeric values where whole numbers only show {3, 4, 5, etc.}. This includes, e.g., using cDbl, etc.

    Looks like I need to do more research...

  8. #8
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    Oh super awesome. </sarcasm>

    If you use FORMAT to do it, the Format function returns a string, not a number. So this will make your numbers look pretty, but they will no longer be numbers, but text strings:

    SELECT TeamResults.ID, TeamResults.EventDate, TeamResults.Teams, FORMAT(TeamResults.Player1,"0.00") AS FormattedPlayer1
    FROM TeamResults;

  9. #9
    jojowhite's Avatar
    jojowhite is online now Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    You Format it on the Query designer not in the Column:
    Click image for larger version. 

Name:	avg.png 
Views:	27 
Size:	28.6 KB 
ID:	52812
    Click image for larger version. 

Name:	qry.png 
Views:	27 
Size:	18.2 KB 
ID:	52813
    or you can use code on the demo datasheet form (jojo_demoDatasheet) i provided on the attached.
    initially when i created this form, the Recordsource is Query_Average_2_Numeric.
    then i remove the recordsouce to make it Unbound.
    next i set the Recordsource on the Load Event of the form, with somewhat
    similar Query string you have on your post.
    then set up the Properties of AVG_SCORE textbox.
    Note also that I am able to Total the column.
    Attached Files Attached Files

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by jojowhite View Post
    You Format it on the Query designer not in the Column:
    Click image for larger version. 

Name:	avg.png 
Views:	27 
Size:	28.6 KB 
ID:	52812
    Click image for larger version. 

Name:	qry.png 
Views:	27 
Size:	18.2 KB 
ID:	52813
    or you can use code on the demo datasheet form (jojo_demoDatasheet) i provided on the attached.
    initially when i created this form, the Recordsource is Query_Average_2_Numeric.
    then i remove the recordsouce to make it Unbound.
    next i set the Recordsource on the Load Event of the form, with somewhat
    similar Query string you have on your post.
    then set up the Properties of AVG_SCORE textbox.
    Note also that I am able to Total the column.
    O/P was already advised of that in post #2
    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
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    We still don't know what happens to the strSQL in the end (how is its output displayed), is it used as recordsource for a form or report, saved as a query that then gets exported, etc...
    Here is another post showing how to change the format using DAO once the strSQL is saved (using a querydef object, the example below is for a table but it is the same idea).
    https://stackoverflow.com/questions/...ype-and-format
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  12. #12
    jojowhite's Avatar
    jojowhite is online now Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    Quote Originally Posted by Welshgasman View Post
    O/P was already advised of that in post #2
    I did not see any Form on his attachment, did You?

  13. #13
    Join Date
    Apr 2017
    Posts
    1,792
    I suspect, OP creates a query string in form, and after that simply runs this query from there using the command - and wants to be the query result table field to be formatted having the returned query table field default format changed instead general to numeric with 1 decimal)!

  14. #14
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Here is a sample using VBA DAO to set the DecimalPlaces property of the field.

    Cheers,
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by jojowhite View Post
    I did not see any Form on his attachment, did You?
    Gicu offered setting format in query field properties as did you?
    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

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 01-06-2022, 05:55 AM
  2. Replies: 5
    Last Post: 05-10-2018, 07:43 AM
  3. Query outputs duplicates from 1 table
    By Govman in forum Queries
    Replies: 1
    Last Post: 02-16-2015, 06:01 PM
  4. Replies: 16
    Last Post: 07-22-2011, 09:23 AM
  5. Replies: 1
    Last Post: 04-07-2010, 08:49 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