Validating data in a control array
For this lesson, download the sample project: ReportCard
The Visual Basic form has a powerful tool for creating multiple similar controls in a form. It's called a control array
There is also a very useful Procedure to check the data that is input into a form to make sure that it conforms to the rules set down for the project. That's the Validate Procedure.
Before getting to the Validate procedure, just a few words on formatting the form.
It is usually required that all the controls on a form will be of the same size, color, font and spacing.
For the alignment, sizing and spacing of the controls, use the Format icon on the toolbar.
Using the Validate Procedure on a simple textbox is straightforward:
- create the procedure
- write the validation rules - what is to be checked
- if there's an error, turn on the Cancel argument
- if Cancel is True, the cursor refuses to leave the textbox and the user must enter a valid value
Validating textboxes in a control array is not much more difficult.
Just remember that each textbox now as an index in its name. The Validate procedure will pass the index to the code and you have to use it.