Solved

Converting Fractions to Decimals

Posted on 2003-11-28
6
1,069 Views
Last Modified: 2012-05-04
Using AC2002.

I am using the following code to convert fractions to decimals. However, it won't convert the number if it is a whole number only. Need help in modification.

Private Sub Command1_Click()
Dim x As Integer
Dim y As Integer
Dim z As String
Dim a1 As Integer
Dim a2 As Integer
Dim MyNumber As String

DoCmd.GoToControl "Text1"

x = InStr(1, Text1.Text, " ")
y = InStr(x, Text1.Text, "/")
z = Right$(Text1.Text, Len(Text1.Text) - x)
y = InStr(1, z, "/")
a1 = Val(Left$(z, y - 1))
a2 = Val(Right$(z, Len(z) - y))
MyNumber = Val(Left$(Text1.Text, x - 1)) + a1 / a2
Text1.Text = MyNumber
End Sub
0
Comment
Question by:CatDaddy2003
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9840167
@CatDaddy2003:

> I am using the following code to convert fractions to decimals. However, it
> won't convert the number if it is a whole number only. Need help in modification.

If it is a whole number only, then your value will be missing a "/".  So, check for y = 0, and if it is, then just use the whole value.
     
     Private Sub Command1_Click()
          Dim x As Integer
          Dim y As Integer
          Dim z As String
          Dim a1 As Integer
          Dim a2 As Integer
          Dim MyNumber As String

          DoCmd.GoToControl "Text1"

          x = InStr(1, Text1.Text, " ")
          y = InStr(x, Text1.Text, "/")
          If ( y > 0 ) Then
               z = Right$(Text1.Text, Len(Text1.Text) - x)
               y = InStr(1, z, "/")
               a1 = Val(Left$(z, y - 1))
               a2 = Val(Right$(z, Len(z) - y))
               MyNumber = Val(Left$(Text1.Text, x - 1)) + a1 / a2
          Else
               MyNumber = Val(Text1.Text)
          End IF

          Text1.Text = MyNumber
     End Sub

Hope That Helps,
Dex*
0
 
LVL 2

Expert Comment

by:rthomsen
ID: 9840170
Try using the Eval function just to perform the division.  For Example:

Dim FractionString as string
Dim DecimalValue as Double

FractionString = "5/8"
DecimalValue = Eval(FractionString)



0
 

Author Comment

by:CatDaddy2003
ID: 9840241
I inserted your revised code, following are the results:

13 5/8 became 13.625 (works fine)

13 became a Runtime error '5', invalid procedure call and points to this line in the code:
 y = InStr(x, Text1.Text, "/")

and when I comment out that line, then the results become:

13 5/8 became 135

13 became 13
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 19

Expert Comment

by:Dexstar
ID: 9840245
After further refinement, including rthomsen's suggestion, here is what I came up with:

     Private Sub Command1_Click()
          Dim nPos As Integer
          Dim strValue as String
          Dim MyNumber As String
          Dim nValue As Double

          DoCmd.GoToControl "Text1"
          strValue = Text1.Text
          nPos = InStr( strValue, " " )
          If ( nPos > 0 ) Then
                    nValue = CLng(Left(strValue, nPos-1))
                    strValue = Mid(strValue, nPos+1)
          Else
                    nValue = 0
          End If
          nValue = nValue + Eval( strValue )
          MyNumber = CStr(nValue)
          Text1.Text = MyNumber
     End Sub

If that doesn't work for you, let me know what errors / problems you have.  Thanks.

Dex*
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 250 total points
ID: 9840254
The updated version should fix the issues that you just reported.  Try it out and let me know.

Dex*
0
 

Author Comment

by:CatDaddy2003
ID: 9840268
Worked just fine. Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question