Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms / May 2007

Tip: Looking for answers? Try searching our database.

Converting a fraction to decimal

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George R - 08 May 2007 20:27 GMT
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.
Klatuu - 08 May 2007 21:00 GMT
Let's say we have a text box named txtFraction to put the enter number and
fraction in and we want it to show in decimal format in a control named
txtDecimal

'Set up a variable the Split function can use.  So we can separate the
number and bothi sides of the fraction into an array:

   strNumber = Replace(Trim(Me.txtFraction), " ", "/")

'Create the array:

   aryAllParts = Split(strNumber, "/")

'Element 0 is the whole number
'Element 1 is left side of the fraction
'Element 2 is the right side of the fraction

'Do the math and put it in the txtDecimal Control

   Me.txtDecimal = cstr(aryAllParts(0)) & format(aryAllParts(1) /
aryAllParts(2), "#.0000")

You can change the formatting to suit yourself, but the leading # in format
string is to surpress the 0 that is alway on the front of a number less than
1.

If I were doing this, I would write a function that would return the
formatted string and call it from txtDecimal's control source

Signature

Dave Hargis, Microsoft Access MVP

> 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
[quoted text clipped - 4 lines]
> and convert to decimal, eg: 4 1/2 to 4.500.
> Thank you for your consideration.
George R - 08 May 2007 22:35 GMT
Thanks Dave,
I wrote the following function and it hung up on the Line: aryAllParts =
Split(strNumber, "/") with the message "Cannot assign to array."

Public Function FractionToDecimal(txtFraction As String) As Single
   'Set up a variable the Split function can use.  So we can separate the
   'number and both sides of the fraction into an array:

   Dim strNumber As String
   Dim aryAllParts(2) As String
   strNumber = Replace(Trim(txtFraction), " ", "/")
   'Create the array:

   aryAllParts = Split(strNumber, "/")

   'Element 0 is the whole number
   'Element 1 is left side of the fraction
   'Element 2 is the right side of the fraction

   'Do the math and put it to the function value
   FractionToDecimal = CSng(CStr(aryAllParts(0)) & Format(aryAllParts(1) /
aryAllParts(2), "#.0000"))

End Function
I would appreciate your comments on how to fix this even though Fred G has
given me another function.
Thaks,
George R.

> Let's say we have a text box named txtFraction to put the enter number and
> fraction in and we want it to show in decimal format in a control named
[quoted text clipped - 33 lines]
> > and convert to decimal, eg: 4 1/2 to 4.500.
> > Thank you for your consideration.
fredg - 08 May 2007 21:45 GMT
> 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
[quoted text clipped - 4 lines]
> 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])
Signature

Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail

George R - 08 May 2007 22:41 GMT
Thanks Fred,
Your function works fine (after adding an error line).
George

> > 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
[quoted text clipped - 49 lines]
>
> DecValue:ConvertFraction([YourFieldName])
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.