Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Format number as fractions
Robert Brown 
    
4 years ago
I have an Access database I built  for recipes.  Is there a way to format the ingredient amounts as fractions (i.e., so .125 is displayed as 1/8)?
Richard Rost  @Reply  
          
4 years ago
Not easily, no. I've seen some VBA code to approximate it, but it's not a simple thing to implement.
Kevin Yip  @Reply  
     
4 years ago
The simplest way I can think of is:

1. Store all fractions as strings in your table (easy), and have users type fractions in data entry (also easy, and your users may actually prefer this).
2. Use the strings for display (easy).
3. To get the actual value of a fraction, use a query and a custom function (harder).

E.g. If a string called s equals "1/8", then this query:

    " SELECT " & s & " AS Result"

will become:

     SELECT 1/8 AS Result

which will give you the value 0.125, because the query will calculate the expression, provided that it is a valid math expression.  You can perform any valid complex math calculations with this method too: (1/8+2/3*3/4).

If it is not a valid math expression, the query returns an error, which is a good way to validate user entries.

STEP 4: Write a custom function in VBA that validates and calculates the value of a fraction (hardest).  E.g.

Function Calc(s As String) As Variant
    Dim r As Recordset
    On Error GoTo 911
    Set r = CurrentDb.OpenRecordset("SELECT " & s & " AS Result")
    r.MoveFirst
    Calc = r!Result
    Exit Function
911:
    MsgBox "Invalid math expression!"
    Calc = Null
End Function

Using this function, Calc("1/8") will give you 0.125.  An invalid entry returns Null.
Alex Hedley  @Reply  
           
4 years ago
Not sure if this will convert in HTML but have you thought about having a lookup table to the symbol ⅛

1/8
1⁄8
Dan Jackson  @Reply  
            
4 years ago
Kevin - 911?
Robert Brown OP  @Reply  
    
4 years ago
Although I don't know if this is a way to accomplish this, my thought was that there must be a DLL library file that would make this possible because Excel can do this without any trouble.  The issue for me is that this number is used in two calculations: 1. when determining the amount of an ingredient for multiple batches of a given recipe, and 2. when the database combines the same ingredient amounts for a combined shopping list (i.e., a cake recipe and a corn bread recipe each having flour as an ingredient which would be summed to a single amount on the shopping list).
Scott Axton  @Reply  
        
4 years ago
Dan old school reference.  In programming it used to be in some languages that you did jumps by specifying the line number in code.

This is also (my guess) a possible subtle reference to the September 11, 2001 disaster at the twin towers in NYC. Shortened to 911 in the US. (Pronounced Nine - Eleven)

In addition to all that - in the US - 9-1-1 is the universal emergency services number.  When dialed on the phone it connects to an operator that will determine the emergency and hand off the problem to the appropriate agency.  Fire, Police, Ambulance, etc.

In reality, it's just a label.  It the same as saying
       On Error goto ErrHandler
       'code here
       'more code
       Exit Sub

ErrHandler:
      'what to do if errors
End Sub
Dan Jackson  @Reply  
            
4 years ago
Yeah, i'm familiar with the emergency services (FYI, over here in England, its 999) and the 911 disaster. Just couldn't understand Kevin choosing that lol. Seemed very random

Gotos are awesome, i've used that a few times
J David Bacon  @Reply  
    
4 years ago
I am somewhat a novice but how about this...
Setup a table with 2 Fields - Fraction as String, Decimal as Number.
The Ingredient table would also have the same two fields.
The user would then select the Fraction from a Combo Box
Use the After Update Event from the Combo Box to add the Decimal from the Combo Box to the Ingredient table.

What do you think?
Kevin Yip  @Reply  
     
4 years ago
Hi Robert, if you want to perform calculations with fractions and also return the results in fraction format, you need to use automation.  Automation allows Access to "borrow" functions from Excel.  As you said, Excel can output fractions.  The Excel function Text() can format a number into fraction format, so we can use that here.  Below is a custom VBA function CFrac() in Access that "talks" to Excel and uses its Text() function:

' Create an instance of Excel. "G_" is my own naming convention to denote a global variable.
Public G_appExcel As New Excel.Application

Function CFrac(ByVal s As Variant) As String
    ' Convert a string s to fraction; return it as string.
    On Error GoTo 311
    ' Use Text() and specify output format.
    '  "# ######/######" below means the output is a proper fraction (a/b where a<=b) with a max of 6 digits in the fraction.
    ' This will return incorrect result if the fraction has 7 digits or more. So make sure you put in enough # signs.
    CFrac = G_appExcel.WorksheetFunction.Text(Calc(s), "# ######/######")
    Exit Function
311:
    MsgBox "Invalid input value."
    CFrac = Null
End Function

The Calc() function above is my own custom function I mentioned in my previous post.  It does the opposite: it calculates a fraction and returns its true value:

Function Calc(ByVal s As String) As Variant
    Dim r As Recordset
    On Error GoTo 911
    Set r = CurrentDb.OpenRecordset("SELECT " & s & " AS Result")
    r.MoveFirst
    Calc = r!Result
    Exit Function
911:
    MsgBox "Invalid math expression!"
    Calc = Null
End Function

*CAVEAT*: The Text() function is a only a string formatter; it does not calculate.  It will return *any value* as a fraction even if it isn't.  You give it the value of pi (which is irrational), and Text() will still return a fraction.  But if your calculations always involve only fractions, your results will always be fractions, and Text() will always work.

P.S. Hi Dan, I use "311", "611", "911" etc. to help me look for error handlers in my VBA code.  Access doesn't have a global error handler, so you need an error handler in virtually every procedure if you are developing a proper app.  So you can't really name them all the same.  You need a system and some kind of naming convention.  I use 311, 411, 611, etc. to indicate the severity of the error.  Any system can be devised, of course, but it's best to have a system.
Robert Brown OP  @Reply  
    
4 years ago
I really appreciate all of your help.  I'll give these solutions a try and see if they work for me.  I'm pretty new at access VBA.
Dan Jackson  @Reply  
            
4 years ago
I know how you feel. One step at a time

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 10:06:15 AM. PLT: 1s