Results 1 to 6 of 6
  1. #1
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49

    Change calculated field result in a query to 3 significant figures

    We have a query that we look at in datasheet view. Fill in some known values into a few fields, and a calculated result based on the entered data shows in the RESULTS field. We would like to have the calculated field end up displaying the result of the calculation in 3 significant digits.



    How do I go about changing the field that contains the calculated result to 3 sig figs?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Would the Round() function work for you?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    Round() does bankers rounding. I have found a function that can handle that aspect of rounding. I think the best way to explain the problem would be a few examples.


    Calculated Result Result in 3 sig figs
    0.167857899 0.168
    37.23789357 37.2
    695.8390326 696
    8924.258434 8920
    78935.36836 78900
    678238.9235 678000

    We accomplished this in excell vba with a function and a subroutine.
    Code:
    Function SigRnd(ByVal d As Double, n As Integer) As Double    ' shg 2008-09
        ' Rounds d to n significant digits
        Const ln10  As Double = 2.30258509299405
        Dim dSgn    As Double
     
        If d <> 0 Then
            If n < 1 Then n = 1
            If n > 16 Then n = 16    ' max precision of double
            dSgn = Sgn(d)
            If d < 0 Then d = -d
            SigRnd = dSgn * WorksheetFunction.Round(d, n - 1 - Int(Log(d) / ln10))
        End If
    End Function
    Code:
    Sub SigFig()'
    ' SigFig Macro
    ' Macro recorded 12/31/2004 by Norman Numnutts
    
    
    ' Use function SigRnd to put digits into sig figs.
    If IsNumeric(ActiveCell) Then
    ActiveCell = SigRnd(ActiveCell, 3)
    Select Case ActiveCell
    Case Is < 1
    Selection.NumberFormat = "0.000"
    Case Is < 10
    Selection.NumberFormat = "0.00"
    Case Is < 100
    Selection.NumberFormat = "0.0"
    Case Is < 1000
    Selection.NumberFormat = "0"
    Case Is < 10000
    Selection.NumberFormat = "00"
    Case Is < 100000
    Selection.NumberFormat = "000"
    Case Is < 1000000
    Selection.NumberFormat = "0000"
    End Select
    End If
    If ActiveSheet.Name = "524" And ActiveCell < 1 Or ActiveSheet.Name = "524 DEP" And ActiveCell < 1 Or ActiveSheet.Name = "524 DEP Sub" And ActiveCell < 1 Or ActiveSheet.Name = "EDEP" And ActiveCell < 1 Or ActiveSheet.Name = "THM DEP" And ActiveCell < 1 Or ActiveSheet.Name = "THM DEP SUB" And ActiveCell < 1 Or ActiveSheet.Name = "Vol Log" And ActiveCell < 1 Or ActiveSheet.Name = "PCE" And ActiveCell < 1 Then
    Selection.NumberFormat = "0.00"
    End If
    
    
    End Sub

    The number format in excel vba allowed us to deal with 0's when there is no decimal point.

    So...

    We have a calculated field [Result] in a table. Based on values entered in a form we get the value for the [Result] field.

    Please bear with me as I am new to Access.

    First question: Do I even want the [Result] field in the table to be a calculated data type, or should it be something like text. Take a variable from the form (call it txtResults), put it in sigfigs, and stick it in the [Result] of the table.

    Second question: How the heck can I get those pesky 0's to show up when numbers are larger than 1000??

    Thanks

  4. #4
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    use the Format function?

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    First question: Do I even want the [Result] field in the table to be a calculated data type, or should it be something like text. Take a variable from the form (call it txtResults), put it in sigfigs, and stick it in the [Result] of the table.
    I would not have a calculated field.

    Second question: How the heck can I get those pesky 0's to show up when numbers are larger than 1000??
    Seems to be there automatically...???

    Is this what you want??
    Attached Files Attached Files

  6. #6
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    Awesome. I was hung up on the implementation. Thx!

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

Similar Threads

  1. Replies: 19
    Last Post: 03-29-2014, 12:48 AM
  2. Calculated field to change a string to a number
    By Cyberwombat in forum Database Design
    Replies: 2
    Last Post: 01-09-2014, 02:48 PM
  3. Change field color based on result
    By dniezby in forum Programming
    Replies: 1
    Last Post: 05-01-2013, 09:43 PM
  4. Replies: 1
    Last Post: 01-09-2013, 02:42 PM
  5. Replies: 2
    Last Post: 05-27-2010, 10:31 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