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
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
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.
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....)
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?
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
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.
Bookmarks