Results 1 to 5 of 5

Thread: Trouble formatting currency

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Trouble formatting currency

    I am having trouble formatting currency in a textbox.

    What I want is when the user enters a digit it appends it in the textbox with a .00. So if I enter a 2 it would look like 2.00 in the textbox. The problem I am having is when I add more digits it does not display them right. For example lets say I want to enter 235.00. When I enter a 2 it looks like this:
    2.00. When I enter the 3 it looks like this: 32.00 and when I enter a 5 it will look like this: 352.00. Here is the piece of code I am using to format:

    VB Code:
    1. Private Sub txtDispense_Change()
    2.  
    3.    
    4.       txtDispense.Text = Format(txtDispense, "fixed")
    5.    
    6.    
    7. End Sub

    Any help would be much appreciated

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Trouble formatting currency

    Try using the masked edit control. It should do what you want.
    This one formats phone numbers, but you could change it to whatever you want.

    http://vbforums.com/attachment.php?attachmentid=38605

  3. #3
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Trouble formatting currency

    maybe you could format the text box once it has lost focus?

    VB Code:
    1. Private Sub txtDispense_Lost_Focus()
    2.  
    3.    
    4.       txtDispense.Text = Format(txtDispense, "fixed")
    5.    
    6.    
    7. End Sub

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Trouble formatting currency

    Everytime you set the Text property in code, the caret is re-positioned to the beginning.

    To work around this issue you can manually re-position the caret again after formatting the data.

    VB Code:
    1. Private Sub txtDispense_Change()
    2.       Dim lngPos As Long
    3.  
    4.       lngPos = txtDispense.SelStart
    5.  
    6.       txtDispense.Text = Format(txtDispense, "fixed")
    7.    
    8.       txtDispense.SelStart = lngPos
    9.    
    10. End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Smile Re: Trouble formatting currency

    Thanks that worked perfect.

    Post resloved.

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