Hi all,
I have various textbox's where users can input a number.
Using one as an example of my problem...
I want to limit the input of a percentage to upto 99.99.
I know I can add validation after the users entered a value but I'm trying to be a bit smarter and not allow an invalid value to be entered in the first place.
I've restricted the length of the textbox to 5 chars and I limit entries to 0-9, '.' and backspace within the KeyPress event. However this wouldn't stop a user from entering 99999. So I've also tried to anticipate what the entry value will be once the new keystroke takes effect. All works OK unless... the user enters 99.9 then moves the cursor back and enters another 9 (so the result is 999.9). My code passes this value a valid as it adds the new keystroke to the end of the existing value so anticipating the value will be 99.99.
Is there a way I can establish where the cursor position is within the textbox so I can more accurately predict what the new value will be.?
Here's my code....
Thanks in advance for your help.
I have various textbox's where users can input a number.
Using one as an example of my problem...
I want to limit the input of a percentage to upto 99.99.
I know I can add validation after the users entered a value but I'm trying to be a bit smarter and not allow an invalid value to be entered in the first place.
I've restricted the length of the textbox to 5 chars and I limit entries to 0-9, '.' and backspace within the KeyPress event. However this wouldn't stop a user from entering 99999. So I've also tried to anticipate what the entry value will be once the new keystroke takes effect. All works OK unless... the user enters 99.9 then moves the cursor back and enters another 9 (so the result is 999.9). My code passes this value a valid as it adds the new keystroke to the end of the existing value so anticipating the value will be 99.99.
Is there a way I can establish where the cursor position is within the textbox so I can more accurately predict what the new value will be.?
Here's my code....
Code:
Private Sub txtCommisionValue_KeyPress(Index As Integer, KeyAscii As Integer)
Dim sValue As String
Dim cValue As Currency
Dim cMaxValue As Currency
'Ensure only numeric values entered...(ignore backspace)
If KeyAscii <> 8 Then
KeyAscii = Mask(KeyAscii, MASK_DECIMAL, txtCommisionValue(Index), False, 2)
If KeyAscii > 0 Then
If Len(Trim(txtCommisionValue(Index))) > 0 Then
cMaxValue = 99.99
'calculate new value..
sValue = txtCommisionValue(Index) + Chr(KeyAscii)
cValue = CCur(sValue)
'Ensure new value is not too large..
If cValue > cMaxValue Then
KeyAscii = 0
End If
End If
End If
End If
End Sub
Thanks in advance for your help.