How to determine if there are at least two characters entered in a text box?
I like the Change Event approach also because it seems to cover all types of
input. Another approach that works and requires no key by key or paste by
paste validation is to use a command button (an OK button) on your form
that when clicked validates all of the data entered on the form. If it
finds something it doesn't like, it displays a message box and returns focus
to the offending control. This is an efficient way to do it but limits your
flexibility somewhat. There will always be situations where you need to
validate as data is entered. One example is to use data entered at the
beginning of a form to determine default values for controls further along
on the form. The Change Event seems to be the way to handle that type of
situation. I look forward to using VB6 with the new Validation Event. It
might solve all these problems.
>>To answer the second part of your question.
>>Instead of checking whether a user has entered something you do not want
>>them to, why not disallow them to do so by using the KeyPress event: -
>> Private Sub txtInput_KeyPress(KeyAscii As Integer)
>>Code this sort of thing: -
>> Select Case KeyAscii
>> Case 0 To 7, 9 To 35, 37 To 64, 91 To 96, 123 To 162, 164 To 255
>> KeyAscii = 0
>> End Select
>The one problem in this approach is that is does not prevent the user from
>PASTING text into the box. (I admit that I use this method, so my
>"validated" text boxes are susceptible to this "back door" as well.) The
>recommend solution is to use the Change event of the text box. I personally
>opt for a combination of the two. Perhaps someday I'll sit down and see if
>the text box can be subclassed to prevent pasting. (Probably all that needs
>to be done is to trap and ignore WM_PASTE.) Then again, that wouldn't be
>very Windows-like, would it?
>And although someone said he could prevent the user from using Ctrl-V to
>paste by trapping Chr$(22), this doesn't work with Shift-Insert.
>Bottom line: Change event.