Solved

# Using fractions in Access

Posted on 2003-02-28
Medium Priority
909 Views
Hi All,

I am working on a project which has dimensions in fractions of an inch e.g. 36 3/8". Is there any way that Access can handle fractions and show them in forms and reports?

Thanks in advance for any help.

Regards,

Bill
0
Question by:WJReid
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 9

Expert Comment

ID: 8042785
Access does not natively handle functions, but you can write a VB function to display long double numbers as fractions.
0

LVL 13

Author Comment

ID: 8043367
Hi mcallarse,

Can you tell me if you know of any functions that are available to do this?

Regards,

Bill
0

LVL 2

Expert Comment

ID: 8043423
You can use chr(188), chr(189), and chr(190) to return 1/4, 1/2, and 3/4--but those are strings.  Probably not very helpful.
0

LVL 13

Author Comment

ID: 8043478
Hi kraig,

I need to be able to enter the dimensions like 36 3/8" etc. I don't know if this is possible, but some companies must still be working in inches.

Regards,

Bill
0

LVL 9

Accepted Solution

mcallarse earned 200 total points
ID: 8044355
Here is a set of quick and dirty functions to convert a fraction to a double (frFraction) and a double to a fraction (toFraction). Run myFunction to get an idea of how they work. Quick and dirty are the operating words here; if you end up using the functions, you'll need to account for overflow issues and such.

--

Function frFraction(ByVal strFraction As String) As Double

Dim numDbl As Double

strFraction = Trim(strFraction)

sp = InStr(strFraction, " ")

If sp = 0 Then
sp = InStr(strFraction, "/")
If sp = 0 Then
numDbl = strFraction
Else
numNum = Left(strFraction, sp - 1)
numDen = Mid(strFraction, sp + 1)
End If
If Not numDen = 0 Then
numdbl = numdbl + (numNum / numDen)
End If
Else
numdbl = Left(strFraction, sp)
strFraction = Trim(Mid(strFraction, sp + 1))
sp = InStr(strFraction, "/")
If Not sp = 0 Then
numNum = Left(strFraction, sp - 1)
numDen = Mid(strFraction, sp + 1)
End If
If Not numDen = 0 Then
numDbl = numDbl + (numNum / numDen)
End If
End If

frFraction = numDbl

End Function

Function toFraction(numdbl As Double) As String

Dim numLng As Long, numRem As Double, numNum As Long, numDen As Long
Dim strRem As String, strRemLen As Byte, strFraction As String

numLng = Int(numdbl): numRem = numdbl - numLng

strFraction = numLng

If numRem > 0 Then
strRemLen = Len(Mid(numRem, InStr(numRem, ".") + 1))
numDen = 10 ^ strRemLen
numNum = numRem * numDen
mygcd = GCD(numNum, numDen)
numNum = numNum / mygcd: numDen = numDen / mygcd
strFraction = strFraction & " " & numNum & "/" & numDen
End If

toFraction = strFraction

End Function

Function GCD(ByVal a As Long, ByVal b As Long) As Long

Dim r As Long
Dim hold As Long

If a < b Then
hold = a: a = b: b = hold
End If

If b = 0 Then Exit Function

Do
If Not r = 0 Then
a = b
b = r
End If
r = a Mod b
Loop Until r = 0

GCD = b

End Function

Function myFunction()

MsgBox toFraction(1.5)
MsgBox frFraction("1 1/2")

End Function
0

LVL 5

Expert Comment

ID: 9460523
WJReid,
No comment has been added lately (214 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to mcallarse http:#8044355

-- Please DO NOT accept this comment as an answer ! --

Thanks,

knowlton
EE Cleanup Volunteer
0

LVL 4

Expert Comment

ID: 22859248
mcallarse:
I have many occassions herre on EE as you have assisted me solved many listings. Your problem solving technique is very unique.

I wonder if you would be willing to assist me in listing a new question as a follow up to your code here on fractions.

I need to test the functions but dont know how as there were no further explanations offered her to test the routines you posted.

Regards
Bill
0

## Featured Post

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
###### Suggested Courses
Course of the Month15 days, 10 hours left to enroll