Solved

Form number field decimals based on a second field

Posted on 2014-07-29
7
248 Views
Last Modified: 2014-07-30
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
Comment
Question by:SteveL13
7 Comments
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 250 total points
ID: 40227424
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

Open in new window


print roundtonearest(3.45678, .001)
 3.456
0
 
LVL 13

Expert Comment

by:Russell Fox
ID: 40227476
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)

Open in new window

0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40228642
This truncates the decimals while performing a 4/5 rounding:

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

/gustav
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:SteveL13
ID: 40229112
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

by:Gustav Brock
ID: 40229127
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

by:SteveL13
ID: 40229186
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

by:
Gustav Brock earned 250 total points
ID: 40229208
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now