VERSION 5.00 Begin VB.Form frmChangeBookRecord Caption = "Change Book Record" ClientHeight = 1920 ClientLeft = 2448 ClientTop = 2736 ClientWidth = 5508 LinkTopic = "Form1" ScaleHeight = 1920 ScaleWidth = 5508 Begin VB.CommandButton cmdChangeBook Caption = "Delete" Height = 372 Index = 1 Left = 4200 TabIndex = 7 Top = 840 Width = 1092 End Begin VB.ComboBox cboTitle Height = 288 Left = 960 Sorted = -1 'True TabIndex = 1 Top = 360 Width = 2772 End Begin VB.CommandButton cmdDone Caption = "Done" Height = 372 Left = 4200 TabIndex = 8 Top = 1320 Width = 1092 End Begin VB.CommandButton cmdChangeBook Caption = "Update" Height = 372 Index = 0 Left = 4200 TabIndex = 6 Top = 360 Width = 1092 End Begin VB.TextBox txtInStock Height = 288 Left = 1200 TabIndex = 5 Top = 1320 Width = 612 End Begin VB.TextBox txtAuthor Height = 288 Left = 1200 TabIndex = 3 Top = 840 Width = 2292 End Begin VB.Label lblInStock Caption = "In Stock:" Height = 252 Left = 360 TabIndex = 4 Top = 1320 Width = 732 End Begin VB.Label lblAuthor Caption = "Author:" Height = 252 Left = 360 TabIndex = 2 Top = 840 Width = 732 End Begin VB.Label lblTitle Caption = "Title:" Height = 252 Left = 360 TabIndex = 0 Top = 360 Width = 492 End End Attribute VB_Name = "frmChangeBookRecord" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False ' Chapter 11 Change Book Record ' 3/99 Option Explicit Private Sub Form_Load() Call RefreshInterface End Sub Private Sub cboTitle_Click() Dim intBookFile As Integer Dim strTitle As String, strAuthor As String, intInStock As Integer ' Open file intBookFile = FreeFile Open "Books.txt" For Input As #intBookFile ' Display record Do While Not EOF(intBookFile) ' Read record Input #intBookFile, strTitle, strAuthor, intInStock If strTitle = cboTitle.Text Then txtAuthor.Text = strAuthor txtInStock.Text = intInStock End If Loop ' Close file Close #intBookFile End Sub Private Sub cmdChangeBook_Click(Index As Integer) Dim intBookFile As Integer, intNewBookFile As Integer Dim strTitle As String, strAuthor As String, intInStock As Integer If cboTitle.Text <> "" Then ' Open original file intBookFile = FreeFile Open "Books.txt" For Input As #intBookFile ' Open new file intNewBookFile = FreeFile Open "Tempbooks.txt" For Output As #intNewBookFile ' Process records Do While Not EOF(intBookFile) ' Read record from original file Input #intBookFile, strTitle, strAuthor, intInStock If Index = 0 Then ' update stock If strTitle = cboTitle.Text Then intInStock = txtInStock.Text End If Write #intNewBookFile, strTitle, strAuthor, intInStock ElseIf Index = 1 Then ' delete book ' Write record to new file if not displayed record If Not strTitle = cboTitle.Text Then Write #intNewBookFile, strTitle, strAuthor, intInStock End If End If Loop ' Close files Close #intBookFile Close #intNewBookFile ' Delete original file and rename new file Kill "Books.txt" Name "Tempbooks.txt" As "Books.txt" Call RefreshInterface End If End Sub '********************************************************************** ' Clears the text boxes and current combo box list and ' adds an updated list of titles ' ' pre: Books.txt has records with Title, Author, and In Stock fields ' post: List of titles displayed in combo box '********************************************************************** Sub RefreshInterface() Dim intBookFile As Integer Dim strTitle As String, strAuthor As String, intInStock As Integer txtAuthor.Text = "" ' Clear Author text box txtInStock.Text = "" ' Clear InStock text box ' Open file intBookFile = FreeFile Open "Books.txt" For Input As #intBookFile ' Add titles to combo box cboTitle.Clear Do While Not EOF(intBookFile) ' Read record Input #intBookFile, strTitle, strAuthor, intInStock ' Add title to combo box cboTitle.AddItem strTitle Loop ' Close file Close #intBookFile End Sub Private Sub cmdDone_Click() Unload Me End Sub