Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 24

Thread: Solved: Calculation in a TextBox

  1. #1

    Solved: Calculation in a TextBox

    Hi all,

    I am having one more problem with my form that I am working on:

    I need to add a TextBox that caluclates Feet/Inches to centimeters upon "Exit".

    example: the user enters 5'3" and tabs out of the box and 160 cm appears in that box.

    I do know that the user will have to enter the 5'3" value in a different way" 503 for this to work. Right?? And that I need to run a macro to get this done.

    Please me - as this is a bit too much for this VBA Newbie.

    Thanks in advance for all your help

    Trudy

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi ctengelen,

    The below code will take the text from text1 (in the format 12'-4 3/16 or 12'4 3/16) in the active document and return the metric conversion.

    [VBA]
    Function GenDec(FtInch As String) As Double
    Dim mFeet As Integer
    Dim mInch As Integer
    Dim mFract As Double
    Dim mI As Integer
    Dim mHldStr As String
    Dim mIncSix As Variant
    mHldStr = FtInch
    mFeet = Val(mHldStr)
    mI = InStr(1, mHldStr, "'-")
    If mI = 0 Then mI = InStr(1, mHldStr, "'")
    If mI > 0 Then
    mHldStr = Replace(Mid(mHldStr, mI + 1), "-", vbNullString)
    Else 'means no feet
    mFeet = 0
    End If
    mIncSix = Split(mHldStr, " ")
    mInch = Val(mIncSix(0))
    If UBound(mIncSix) > 0 Then
    mI = InStr(1, mIncSix(1), "/")
    mFract = Val(mIncSix(1)) / Val(Mid(mIncSix(1), mI + 1))
    ElseIf InStr(1, mIncSix(0), "/") > 0 Then
    mInch = 0
    mI = InStr(1, mIncSix(0), "/")
    mFract = Val(mIncSix(0)) / Val(Mid(mIncSix(0), mI + 1))
    End If
    GenDec = mFeet * 12 + mInch + mFract
    End Function
    Function MakeMetric(FeetText As String) As Double
    MakeMetric = GenDec(FeetText) * 2.54
    End Function
    Public Sub GetInfo()
    Dim Info As FormField
    Set Info = ActiveDocument.FormFields("Text1")
    Info.Result = MakeMetric(Info.Result)
    End Sub
    [/VBA]

    HTH

  3. #3
    Hi Tommy,

    This is GREAT!!!! Just one more small thing - I do not want the fraction for the conversion - only up to the whole centimeter. What do I change???

    Trudy
    Forever grateful.

  4. #4
    Hello again Tommy,

    Now my boss - seing that it is possible to do the height conversion looks for a weight conversion also. Can't satisfy them bosses no how!

    Pretty Please!!!!

  5. #5
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    [VBA]
    Public Sub GetInfo()
    Dim Info As FormField
    Set Info = ActiveDocument.FormFields("Text1")
    Info.Result = Round(MakeMetric(Info.Result) ,0) '<-- this line add round to round to the first decimal
    End Sub
    [/VBA]

  6. #6
    Tommy this works GREAT!!!!!

    Thanks you so much.

    Trudy

  7. #7
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location

    Weights - are they entered? or do they need to be calculated from lengths?
    what is the unit of measuresment? Lbs to ? LOL I've got to the point of just saying ok to them bosses

  8. #8
    Hi Tommy,

    Yep, it is the best to just smile and say sure to them darn bosses. But to say the least You have totally impressed him.

    The weight measurements are from lbs. to kilogram and again without the fractions.

    The part of the form I am working on at this moment is for description of people. Once I have completed this part (I hope) this form should be in bed, so to speak. Then I will be able to get my brain wrapped around this conversion macro to try to learn from it.

    Thanks one more time.

    Trudy

  9. #9
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    This should make it happen
    [VBA]
    Function ConvertLbsToKG(iLbs As String) As Double
    ConvertLbsToKG = Round((Val(iLbs) * 0.45359237), 0)
    End Function
    Public Sub GetLbs()
    Dim Info As FormField
    Set Info = ActiveDocument.FormFields("Text2")
    Info.Result = CStr(ConvertLbsToKG(Info.Result))
    End Sub

    [/VBA]

  10. #10


    Tommy - you are GREAT! Thank you so so much for your help!

    From my boss a handshake for thank you. (I just hope he doesn't come up with more and 'better' ideas on making this form work.)

    Forever grateful.

    Trudy

  11. #11


    I knew it!!!!

    Now he wants to have the original feet/inches measurement shown in one TextBox (TextBox1) and the conversion than going into the next TextBox (TextBox2). Same for the weight.

    So it looks like: 5'3" ft/in 160 cm

    At least this is happening to me on a Friday and not on a Monday!!!!

    Give a Boss an INCH and he wants it also in cm.

    Trudy

  12. #12
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Is this textbox on a form or in a doc? If it is in a doc I can't seem to get a hold of it. in a form no problem.

  13. #13
    The textbox is in a form.

  14. #14
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    I am using the same functons I am just passing the text from the textboxes.
    [VBA]
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox2.Text = MakeMetric(TextBox1.Text)
    End Sub

    Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox4.Text = ConvertLbsToKG(TextBox3.Text)
    End Sub
    [/VBA]

  15. #15
    Hello again Tommy... so far I could follow and implement the two prior macros and they work just great...where do I now place the last one - at the end of the MakeMetric code or in a seperate module??

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox2.Text = MakeMetric(TextBox1.Text)
    End Sub

    This is not healthy for my brain to work that hard so late in the week.

    Trudy

  16. #16
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    LOL put the macros in the form. In this form there should be 4 textboxes, named TextBox1,TextBox2, TextBox3, and TextBox4.
    TextBox1 is the input for feet. TextBox2 is output for CM.
    TextBox3 is input for lbs. TextBox4 is output for kg.

    I used the default naming conventions for examples only, I would normally name the TextBox1-> "InputFeet", TextBox2 ->"OutCm", TextBox3 ->"InputLbs", TextBox4 ->"Outkg", this way I can look at the names and tell immediately what is going on. I would suggest that you name the textboxes in a manner that makes sense to you. I have included a sample file for you, this is what I tested with.

  17. #17


    Hi again Tommy.

    This is really to have kittens on a Friday afternoon.

    The UserForm works wonderful = IF= the form is unprotected, but as soon as I protect the from again it stops and laughs at me (or so it seems).

    Can I Unprotect the form just BEFORE I activate the UserForm and protect it again immediatly after???

    Trudy

    I attached a little word document for your Friday joy.

  18. #18
    See how bad it is getting - I even forgot the attachment.

    I need a drink!!!!!!!!!!!!!!!

  19. #19
    Tommy - Tommy - Tommy!

    It is me again.....I found a way to Unprotect my document.

    I wrote another little macro that runs when the user enters a textbox. This macro will unprotect the form and runs the Userform. But no matter what I try, I can't protect the form after again.

    With this - I will leave to go home. I hope you will have a great weekend and thanks for all your help.

    Trudy

  20. #20
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Nice job Tomy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •