Which does format the value to the correct format. This is in the lostFocus event handler.
If i type 1000, it changes it to £1,000.00
But if i type £1,000.00, it changes the value to £0.00.
How can i check to see if the text is already in the correct format?
Try to dump everything except the numbers and the “.”, then make it a number first.
Also look at the mask property of the textfield. See if you can use that to limit what people can enter.
When you do val(me.text) the value method hits the £ char., decides this is not a number, and returns a value of 0. For best results with whatever is entered in your field, use the mask property as Dirk suggested.
“Mask escape character.
Treats the next character in the mask as a literal. The escape character enables you to use the ‘#’, ‘&’, ‘A’, ‘?’ (and so on) characters in the mask. The escaped character is treated as a literal (formatting) character. For example, the mask “\C\C-9999” accepts the entry “1234” and returns “CC-1234”.”
Function Unformat(s As String) As Double
Dim i As Integer
Dim ch As String
Dim result() As String
for i = 1 to Len(s)
ch = Mid(s, i, 1)
if IsNumeric(ch) or (ch = ".") then
result.Append(ch)
end if
next i
return Val(Join(result, ""))
End Function
Thanks Alwyn.
Roger - That doesnt work properly. Using an input mask of “\C\C-9999” requires you to type a capital C before you can type anything else.
How about filtering the input at keydown or keypress, keeping only numeric characters and decimal separators of your choice (or one defined by system defaults, and doing the formatting at lost focus?
You may also want to allow delete, backspace and other such keys depending on your specific needs.