Measurement conversion

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with two text boxes, one called txtDecimal, and another called
txtLength. I want to enter a decimal value into txtDecimal, and output a feet
and inches value into txtLength. Here is code used preveously in excel for
this.

Public Function lentext(Feetin As Double)
' This function will change a decimal number of feet to the text string
' representation of feet, inches, and fractional inches.
' It will round the fractional inches to the nearest 1/x where x is the
denominator.

Denominator = 8 ' must be 2, 4, 8, 16, 32, 64, 128, etc.
NbrFeet = Int(Feetin)
InchIn = (Feetin - NbrFeet) * 12
NbrInches = Int(InchIn)
FracIn = (InchIn - NbrInches) * Denominator
Numerator = Application.WorksheetFunction.Round(FracIn, 0)
If Numerator = 0 Then
FracText = ""
ElseIf Numerator = Denominator Then
NbrInches = NbrInches + 1
FracText = ""
Else
Do
' If the numerator is even, divide both numerator and divisor by 2
If Numerator = Application.WorksheetFunction.Even(Numerator) Then
Numerator = Numerator / 2
Denominator = Denominator / 2
Else
FracText = " " & Numerator & "/" & Denominator
Exit Do
End If
Loop
End If
lentext = NbrFeet & "' " & NbrInches & FracText & """"
End Function
 
I haven't done any very extensive testing for accuracy, but with that
caveat, here's an adaptation of the code you posted ...

Public Function lentext(Feetin As Double)
' This function will change a decimal number of feet to the text string
' representation of feet, inches, and fractional inches.
' It will round the fractional inches to the nearest 1/x where x is the
denominator.

Dim Denominator As Double
Dim NbrFeet As Double
Dim InchIn As Double
Dim NbrInches As Double
Dim FracIn As Double
Dim Numerator As Double
Dim FracText As String

Denominator = 8 ' must be 2, 4, 8, 16, 32, 64, 128, etc.
NbrFeet = Int(Feetin)
InchIn = (Feetin - NbrFeet) * 12
NbrInches = Int(InchIn)
FracIn = (InchIn - NbrInches) * Denominator
'Numerator = Application.WorksheetFunction.Round(FracIn, 0)
Numerator = Round(FracIn, 0)
If Numerator = 0 Then
FracText = ""
ElseIf Numerator = Denominator Then
NbrInches = NbrInches + 1
FracText = ""
Else
Do
' If the numerator is even, divide both numerator and divisor by
2
'If Numerator = Application.WorksheetFunction.Even(Numerator)
Then
If Numerator Mod 2 = 0 Then
Numerator = Numerator / 2
Denominator = Denominator / 2
Else
FracText = " " & Numerator & "/" & Denominator
Exit Do
End If
Loop
End If
lentext = NbrFeet & "' " & NbrInches & FracText & """"
End Function
 
Then how do I apply this to txtDecimal and txtLength? If I enter this into
the form, how do I make it so when I enter a decimal number into txtDecimal,
to produce a feet and inches to appear in txtLength?

Dave
 
Private Sub txtDecimal_AfterUpdate()

Me.txtLength = lentext(Me.txtDecimal)

End Sub
 
The code I posted compiles without error, but there are various ways that
things can go wrong when copying code from a newsgroup post into the VBA
editor. Please post the code that is not compiling for you. The exact
wording of the error message might help, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top