-
October 23rd, 2012, 08:17 PM
#1
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 !
-
October 23rd, 2012, 10:11 PM
#2
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
-
October 23rd, 2012, 11:46 PM
#3
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.
-
October 24th, 2012, 05:12 AM
#4
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.
-
October 24th, 2012, 06:05 AM
#5
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.
-
October 24th, 2012, 03:04 PM
#6
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 ...
-
October 26th, 2012, 01:24 PM
#7
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?
-
October 27th, 2012, 05:36 AM
#8
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.
-
October 27th, 2012, 08:59 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|