When you are entering data into textboxes, it is a good idea to check that it is sensible. This is called Data Validation.
There are several types of validation:
Presence Check - has data been entered?
Length Check - is the data entered the correct length?
List (or Lookup) Check - is the data entered in a specified list?
Range Check - is the data entered within a specified range?
Type Check - is the data entered of the correct data type?
Format Check - has the data been entered in the correct format?
Click on these links to go the the section you need------>>>>>
Sometimes, we need to make sure data has been entered into a field (or textbox) i.e. we don't want it left empty or blank.
To check if a field is empty, we can use "", which signifies an empty string. A message box tells us if nothing has been entered.
If we want to check the length of data entered, we can use the LEN (length) function and compare it with the valid length.
Compare the length of the data in the textbox, with it's valid length (11 in this example). A message box tells us if the phone number is not 11 digits.
An alternative to using the Len function, is to add .Length to your textbox name:
i.e. If txtPassword.text.Length < 8
MsgBox "Your password must be at least 8 characters"
End If
When we only want the user to choose from a certain set of values, we can do it in one of two ways; use a combo(dropdown box) in VB, or check the valid options written in the code.
Using code:
Using a combo (dropdown) box:
Put a combobox on your form:
2. Choose the Items property and click on (Collection)
3. Enter each valid option on a different line and click OK
4. Dropdown box shows on the form, when you run the code, so you can choose only a valid option.
A range check allows to check that a value is in between two boundaries, an lower and upper boundary.
The < (less than) and > (greater than) are logical operators used regularly in programming - very useful for range checks.
Note: there are two conditions in the IF statement. Each condition has to have both sides of the argument i.e. you have to put txtNumber.text in twice to compare against both the lower and upper boundaries/limits.
You can also do a special type of Range Check, to ensure someone is old enough to log into your system Look at the DATES webpage on this site, for more info.
A type check enables you to make sure that data entered is of the correct data type.
This type of check allows you to ensure that data entered is in the correct format.
You can check that a textbox contains a specific character.
For example, you may want to check that an email has the @ symbol in it.
Another example of a Format Check is when you want to check that a new or changed password meets certain criteria.
In this example, the password needs to contain at least one capital letter and at least one punctuation mark.
This is the ASCII table, showing ASCII values for all characters on our keyboard.
The ASCII values you need to use in your VB program are in the 'dec' columns (short for decimal).
Sub txtHours_KeyPress(KeyAscii As Integer)
'Only allow digits
If KeyAscii<Asc("0") or KeyAscii>Asc("9") Then
Beep
KeyAscii=0
End If
NOTE - Asc find the ASCII Coe and Beep beeps
Can you also implement it for letters from A to Z?