Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 930

# Using fractions in Access

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
WJReid
1 Solution

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

Author Commented:
Hi mcallarse,

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

Regards,

Bill
0

Commented:
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

Author Commented:
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

Commented:
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

Web developerCommented:
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

Commented:
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

Tackle projects and never again get stuck behind a technical roadblock.