CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Trap for Young (and Old) Players - (Text Box Formatting Trap)

    I cant believe I have never come across this problem before (Its now about 14 years working with VB6 on a daily basis)

    I tested software on my machine - worked well !
    Ran on the customer machine and kept getting strange results

    Finally tracked it to this statement

    Code:
    ValIncTax = Val(txtInvValue.text)
    
    ValIncTax is a Currency Type
    txtInvValue.text is a Text Box
    Here is the problem

    The Text Box had the value formatted as ##,###,##0.00
    eg, 3,245.65

    What was being transferred to ValIncTax was just the 3 ! (The rest was truncated off)
    Worked on my machine because I was using figures no larger than 999.99


    So there's one for the books !

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Trap for Young (and Old) Players - (Text Box Formatting Trap)

    If you had to PRINT the result, it would need a special FORMAT, and you can easily test values that way. Printing a receipt required TWO DECIMAL PLACES extra. Can now create a MAX SIZE, and MAX VALUES to test. Hasn't failed in 14 years. Never went over $1,000 per unit, though. Can do $100K
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Trap for Young (and Old) Players - (Text Box Formatting Trap)

    Yeah Val() only works on numerics the comma kills it. Would get the same result if it were the letter x where the comma is.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Trap for Young (and Old) Players - (Text Box Formatting Trap)

    The Val function stops reading the string at the first character it cannot recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized.

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Trap for Young (and Old) Players - (Text Box Formatting Trap)

    Wait till you put it on a system where the region settings are set to use '.' as the thousand separator and ',' as the decimal.....

    i'v found it better to accept the input straight as an unformatted number, Save/store it, then show the formatted values when edit is done.. (Gotfocus and lostfocus events are the best....)
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Jul 2005
    Posts
    1,083

    Re: Trap for Young (and Old) Players - (Text Box Formatting Trap)

    Or
    You could use the right function to convert string to numeric
    CCUR CDBL CSNG CLNG CINT
    Code:
    If IsNumeric(txtInvValue.Text) Then 
        ValIncTax = CCur(txtInvValue.text)
    End If
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  7. #7
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Trap for Young (and Old) Players - (Text Box Formatting Trap)

    The problem you can run into when using CCur() on a Textbox is if the user enters a non-numeric value. Testing the string first can avoid this however. Formatting the contents of the Textbox afterward (as GremlinSA mentioned) can be helpful in some applications, and it can help "train" the user to enter proper values.
    Code:
    If IsNumeric(Text1.Text) Then
      Text1.Text = Format$(Text1.Text, "##,###,##0.00")
    End If
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Trap for Young (and Old) Players - (Text Box Formatting Trap)

    This is a sample of my tried and trusted method..

    Create a new form .. add two textbox's (simply so that you got two items to transfer focus) then add this code...
    Code:
    Public Text1Val
    
    Private Sub Text1_GotFocus()
        Text1.Text = Text1Val
    End Sub
    
    Private Sub Text1_LostFocus()
        Text1Val = Val(Text1.Text)
        Text1.Text = Format(Text1Val, "$###,###,##0.00")
    End Sub
    Start the App and type a number into text1. and see the effect of when it gets and looses focus..
    As a 'Bonus' you could even add in this code to restrict the keys the textbox accepts..
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If IsNumeric(Chr(KeyAscii)) Then
        Exit Sub
    End If
    If KeyAscii < 20 Then ' also allow control keys.
        Exit Sub
    End If
    KeyAscii = 0
    End Sub
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Trap for Young (and Old) Players - (Text Box Formatting Trap)

    If there is any chance that the value entered may not be numeric then then way to go is to first test it using IsNumeric() then use the CCur() or CLng() or whatever suits.
    Always use [code][/code] tags when posting code.

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

Featured