Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substituting letters for fractions

Status
Not open for further replies.

vepz

Technical User
May 3, 2005
30
US
I need to enter a fraction into one of the fields on my form. I want to use a letter for each fraction like w=1/16, e=1/8, etc up to k=15/16. I would like to be able to enter the letter into the field but have the fraction displayed and the decimal equivalent available for other uses. I am unsure how to accommplish this.

I want to type an E, disply 1/8 on the form and store .125 for calculations.

Thanks,

Greg
 
You can use a table with the various references and a combobox.
 

Or something like this, with the field defined as Text:

Code:
Private Sub YourField_AfterUpdate()
 Select Case YourField
   Case "w"
    Me.YourField = "1/16"
   Case "e"
    Me.YourField = "1/8"
   Case Else
    MsgBox Me.YourField & " Is Not a Valid Entry"
   End Select
 End Sub

Then, to use the field in a calculation

Eval(Me.YourField)

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
missinglinq
The op says "and store .125 for calculations".
 
I tried to use the code because it seemed cleaner than using the combo box.

I went to properties/afterupdate and changed it to event procedure. I then opened the code page and cut and pasted your code, switching the fields to match my appication. I received an error ambiguous name detected, fraction_afterupdate.

Did I not use your code properly? When I enter a w, a w shows in the field and then I get the error message.

I really was not sure how to use your information properly.
 
I would recommend the table idea as well. It is very flexible. If you go with code then every time you want to add a new fraction you will have to rewrite your code.

simply build the table

tblFractions
fractionID
fractionText
decimalValue


I.e

a "1/2" .5
w "1/4" .25
x "1/3" .3333

etc.

now you can add to this table at any time. To use as a combo you will return the first two fields in a query
and show the ID but return the fractionText.

 
I already have the table create. For the record, the fractions are limited to 15 set fractions, no more.

Please clarify how I will enter a letter, display the fraction and store the decimal equivalent in my form. I am fairly new to this and could use some advice.

Thanks,

Greg
 
I have gotten the code to work so when I enter a letter, the proper fraction is displayed.

I am not sure how to use the eval function to return a decimal equivalent.

Any tips?
 
It is a relation database. So if you have a fraction table there is no need for any code. If you store a fraction ID in your main table then link it to the fraction table and you can use the decimal value field in a query.
 

BTW, the error message "ambiguous name detected, fraction_afterupdate" means that you already had a fraction_afterupdate sub in your code; you can only have one.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
I cleaned up the code and things are working well. I have one more issue. I used eval to convert the fraction into the decimal equivalent. However, when there is no fraction, the field is null. I have converted the null field into a 0 for calculation purposes but when I try and use the 0, I receive an error message. Since the original field is a text field so it can accept the letter, I thinkn there is a problem converting between text and number fields. Even though the eval field is a number field.

I can supply more info if I know what is needed to cure.

Thanks,

Greg
 
What are you trying to use the zero for and what error are you using?

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
I am taking the feet, inches and fractions and calculating the length in linear feet.

Feet+inch/12+fraction/12.

I have found if I enter a 0 instead of the letter designation for the fraction, the zero value does work.
 
Another "challenge" I have run into is with the output. I combined [feet],[inch] and [fraction] to produce 5'-6 1/2". However, when the fraction is 0, my output becomes 5'-6 0" whick is not very desirable. I would love to change the output with a 0 fraction to 5'-6". I cannot get a cndition statement to work properly.
 

Let's see the code you use to do the combining of feet/inches/fraction.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
=[feet] & "'-" & [inch] & " " & [fraction] &
 

How about...
Code:
& iif([fraction] = 0,"",[fraction])


Randy
 
I'd use this:
Code:
=[feet] & "'-" & [inch] & IIf([fraction]=0, "", " " & [fraction]) & """"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

I used your suggestion which did eliminate the 0 when the fraction was O. However, when there was a fraction, it reutrns an error message. I cannot see why.
 
it reutrns an error message
Any chance you could post the error message ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top