Skip to main content

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 <= 57) Or KeyAscii = 127 Or KeyAscii = 8) Then
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 <= 122) Or KeyAscii = 127 or (KeyAscii >= 65 And KeyAscii <= 90) Or KeyAscii = 8 Or KeyAscii = 32 Or KeyAscii = 46) Then
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 <= 122) Or (KeyAscii >= 65 And KeyAscii <= 90) Or KeyAscii = 8 Or KeyAscii = 32 Or KeyAscii = 46 Or KeyAscii = 44 Or KeyAscii = 127 Or (KeyAscii >= 48 And KeyAscii <= 57)) Then

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

Comments

  1. hi i need to check the input is alphabet or not

    ReplyDelete
  2. refer point 2 and 3 for all the details you want!
    http://niteshpatare.blogspot.in/2010/11/validations-in-visual-basic-60.html

    ReplyDelete
  3. hii

    this blog is very useful for my project

    ReplyDelete
  4. hii
    this blog is very useful for my project

    ReplyDelete
  5. hey i need to limit the number of entries to enter an phone no.......

    ReplyDelete
  6. This Blog is very UseFull nd if admin can help me i will be very thankfull to Him\Her
    My Email is
    shahzeb_sharif@hotmail.com

    ReplyDelete
  7. this is really important for project...

    ReplyDelete
  8. can you also give validations for e-mail address
    my email is-
    jainmridul12@gmail.com

    ReplyDelete
  9. Thank u this is useful

    ReplyDelete
  10. Please can u provide validation for e-mail

    ReplyDelete
  11. how to check error for 2nd condition?

    ReplyDelete

Post a Comment

Popular posts from this blog

Oracle and VB 6.0 Connectivity (Part 1)

Here is a basic VB application called voter information system demonstrated which explains the concepts quite clearly. The first step required to establish connectivity is preparing the back end and the front end of the project. These steps are divided into two parts first the back end that is the oracle and front end that is the Visual Basic 6.0. Oracle Firstly we need to set up the database. Create the tables, triggers and procedures and database in the oracle. Create tables: Example: create table voter(id number(5) primary key, name varchar2(25),address varchar2(50),age number(3),gender varchar2(6),dob varchar2(30)); create table votertemp(id number(5) primary key, name varchar2(25),address varchar2(50),age number(3),gender varchar2(6),dob varchar2(30)); (we will be using the next table in the triggers that we will be implementing) Visual Basic 6.0 Develop the VB application as in this example given below. Design the necessary forms for the application. The next part is

Oracle and VB 6.0 Connectivity (Part 3)

Now we will see how to insert values into table a table and to view them. Oracle Initially we had created table in part1. Visual Basic 6.0 Now we wil insert data into the table through visual basic. The code for inserting will be as follows When you double click on add button it will open your code window it will have this code! Private Sub Command1_Click() Dim gen As String If (Option1.Value = True) Then gen = "male" ElseIf (Option2.Value = True) Then gen = "female" End If rsv.Open "insert into voter values(" & Text5.Text & ",'" & Text2.Text & "','" & Text3.Text & "','" & Combo1.Text & "','" & gen & "','" & DTPicker1.Value & "')", conv, adOpenDynamic, adLockOptimistic If (rsv.State = 1) Then rsv.Close End If MsgBox ("Data Entered") End Sub This will add the data entered into