Allowing only certain characters in string in vb6

Berker Yüceer picture Berker Yüceer · Dec 12, 2011 · Viewed 7.9k times · Source

I wanna make only predefined characters to be able to used on my textbox in vb6. How can i achive that?

Predefined characters will be like 0-9 and A, C, M, E all other characters besides these gonna give a msgbox as err. it can also be a,c,m,e i can use Ucase() to solve it.

Answer

Alex K. picture Alex K. · Dec 12, 2011

You can;

private Sub atextbox_KeyPress(keyascii As Integer)
   if InStr(1, "0123456789ACME", Chr$(keyascii)) = 0 Then keyascii  = 0 '//case sensitive
End Sub

or

if Chr$(keyascii) like "[0-9]" or Chr$(keyascii) like "[ACMEacme]"

alternatively formatted

select case true
    case chr$(keyascii) like "[0-9]"
    case chr$(keyascii) like "[ACMEacme]"
    case else
        keyascii = 0
end select