Вы находитесь на странице: 1из 7

Session7: Using SQL in VB6.

0 This exercise requires you to use SQL commands to query the contents of a MS Access database. Download the MS Access database session7.mdb. following information: StaffNo 65478 25983 76764 00987 03456 fName Graeme Morris Tim Steve Steve lName Small Middle Top Big Large Phone 34400 56300 54000 33300 33234 This database contains the Salary 33,000 14,000 8,000 7,000 14,000

email g@small m@middle t@top s@big st@lrgte

During this practical you will be required to write an application to perform the SQL queries related to the SQL tutorial (see website for full tutorial sheet). Step 1 Adding the ADO data control (ADODC) and the data grid to your toolbar. To allow your application to run the SQL commands and display their results it is necessary that you add to the toolbar the following components: 1. Microsoft ADO Data Control 6.0 (OLEDB) 2. Microsoft DataGrid Control 6.0 (OLEDB) These can be added to the toolbar by selecting <Project> and then <Components>. You will then be prompted with the screen as shown below. Make the selections as shown and click OK.

Step 2 Creating the main interface Using the data control (as in session 6) create the following interface and ensure that the command buttons allow all records to be displayed. At this point do not attach any code to the query command buttons. MAKE SURE THAT ALL OBJECTS ON THE FORM ARE APPRORIATELY LABELLED USING THE NORMAL CONVENTIONS.

Step 3 Creating the Query Forms Add two further forms to your project. One should be named Queries 1-4 and the other Queries 5-8. These will be used to display the results following activation of the query command buttons on your main form. Attach the necessary code to the command buttons on the main form so that once they are pressed the application will display either the Queries 1-4 form or the Queries 5-8 form.

Step 4 Establishing the ADODC The data control as used in step 2 above (and during session 6) provides various properties, methods and events that enable you to work with a recordset constructed from a database. The ADODC provided by VB can be used to handle not only the database but also various data stores i.e. data that are stored in any form, not just the database format. It is this feature which we will use to allow the results from the SQL queries to be accommodated for. Place an ADODC object onto one of your query forms. Double click on the <ConnectionString> property of the ADODC within the properties window. You will be prompted with the screen as shown below.

Click on <Build> and you will be prompted with the screen as shown below.

Select the Microsoft Jet 4.0 OLE DB Provider option and then select the <Connection> tab.

You will then be prompted with a further screen(see below) requesting you to specify the location of the database you wish to use. Using the browse facilities offered, locate the file session7.mdb and click OK. You have now successfully established a connection with this database.

Step 5 Completing the Query Forms On each of the two query forms you are required to attach 4 labels, the captions of which give details of which SQL queries you will undertake and 4 command buttons. Attached to each of the command buttons you will need to attach the relevant code to perform the SQL queries. The results of each of the SQL will be presented in a datagrid, hence you must add a datagrid object to each of these forms. The following screen shot is an example of how the Queries 1-4 form should be presented.

Code to be attached to the Process Query command button associated with question 1 could be the following:
Private Sub Command1_Click() Dim SQL As String 'This specifies the SQL to be executed SQL = "select * from staff" 'This specifies that the type of the ADODC is for SQL Adodc1.CommandType = adCmdText 'This assigns the SQL result to the ADODC Adodc1.RecordSource = SQL Adodc1.Refresh 'The result from the SQL will then be used 'to populate the datagrid Set DataGrid1.DataSource = Adodc1 End Sub

For the remaining seven queries required (see below) the only code which will change for each of the command buttons is that which is highlighted above i.e. detailing the SQL. Attach the appropriate code to the remaining seven command buttons which will perform the following queries:

1. Produce a list of salaries for all staff, showing only staff number, first and last names, and salary. 2. List all staff (staff number and last name) with a salary greater than 9,000. 3. write the SQL statement which will allow all details from the table to be retrieved. 4. Retrieve a list of the first names stored in the relation/table. 5. Retrieve only the first names that dont occur more than once (hint. Use distinct.) 6. Display each persons first name as well as their pay per month. 7. List the staff numbers of staff that have a salary of less than 10,000. 8. List the staff number of staff that are called Steve and have a salary of more than 12,000. Make sure you save all your work.

Вам также может понравиться