Re: Converting a fraction to decimal

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thanks Fred,
Your function works fine (after adding an error line).
George

"fredg" wrote:

On Tue, 8 May 2007 12:27:03 -0700, George R wrote:

I would like to use an unbound control on an unbound form to specify
parameters for a query. I would like the user to enter inches and then
fractional inches and then have another text box show the total of those two
entries in decimal form. How can I convert an entry such as 1/8 inches to
0.125 inches?
Or, if possible, have the user enter inches and fractions in the same box
and convert to decimal, eg: 4 1/2 to 4.500.
Thank you for your consideration.

You can convert the Text value of 4 1/2 using the following function.
Place it in a Module.

Function ConvertFraction(strGetNumber As String) As Double
' Will convert a fraction, such as 12 3/4 to
' it's decimal equivalent, 12.75
Dim dblFraction As Double
Dim intPosition As Integer
Dim strTop As String
Dim strBottom As String
Dim dblWhole As Double
Dim strFraction As String
On Error GoTo Err_Convert
intPosition = InStr(strGetNumber, "/")
If intPosition = 0 Then
ConvertFraction = strGetNumber ' Not a whole number
Exit Function
End If

intPosition = InStr(strGetNumber, " ")
If intPosition > 0 Then
dblWhole = Val(Left(strGetNumber, intPosition - 1))
Else
dblWhole = 0
End If
strFraction = Mid(strGetNumber, intPosition + 1)
intPosition = InStr(strFraction, "/")
strTop = Left(strFraction, intPosition - 1)
strBottom = Mid(strFraction, intPosition + 1)
dblFraction = Val(strTop) / Val(strBottom)
ConvertFraction = dblWhole + dblFraction

Exit_Function:
Exit Function

Err_Convert:
MsgBox "Error #: " & Err.Number & " " & Err.Description,
vbInformation
Resume Exit_Function
End Function

You can call the above function from a query.

DecValue:ConvertFraction([YourFieldName])
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail

.



Relevant Pages

  • Re: Converting a fraction to decimal
    ... I would like the user to enter inches and then ... Or, if possible, have the user enter inches and fractions in the same box ... Dim intPosition As Integer ...
    (microsoft.public.access.forms)
  • Re: Testing for names that sounds alike
    ... Public Function fSoundexAs String ... Dim strSource As String, strEncode As String ... Dim intPosition As Integer ... Dim temp As String ...
    (microsoft.public.access.queries)
  • Re: How to parse a csv string with text qualifiers
    ... >>Public Sub DoTheImport() ... >>Dim FName As Variant ... >>Dim Sep As String ... >>3 inches ...
    (microsoft.public.excel.programming)
  • Re: Next in alphabet
    ... Dim intOrder as Integer ... Dim strLetter as String ... Dim intPosition as Integer ...
    (microsoft.public.access.queries)
  • Re: DrawLine Code Draws Box
    ... with the ScaleMode set to inches by a special subrountine that makes the ... Dim fontRA = New Font ... Dim strReturnAddress As String ...
    (microsoft.public.dotnet.languages.vb)