|
||||
|
LESSON 9 for
Working with a database
The first task to be developed in the application consists of table maintenance. For each of the main tables, Employees, Projects and Departments, there have to be ways to add new records, remove records that are no longer needed and change records when appropriate. For example, new employees are hired and other employees leave, the name of a department is changed or a new project is started. Each of these maintenance operations will require a separate form. Once the maintenance functions are in place, and they have to be (remember: referential integrity dictates that you can't accept a timesheet for a non-existant employee or non-existant project), we can start working on the operational functions, entering timesheets and producing reports. There will be forms for these tasks also. To make it easier to access the different forms, we will create an Application Menu like we did in the previous lesson. The layout of the Menu form is standard and the code consists of a series of Load and Show statements for the various forms. ![]()
The Data ControlTo begin the application, we will first create a new form for Projects maintenance: ProjMaint.The first control we will place on the form, once we've set the basic form properties and saved it, is called the Data Control. It is the object which links a form to a database and allows you to access the fields in the tables making up the database. It's called Data in the Toolbox. VB provides other objects that allow you to link to databases. ADO (ActiveX Data Objects) are certainly more powerful and more efficient than the Data Control. However, they do require a lot more coding and are more difficult to implement. Also, they are not available in the Standard Edition of VB, only in the Professional and Enterprise Editions. In simple applications, the Data Control, slow as it is, gives you a tool that is easy to implement and will provide most of the functionality you need. ![]() The arrow buttons on the control are used to navigate through the database records: The buttons correspond to 4 methods of the DC which you can use when you have to navigate using code. They are: MoveFirst
Let's look at the important properties of the Data Control:
![]()
Notice that we've also added several buttons to the form. These buttons represent the most common actions we have to perform on the records. Important note: when you work with bound controls you have to remember that every time you move off a record, the record is automatically modified in the database - every change you make to a TextBox is reflected immediately in the table. That is why there is no Update button - the function is done automatically. The Reset button allows you to cancel any changes you may have made to the fields - if you modified information and then change your mind about updating it, the Reset will skip the automatic update and return the fields to their original state. This is a method of the Data Control object and is written as: dta_proj.UpdateControls There are 2 ways to Add new records to the table:
To Delete the current record, you must use the Delete method followed by a MoveNext to move off the deleted record: dta_proj.Recordset.Delete ![]() Validating dataBefore the data you are entering get permanently transfered to the database, you often want to make sure they are correct. That is called data validation. We look here at two simple ways of validating data.Let's say that the specs for the Projects maintenance part of the application call for three verifications:
The second technique is to use the LostFocus event. When focus moves off the control, you do the validation. If there is an error, you evoke the SetFocus method to put focus back to the control with the error. ![]() Finding a specific recordWhen you navigate with the arrow buttons or the Move... methods you are necessarily moving one record at a time. Very often there is a need to access a specific record in the database. For example, it might be to change the ending-date for the project called "XYZ Corp. Payroll System".In this example we assume that the search will be conducted on Project title. It could be on Number or End-date if necessary and it would just involve minor changes to the code. We also assume that the user does not want to enter the full project title and will only input the first few characters; we will therefore make use of the "Like" operator to match the recordset to the search string. First, we create a new TextBox, called txt_findTitle, to enter the search string. We will give this TextBox the TabIndex 0 because we want it to be the first control read when we look at a record. As soon as we move off the TextBox, the LostFocus event is triggered and checks whether the user has entered a search string or not. If there is no input into the search string, the user can work with the current record in the form. If there is a search string specified, the appropriate record will be loaded into the form. The FindFirst method of the DC will locate the first occurence in the recordset matching the "content" parameter. If there are more than one records that match, the user then navigates forward using the arrows. The format of the FindFirst method is: DataControl.Recordset.FindFirst "fieldname = 'searchstring'" ![]() ![]() You might also want to visit this site for Free Visual Basic 6 tutorials and sample source code examples .
If you haven't found the Visual Basic resource you're looking for,
|