Validations in Visual Basic 6.0

This topic will demonstrate how to perform client side validations in Visual Basic 6.0. It will guide you with a step by step procedure.

1)
Firstly in the below image suppose you want to accept only integer numbers as id in the textbox next to ID label in the form from the user you can perform client side validations in the following way:
These are the ascii codes that i am using for acepting only integer values from user.
Back space – 8
0 to 9 Integers – 48 to 57 
Delete – 127

This is how the function will look.

Private Sub Text2_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 48 And KeyAscii MsgBox “enter proper value”
KeyAscii = 0
End If
End Sub

Double click on the text box which you want to validate.

Next select the event as keypress from top right list box. In this case the name of text box is text1.

This is the function for accepting input as integer value only from the user.

When we provide any other value other than numbers say any alphabet is typed in this text box it will immediately display the message from the msgbox.

2)
Similarly for accepting only alphabetical values.
These are the ascii codes that i am using for acepting only name from user in the textbox for name input.
Back Space – 8
Space – 32
Full Stop – 46
Capital Alphabet – 65 to 90
Small Alphabet – 97 to 122
Delete – 127

This is how the function will look.

Private Sub Text5_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 97 And KeyAscii = 65 And KeyAscii MsgBox “enter only alphabets!”
KeyAscii = 0
End If
End Sub

3)
Similarly for accepting alphabetical values for fields such as address.
These are the ascii codes that i am using for acepting address from user in the textbox for address input.
Back Space – 8
Space – 32
Comma – 44
Full Stop – 46
0 to 9 Integers – 48 to 57 
Capital Alphabet – 65 to 90
Small Alphabet – 97 to 122
Delete – 127

This is how the function will look.

Private Sub Text3_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 97 And KeyAscii = 65 And KeyAscii = 48 And KeyAscii
MsgBox “enter proper values”
KeyAscii = 0
End If
End Sub

4)
I you are making entry into a database for an employee and his joining date should be before todays date then it could be like this.

This is the code for that.
If CDate(Text3.Text) >= Date Then
MsgBox “hire date must be in the past!”
Text3.SetFocus

Enhanced by Zemanta
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s