19 04 SC6AK4KJ2HQ5LRGBZOT3HDOU33SWXPLBAEILVPQ




Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Database Basics
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Initializing The Program
Program initialization steps are carried out in the Form_Load event procedure, shown in Listing 19.7. The first step is to generate the full path of the data file, using the Path property of the App object to obtain the program directory. We then add a backslash and the data file name, which is contained in the constant FILENAME. Next, the file is opened specifying RANDOM mode and a record length equal to the length of the user-defined type Address, obtained by passing an instance of type Address to the Len function. You can obtain the number of records in the file by dividing the file length by the record length.
If no records are in the file—in other words, if we are starting a new database—a message box asks whether the user wants to start entering addresses. If the reply is No, the program terminates. Otherwise, the EnteringNew flag is set, the form is displayed, and the AddNewAddress procedure is called to permit entry of the first record. If the file is not empty, the form and the first record’s data are displayed.
Listing 19.7 The Form_Load event procedure.


Private Sub Form_Load()

Dim Reply As Integer

‘ Generate the full data file path and name.
‘ We store it in the application directory.
FullFileName = App.Path & “\” & FILENAME
FileNum = FreeFile

‘ Open the data file and calculate the number of records in it.
Open FullFileName For Random As #FileNum Len = Len(CR)
NumberOfRecords = (LOF(FileNum) / Len(CR))

‘ If the file is empty (just created).
If NumberOfRecords = 0 Then
Reply = MsgBox(“New file—start entering addresses?”, vbYesNo, _
“New file”)
If Reply = vbYes Then
EnteringNew = True
Form1.Show
Call AddNewAddress
Else
Close (FileNum)
End
End If
Else ‘ If the file is not empty, display the first record.
CurrentRecord = 1
EnteringNew = False
Call DisplayRecord(CurrentRecord)
End If

End Sub


Adding an address involves calling the AddNewAddress procedure, which is shown in Listing 19.8. New records are always added at the end of the file. The procedure starts by incrementing the variable NumberOfRecords by one, then setting CurrentRecord to point at this new, blank record. The previous value of CurrentRecord is saved, so we can revert to it should the user cancel entry of the new address. The two Command Button captions are changed (as discussed earlier), the six Text Boxes are cleared, and the focus is set to the LastName Text Box in preparation for data entry.
Listing 19.8 The AddNewAddress procedure.


Public Sub AddNewAddress()

‘ Add a new address to the database.

‘ Add new record at the end of the file.
NumberOfRecords = NumberOfRecords + 1

‘ Remember the previous current record number.
OldRecord = CurrentRecord

‘ Set current record pointer to slot for new record.
CurrentRecord = NumberOfRecords

‘ Change captions on two of the command buttons.
cmdAction(0).Caption = “Save”
cmdAction(1).Caption = “Cancel”

‘ Erase contents of the text boxes.
txtFName.Text = “”
txtLName.Text = “”
txtAddress.Text = “”
txtCity.Text = “”
txtState.Text = “”
txtZip.Text = “”

‘ Set the focus to the Last Name text box.
txtLName.SetFocus

End Sub


After entering the address data in the Text Boxes, the user can click on Save, to save the new address, or Cancel, to delete. Clicking on Save calls the procedure SaveCurrentRecord to save the new record, then resets the Command Button captions and EnteringNew flag. The code for the SaveCurrentRecord procedure, shown in Listing 19.9, copies the data from the Text Boxes to the CR structure, then uses the Put statement to save CR to the file. Remember, the variable CurrentRecord has already been set to point at the new record at the end of the file.
Listing 19.9 The SaveCurrentRecord procedure.


Public Sub SaveCurrentRecord()

‘ Saves the current record.

CR.LName = txtLName.Text
CR.FName = txtFName.Text
CR.Address = txtAddress.Text
CR.City = txtCity.Text
CR.State = txtState.Text
CR.Zip = txtZip.Text

Put #FileNum, CurrentRecord, CR

End Sub


The DisplayRecord procedure handles displaying a record. Passed the desired record number as its one argument, this procedure reads the specified record from the file into the structure CR, then copies the individual fields from CR into the six Text Boxes on the form. This code is shown in Listing 19.10.
Listing 19.10 The DisplayRecord procedure.


Public Sub DisplayRecord(Record As Integer)

‘ Reads the specified record from the file and displays it.
Get #FileNum, Record, CR

txtLName.Text = CR.LName
txtFName.Text = CR.FName
txtAddress.Text = CR.Address
txtCity.Text = CR.City
txtState.Text = CR.State
txtZip.Text = CR.Zip

End Sub


The last procedure in the program’s main form is concerned with navigating the address records in response to clicks of the lower set of Command Buttons. The Click event procedure for this control array is presented in Listing 19.11. The code should be easy to understand; it sets the CurrentRecord variable to point at the desired record, then calls DisplayRecord to load and display that record.
Listing 19.11 The Navigation Button’s Click event procedure.


Private Sub cmdMove_Click(Index As Integer)

‘ For the control array of “movement” buttons.

‘ If we’re entering a new record we don’t want these
‘ buttons to work.
If EnteringNew Then Exit Sub

‘ Save the current record in case it has been edited.
Call SaveCurrentRecord

Select Case Index
Case 0 ‘ First record.
CurrentRecord = 1
Case 1 ‘ Previous record.
CurrentRecord = CurrentRecord - 1
If CurrentRecord < 1 Then
CurrentRecord = 1
MsgBox (“Already at first record!”)
End If
Case 2 ‘ Next record.
CurrentRecord = CurrentRecord + 1
If CurrentRecord > NumberOfRecords Then
CurrentRecord = NumberOfRecords
MsgBox (“Already at last record!”)
End If
Case 3 ‘ Last record.
CurrentRecord = NumberOfRecords
End Select

Call DisplayRecord(CurrentRecord)

End Sub


The List Form
We’ve already designed the form that will display the alphabetical list of addresses. What about its code? When the form is activated by the Show method, it is loaded and displayed just like a program’s startup form at the beginning of program execution. We’ll use its Load event procedure to perform the form’s main task: reading all of the address records from the disk file and placing them in the List Box. This procedure will be executed when the Show method is used in the program’s main form to display the list form. This simple code is shown in Listing 19.12. We set up a loop that will start at record one and progress to the last record. Each record is read into the CR structure; then the various parts of the address, starting with the last name, are concentrated into a temporary string variable before being loaded into the List Box with the AddItem method. Because we set the List Box’s Sorted property to True, the List Box will automatically sort the items.



Previous
Table of Contents
Next






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.



Wyszukiwarka