Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.

Similar presentations


Presentation on theme: "Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid."— Presentation transcript:

1 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 1 Chapter 10: Advanced Database Operations

2 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 2 Revising Vintage Videos 1.Search and display based on a member’s phone number. 2.Browse the member information form and add new members or delete existing members. 3.Display price based on ID number for a video on a Rental form. 4.Add ID number, date and phone number to database for each video that is rented. 5.Search for and display videos with a particular name or partial name to determine if they are in stock. 6.Call the customers who have overdue videos and request they be returned. 7.Check videos back in when they are returned, update the availability status of the video and, if late, calculate late fee.

3 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 3 Plan for Revising Vintage Videos

4 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 4 Adding Forms to the Project In design mode, use the Project|Add File menu option to add a file to a project. As each form is added, you should immediately use Save frmname.frm as... to save the form. Once all forms have been added, the project must be saved in order for the forms to be made a permanent part of the project.

5 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 5 Validating Video Input Before displaying video name and price, several items should be validated: 1. that the txtVideoID text box is not blank. 2.that the video ID number matches a video on the database. 3.that the video is not already rented. Since all validations should take place before a certain command button is clicked, the GotFocus event for the button may be used for validation code. An alternative is the Validate event. The Validate event occurs before the focus shifts to a (second) control that has its CausesValidation property set to True

6 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 6 Code to Search for Video by ID Number datVideos.RecordSource = "Videos" datVideos.Refresh strTarget = "ID_Num = " & txtVideoID.Text datVideos.Recordset.FindFirst strTarget If datVideos.Recordset.NoMatch Then MsgBox "Video does not exist", vbCritical, _ "Video Status" txtVideoID = "" txtVideoID.SetFocus End If

7 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 7 Code to Display Video Private Sub cmdCalc_GotFocus() Dim strTarget As String If txtVideoID.Text = "" Then MsgBox "You must enter a valid ID number." txtVideoID.SetFocus Exit Sub Else datVideos.RecordSource = "Videos" datVideos.Refresh strTarget = "ID_Num = " & txtVideoID.Text datVideos.Recordset.FindFirst strTarget If datVideos.Recordset.NoMatch Or _ datVideos.Recordset("Rented") Then MsgBox "Video does not exist or is rented", _ vbCritical, "Video Status" txtVideoID.Text = "" txtVideoID.SetFocus Else txtVideoName.Text = datVideos.Recordset("Video_Name") txtVideoPrice.Text = Format(datVideos.Recordset_ (“Price”),”currency") End If End Sub

8 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 8 Setting the RecordSource at Run Time Requires two statements—one to assign the RecordSource property to the name of a database table and one to refresh the database with the data control Refresh method: datName.RecordSource = "TableName" datName.Refresh In addition to being set equal to a database table, the RecordSource property can be set equal to an SQL query or even to a query created in Microsoft Access. Once the RecordSource property has been set with these two statements, we may work with the new Recordset.

9 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 9 DB Controls Three useful controls for working with databases are: –DBList control (dbl) –DBCombo control (dbc) –DBGrid control (dgd) You will need to add them to the toolbox by using the Project|Components menu option.

10 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 10 The DBGrid Control The DBGrid control can be used to display an entire table or selected rows and columns. It can also be used to dynamically edit the database by saving changes that are entered in the various cells of the control. The DBGrid control is linked to the database by setting its DataSource property equal to a data control on the same form. To prevent the user from changing the contents of DBGrid cells, set the AllowUpdate property to False.

11 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 11 The DBList and DBCombo Controls The DBList and DBCombo controls are useful for displaying just one column of the database table for all or selected rows. They are read-only controls in that you cannot edit the database through them. The DBList has two key properties for displaying records: –The RowSource property points to the data control that will provide the database records to be listed. –The ListField property designates the database field that will be displayed.

12 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 12 SQL Queries An SQL query may be used as the RecordSource property. The basic form of an SQL query: SELECT fieldnames FROM tables WHERE query The query part of the SQL statement looks exactly like the queries we have been using with the FindFirst and FindNext methods. For example: SELECT * FROM Members WHERE City = ’Athens’ (the asterisk is used to find all fields.)

13 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 13 The LIKE Operator The “LIKE” operator allows us to look for field values that partially match a search string input by the user. The query combines the LIKE operator with the asterisk wild-card character (*). The general form of the query using the LIKE Operator is: –SELECT * FROM Table WHERE FieldName LIKE ’*SearchString*’ ( both the asterisks and the search string are enclosed in apostrophes in the query string)

14 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 14 The SelectedItem Property The SelectedItem property of the DBList control can be used to find the bookmark in the database of a selected item. To ensure an item has been selected use the IsNull( ) function to check the SelectedItem property.

15 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 15 More On the DB Controls The DBList (and DBCombo) control can be linked to another DB control so that the field name in the DBList control is updated by clicking a record in the other DB control. To make a link, consider three other properties: –The DataSource property is the name of the data control with the recordset to be updated. –The DataField property is the field to be updated in the changing recordset. –The BoundColumn is the name of the field with the value to be inserted in the table being updated. Note that the BoundColumn and DataField properties point to the same field in different database tables.

16 Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid Control DBList and DBCombo Controls SQL Queries The Data View Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 16 The Data View Window The Data View window allows you to view and modify a database from within Visual Basic. Click on the Data View icon in the toolbar or select Data View window from the View menu bar option. The Data Link Properties window is where you will need to set the database properties. If connection is set properly, you should be able to display Tables, Views, and Stored Procedures folders.


Download ppt "Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid."

Similar presentations


Ads by Google