Ok here we go......if i have a text box (Text1.text) and its max length is 8 characters.....if someone types 9....how would i get a msgbox to come up on the 9th character?
Printable View
Ok here we go......if i have a text box (Text1.text) and its max length is 8 characters.....if someone types 9....how would i get a msgbox to come up on the 9th character?
if the maxlenght is 8 then there is no way that someone could type 9
Even if the max length is 8....there is still a keypress even is going on.......anyone gonna help.
Try this.
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
'If the key is not Backspace then...
If Not KeyAscii = 8 Then
'Display a MessageBox on the 9th character
If Len(Text1.Text) = 8 Then MsgBox ("No more letters")
End If
End Sub
VB will intercept that 9th keystroke and beep. I don't think you can programatically intercept it without using the API (I'm about 90% sure, Regis!).
If you want a MsgBox to come up on the 9th keystroke, instead of using MaxLength, you could intercept it as it happens in the KeyPress event or after it happens in the Change event. For example:
- or -Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
' weed out control characters
If KeyAscii < 32 Then Exit Sub
If Len(Text1.Text) = 8 Then
MsgBox "Sorry. Limit is 8."
KeyAscii = 0
End If
End Sub
Code:Private Sub Text1_Change
If Len(Text1.Text) > 8 Then
MsgBox "Sorry. Limit is 8."
Text1.Text = Left$(Text1.Text, 8)
End If
End Sub
This works for me
put this in general declarations
Dim max As Integer
Then this anywhere
Private Sub Text1_KeyPress(KeyAscii As Integer)
Let max = max + 1
If max > 8 Then
MsgBox "To many letters", vbCritical,"letter Count"
End If
End Sub
Good Luck
Mark_Dep :) :p
but what if they backspace or type over some of the characters? You'd want the backspace to at least decrease the max variable, and you'd have to consider that the text box could possibly contain characters to begin with.Quote:
Originally posted by mark_dep
This works for me
put this in general declarations
Dim max As Integer
Then this anywhere
Private Sub Text1_KeyPress(KeyAscii As Integer)
Let max = max + 1
If max > 8 Then
MsgBox "To many letters", vbCritical,"letter Count"
End If
End Sub
Good Luck
Mark_Dep :) :p
I would try your suggesions when I get off work, I appreciate the replies!
Megatron it works like a charm! Thanks guys!