Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency format in text box

Status
Not open for further replies.

iamjd

Technical User
Feb 12, 2005
37
0
0
US
I have tried to make my text box limited to only 1 decimal point with only two numbers after the decimal specifically for US currency.
My code is:

If (KeyAscii < 46 Or KeyAscii > 57) And KeyAscii <> 8 Or KeyAscii = 47 Then KeyAscii = 0
If txtPayRate.SelStart >= InStr(1, strText, ".") Then
If Len(Mid(strText, InStr(1, strText, ".") + 1)) > 0 Then
KeyAscii = 0
End If
End If

I can't figure out a way to make sure only one decimal is put in place followed by only two numbers after the decimal.
 
Try something like this.... Dimension an array, use the split function. Then, if there are more than 2 elements of the array, there are more than 1 decimal point. After it's in an array, you can check the 2nd element (arrays are 0 based so the second element of the array is index number 1).

If (KeyAscii < 46 Or KeyAscii > 57) And KeyAscii <> 8 Or KeyAscii = 47 Then KeyAscii = 0
If txtPayRate.SelStart >= InStr(1, strtext, ".") Then
If Len(Mid(strtext, InStr(1, strtext, ".") + 1)) > 0 Then
arData = Split(strtext.Text, ".")
If UBound(arData) > 1 Then
MsgBox ("Too many decimal points")
End If

If Len(arData(1)) > 2 Then
MsgBox ("Too many numbers after the decimal point")
End If
KeyAscii = 0
End If
End If
 
How about something like this in the validate event of the textbox:

If Isnumeric(text1.Text) Then
text1.text = Format(Text1.Text, "#.00")
Else
Cancel = True
Text1.SelStart = 0
Text1.selLength = Len(Text1.Text)
MsgBox "Hey you didn't enter the number right!!!"
End If




Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
I like DrJavaJoe's solution.

1. It's easier to read.
2. It rounds if there are too many digits after the decimal point.
3. It doesn't mysteriously swallow key presses.
4. Did I mention that it's easier to read?
 
thank you both very much, I have been trying to figure this out.
 
I figured this out. If only I knew how to use the format function earlier.

If (KeyAscii < 46 Or KeyAscii > 57) And KeyAscii <> 8 Or KeyAscii = 47 Then KeyAscii = 0
txtPayRate.text = Format(txtPayRate.text, "#.00")

 
To use the windows currency format look into using the FormatCurrency() function.

zemp
 
that's a great way to do it DrJavaJoe, thanks and congratulations

Golden Dragon Is Coming Soon...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top