Solved

# Form number field decimals based on a second field

Posted on 2014-07-29
247 Views
An expert helped me a while back with this topic but for some reason it is not working as I thought it was.

I have two fields on a form.  The first one is named "txtC_SIG_FIG" and is a number field and the data entered into this field is always a whole number with no decimals.  The 2nd one is named "txtC_MIN".  When a number is entered into the 2nd field I want it to carry out to the number of decimals entered in the 1st field.

So, for example, if txtC_SIG_FIG is 3, and if I enter 1.23412 in the 2nd field, I want the result in the 2nd field to display as 1.234

But if the value in txtC_SIG_FIG is 4, and if I enter 1.23412 in the 2nd field, I want the result in the 2nd field to display as 1.2341

Note:  The properties of the 2nd field in the table and on the form is Standard, auto decimals.

The current code which is not working is:

Me.txtC_MIN = Format([txtC_MIN], "0." & Right(10 ^ [txtC_SIG_FIG], Len(10 ^ [txtC_SIG_FIG]) - 1))
0
Question by:SteveL13

LVL 34

Assisted Solution

PatHartman earned 250 total points
Here's a function I found some place.  I didn't write it and I'm sorry I don't remember who did.

``````Function RoundToNearest(dblNumber As Double, varRoundAmount As Double, _
Optional varUp As Variant) As Double

'''The following example creates a user-defined function called RoundToNearest, which accepts three parameters:
'''
'''dblNumber        The numeric value you want to round
'''varRoundAmount   The increment to which dblNumber will be rounded
'''varUp            An optional argument: include to round up; omit to round down
''' For example:
''''•RoundToNearest(3.33, 0.1, up) returns the value 3.4.'
''''•RoundToNearest(3.33, 0.1) returns the value 3.3.
''''.RoundToNearest(3.45678, 0.001) returns the value 3.456.
''''•RoundToNearest(3.33, 1, up) returns the value 4.'
''''•RoundToNearest(3.33, 1) returns the value 3.

Dim dblTemp As Double
Dim intTemp As Integer

dblTemp = dblNumber / varRoundAmount
intTemp = Int(dblTemp)

If intTemp = dblTemp Then
RoundToNearest = dblNumber
Else
If IsMissing(varUp) Then
' round down
dblTemp = intTemp
Else
' round up
dblTemp = intTemp + 1
End If
RoundToNearest = dblTemp * varRoundAmount
End If
End Function
``````

print roundtonearest(3.45678, .001)
3.456
0

LVL 13

Expert Comment

This treats it like text, searching for the "." and moving the appropriate number of places past that:
``````=LEFT(TEXT(txtC_MIN,"#.############"), SEARCH(".",txtC_MIN)+txtC_SIG_FIG)
``````
0

LVL 49

Expert Comment

This truncates the decimals while performing a 4/5 rounding:

Me.txtC_MIN  = Format([txtC_MIN], "0." & String([txtC_SIG_FIG], "0"))

/gustav
0

Author Comment

I think we're getting close.  If I enter 1.12345 and the decimal qualifier is 3 I get 1.123  -  Perfect.

But if I enter 1.1 I get 1.1 and want to see 1.100

In other words, the number of decimals shown should always be the decimal qualifier (txtC_SIG_FIG)

????
0

LVL 49

Expert Comment

Then you'll have to adjust the Format property of the textbox as well:

Me.txtC_MIN.Format  = "0." & String([txtC_SIG_FIG], "0")

/gustav
0

Author Comment

Once more..  this is working great...

Me.txtC_MIN.Format  = "0." & String([txtC_SIG_FIG], "0")

But if the decimal qualifier is "0" and I enter 1 I am getting "1." (with the decimal.

Is there a way to get rid of the decimal if I enter a whole number and the qualifier is "0"?
0

LVL 49

Accepted Solution

Gustav Brock earned 250 total points
Yes, though not very smart:

Me.txtC_MIN  = Format([txtC_MIN], "0" & Left(".", [txtC_SIG_FIG]) & String([txtC_SIG_FIG], "0"))
Me.txtC_MIN.Format  = "0" & Left(".", [txtC_SIG_FIG]) & String([txtC_SIG_FIG], "0")

/gustav
0

## Featured Post

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …