Sunday 22 September 2013

Writing a simple Text Validation function in Visual Basic 6.0 IDE

option explicit
' Enum created for validation type
private enum enumValidation
   numericOnly
   alphabetOnly
end enum

private sub ValidateText(ctl as object,byref keyascii as integer,e as enumValidation)
   ' parameter ctl is a by reference to text object, keyascii is the ascii key code value pressed by  user send as byref so that we can modify the value, e is the validation type

          ' if the validation is not met type then keyascii is set to 0 which will prevent the value of the key pressed by user to be printered in the textbox

   select case e
      case numericOnly
        if not isNumeric(keyascii) then               keyascii=0
        end if
       case textOnly
        if  not (keyascii>=65 and keyascii<=90) or (keyascii>=97 and keyascii<=122)  then
               keyascii=0
        end if
        end select        
End Sub

private sub Text1_Keypress(KeyAscii as Integer)
    call ValidateText(text1,Keyascii,alphabetOnly)
End Sub

No comments: