![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
To create the form, select Form from the Insert menu. Set the forms Name property to lstForm, and its Caption property to List All Recordings. Save the form with the name FRMLIST2. Add a List Box and a Command Button control. Set the Command Buttons Caption property to &Close, making sure that the List Boxs Sort property is set to True. Dont worry about the size and position of these controls, as they will be set in code. Listing 19.17 shows the objects and properties for this form. Listing 19.17 Objects and properties in FRMLIST2.FRM. Begin VB.Form lstForm Caption = List All Recordings Begin VB.CommandButton cmdClose Caption = &Close End Begin VB.ListBox List1 End End The code in FRMLIST2.FRM contains only four event procedures. The Click procedure for the Command Button is simple, using only the Unload statement to unload the form and return the focus to the programs main form. The Form_Load procedure is somewhat more complex. Here you need to extract the Title field data from all of the records in the Recordings table and load them into the List Box (which automatically sorts them). Start by saving the current records bookmark, so you can return to it when you are finished: Bookmark = Form1.Data1.Recordset.Bookmark Next, we want to move to the first record in the Recordset, then loop one at a time through all of them. To do this, you need to know how many records exist. You can obtain this information from the Recordset objects RecordCount property. Before you read this property, however, you must move to the end of the Recordset with the MoveLast method. Why? A Dynaset-type Recordset does not necessarily read all of the tables records into memory at once; until it has actually read the entire table, it will not have an accurate record count. Therefore, you need to force a read to the end of the table with the MoveLast method. Now the RecordCount property will contain an accurate count of the records. After storing that value, you can use MoveFirst to move to the first record: Form1.Data1.Recordset.MoveLast NumRecs = Form1.Data1.Recordset.RecordCount Form1.Data1.Recordset.MoveFirst You are now ready to load the List Box. Using the Clear method firstto be sure the List Box is emptyyou loop through the table, reading the Title field from each record and loading it into the List Box with the AddItem method. Finally, you return to the original record that was saved: List1.Clear For i = 0 To NumRecs - 1 x = Form1.Data1.Recordset.Fields(Title).Value If IsNull(x) Then x = Form1.Data1.Recordset.MoveNext List1.AddItem x Next i Form1.Data1.Recordset.Bookmark = Bookmark Note the complex line of code that actually retrieves the Title data from the current record and stores it in the string variable x. It may be easier to read this line from right to left: The Value of the Field named Title in the Recordset that is associated with the Data control named Data1 on the form named Form1.
How do you go about displaying a record on the main form when the user clicks on its title in the list? My approach is to use the FindFirst method to search for the record. Because the title was retrieved directly from the database table, you know a match will occur when we search for that title. You should place the required code in the List Boxs Click event procedure. When the user selects an item in a List Boxeither by clicking the mouse or using the keyboardthe Click event is generated. Obviously, the program will permit either mouse or keyboard input. The numerical position of the selected item in the list is returned by the ListIndex property. To get the item itself, we use the ListIndex property as the index to the List Boxs List property. The following code places the title from the List Box in a query string: Template = [Title] = & Chr$(34) & List1.List(List1.ListIndex) & Chr$(34) Form1.Data1.Recordset.FindFirst Template Finally, you must code the forms Resize event procedure, which sets the List Box and Command Button sizes to fill the form. We have already reviewed this procedure, so I wont go into detail. The complete code for FRMLIST2.FRM is presented in Listing 19.18. Listing 19.18 Code in FRMLIST2.FRM. Option Explicit Private Sub cmdClose_Click() Unload lstForm End Sub Private Sub Form_Load() Dim x As Variant, NumRecs As Integer Dim i As Integer, Bookmark As String Bookmark = Form1.Data1.Recordset.Bookmark Form1.Data1.Recordset.MoveLast NumRecs = Form1.Data1.Recordset.RecordCount Form1.Data1.Recordset.MoveFirst List1.Clear For i = 0 To NumRecs - 1 x = Form1.Data1.Recordset.Fields(Title).VALUE If IsNull(x) Then x = Form1.Data1.Recordset.MoveNext List1.AddItem x Next i Form1.Data1.Recordset.Bookmark = Bookmark End Sub Private Sub Form_Resize() List1.Left = 0 List1.TOP = 0 List1.Width = lstForm.ScaleWidth List1.Height = lstForm.ScaleHeight * 0.9 cmdClose.TOP = List1.Height + 1 cmdClose.Left = 0 cmdClose.Width = lstForm.ScaleWidth cmdClose.Height = lstForm.ScaleHeight * 0.1 End Sub Private Sub List1_Click() Dim Template As String Set up the template and perform the search. Since we know that there must be a matching title we do not have to provide for the possibility of no match. We use Chr$(34) to enclose the search template in double quotes. This permits the template to contain single quotes, which is not possible if the template itself is enclosed in double quotes. Template = [Title] = & Chr$(34) & List1.List(List1.ListIndex) & Chr$(34) Form1.Data1.Recordset.FindFirst Template End Sub This completes our musical recordings database program. It provides all the functionality of the address list program, but with fewer lines of code. The executing program, with both forms displayed, is shown in Figure 19.11. The ease with which we created this program reflects the power of Visual Basics Data control and the data-aware controls that can be linked to it. Even so, several features that would be desirable in even a simple database program are missing. No error trapping exists, which is perhaps the most serious omission. Error trapping permits a program to deal gracefully with errors, such as a corrupt database file, disk problems, and the like. Likewise, we have no data validation, ensuring that the user does not enter invalid datafor example, a title that is longer than the maximum 40 characters permitted by the table definition. We will delve into these and other important database topics in the following chapters. Of course, this is a simple database program that requires none of the sophisticated and complex features of many commercial applications. Visual Basics database tools provide for these as well, and well see in the next several chapters how to approach the design of a database program with a variety of features that would be required in a real-world project you might create for a paying client.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |