Results 1 to 5 of 5

Thread: [RESOLVED] Missing decimal places in Userform textbox (Word 2003)

  1. #1

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Resolved [RESOLVED] Missing decimal places in Userform textbox (Word 2003)

    Within my userform there are some text boxes that require a price input. However, I want to set it up so that if a user enters, for example, "950", it will be converted to "950.00" (including decimal places).

    Here's what I have:

    VB Code:
    1. Private Sub txtPrice_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    2.        
    3. 'SELLING PRICE ENTRY FORMAT VALIDATION
    4.  
    5.         If IsNumeric(txtPrice.Value) Then
    6.         txtPrice.Value = Format(CLng(txtPrice.Value), "#,###.##")
    7.         Else
    8.         MsgBox "Please enter a valid selling price!", vbExclamation + vbOKOnly, "Invalid Price Format!"
    9.         txtPrice.Value = Null
    10.         Cancel = True
    11.         End If
    12.                
    13. End Sub

    Using this code, if I enter "950", I get "950." (it shows the decimal point, but nothing else after that). I also lose the the trailing zeros even if I enter them. For example, I enter "950.00", but again get "950".

    I know there is a currency format that can be used, but I had problems with that as well and believe that the currency format relies on regional settings, which I don't want to do.

    I also considered FormatNumber, but my userform may be used in Word 97, which apparently doesn't support this function?

    Any help would be appreciated (and probably relieve my headache).

  2. #2
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Missing decimal places in Userform textbox (Word 2003)

    VB Code:
    1. dim spaces as integer
    2. spaces=Instr(txt,1,".")
    3. spaces=len(txt)-spaces
    4. If(spaces=1) then
    5.  txt=txt & "0"
    6. else if (spaces=2) then
    7.  txt=txt & "00"
    8. else
    9.  txt = txt & ".00"
    10. end if
    This may not work exactly I didnt test the code but it shows the gist of it.

  3. #3

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: Missing decimal places in Userform textbox (Word 2003)

    I'm afraid I don't quite understand this reply or how to apply it.

    Reading the code it looks as if it simply adds ".00" to the value entered in the text box (although this is a guess - my username says it all).

    If I take my original code and change the format to "#,###.00" instead of "#,###.##" then I will get my 2 trailing zeros. However, if a user enters, for example, "949.95" it gets rounded up to "950.00" and I don't want the number to be rounded beyond 2 decimal places.

  4. #4
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Missing decimal places in Userform textbox (Word 2003)

    Your rounding issue is because your converting to a LONG variable using CLng(), which must be a whole number. For a decimal conversion use CDec(). Essentially the called function is just detecting where the decimal is, and if there is already something after a character detect how much has to be filled in.
    VB Code:
    1. Public Function FormatToNumber(txt as String) as String
    2.    dim spaces as integer
    3.    'Find the decimal position
    4.    spaces=Instr(txt,1,".")
    5.    'Determine if there is even a decimal
    6.    If(spaces>=0) Then
    7.       'Determine how many positions are left between the decimal
    8.       'and end of text
    9.       spaces=len(txt)-spaces
    10.       If(Spaces=1) Then
    11.          'Value only has one number after the decimal
    12.          txt=txt & "0"
    13.       End If
    14.       If(spaces=2) Then
    15.          'Value has no numbers after the decimal
    16.          txt=txt & "0"
    17.       End If
    18.    Else
    19.       'Value has no decimals
    20.       txt=txt & ".00"
    21.    End If
    22.   FormatToNumber=txt
    23. End Function

    It is possible to add a function to add the "commas" as well to seperate thousands millions etc.. but I will leave that to you.
    Warning: Double check Instr() parameters, don't think I put the ones in the correct order.

  5. #5

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Resolved Re: Missing decimal places in Userform textbox (Word 2003)

    Although I really appreciate the help, I'm afraid that this solution is a little out of my depth (for the moment).

    In the meantime, I managed to fix it with this:

    VB Code:
    1. Private Sub txtPrice_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    2.        
    3. 'SELLING PRICE ENTRY FORMAT VALIDATION
    4.  
    5.         If IsNumeric(txtPrice.Value) Then
    6.         txtPrice.Value = Format$(CDec(txtPrice.Value), ".00")
    7.         Else
    8.         MsgBox "Please enter a valid price!", vbExclamation + vbOKOnly, "Invalid Price Format!"
    9.         txtPrice.Value = Null
    10.         Cancel = True
    11.         End If
    12.                
    13. End Sub

    I have checked it with integers and decimals and it gives the correct result everytime. The only thing that is a little strange is that I have set the text box to a maximum of 10 characters - now when I enter a 10 digit number it adds .00 to the end making a total of 13. This, however, should not cause any problems because the price should not exceed 9999.99 so I have plenty of room.

    Again, thanks for trying - it's much appreciated. And maybe one day I will get to the point where I can understand how to apply your solution.

Posting Permissions

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



Click Here to Expand Forum to Full Width