VERSION 5.00 Begin VB.Form frmChangeBook Caption = "Change Book" ClientHeight = 2244 ClientLeft = 2100 ClientTop = 3084 ClientWidth = 6048 LinkTopic = "Form1" ScaleHeight = 2244 ScaleWidth = 6048 Begin VB.CommandButton cmdChangeBook Caption = "Delete" Height = 372 Index = 1 Left = 4800 TabIndex = 9 Top = 720 Width = 1092 End Begin VB.CommandButton cmdChangeBook Caption = "Update" Height = 372 Index = 0 Left = 4800 TabIndex = 8 Top = 240 Width = 1092 End Begin VB.TextBox txtBookID Height = 288 Left = 2040 TabIndex = 1 Top = 240 Width = 492 End Begin VB.CommandButton cmdDone Caption = "Done" Height = 372 Left = 4800 TabIndex = 11 Top = 1680 Width = 1092 End Begin VB.CommandButton cmdViewBook Caption = "View Book" Height = 372 Left = 4800 TabIndex = 10 Top = 1200 Width = 1092 End Begin VB.TextBox txtInStock Height = 288 Left = 1920 TabIndex = 7 Top = 1680 Width = 612 End Begin VB.TextBox txtAuthor Height = 288 Left = 1920 TabIndex = 5 Top = 1200 Width = 2292 End Begin VB.TextBox txtTitle Height = 288 Left = 1680 TabIndex = 3 Top = 720 Width = 2892 End Begin VB.Label lblBookID Caption = "Enter a valid book ID:" Height = 252 Left = 360 TabIndex = 0 Top = 240 Width = 1572 End Begin VB.Label lblInStock Caption = "In Stock:" Height = 252 Left = 1080 TabIndex = 6 Top = 1680 Width = 732 End Begin VB.Label lblAuthor Caption = "Author:" Height = 252 Left = 1080 TabIndex = 4 Top = 1200 Width = 732 End Begin VB.Label lblTitle Caption = "Title:" Height = 252 Left = 1080 TabIndex = 2 Top = 720 Width = 492 End End Attribute VB_Name = "frmChangeBook" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False ' Chapter 12 Change Book ' 3/99 Option Explicit Private Type BookRecord strTitle As String * 30 strAuthor As String * 30 intInStock As Integer End Type Private Sub Form_Load() Call InitializeForm End Sub '***************************************************************** ' Clears text boxes on form and dims Update and Delete buttons ' ' post: Text boxes on form are empty and Update and Delete ' buttons are dimmed '***************************************************************** Sub InitializeForm() txtBookID.Text = "" txtTitle.Text = "" txtAuthor.Text = "" txtInStock.Text = "" cmdChangeBook(0).Enabled = False ' Dim Update button cmdChangeBook(1).Enabled = False ' Dim Delete button End Sub Private Sub txtBookID_Change() txtTitle.Text = "" txtAuthor.Text = "" txtInStock.Text = "" cmdChangeBook(0).Enabled = False ' Dim Update button cmdChangeBook(1).Enabled = False ' Dim Delete button End Sub Private Sub cmdViewBook_Click() Dim udtBookToView As BookRecord Dim intBookFile As Integer, lngRecLength As Long Dim lngTotalRecords As Long, lngBookID As Long ' Open file intBookFile = FreeFile lngRecLength = LenB(udtBookToView) Open "Randbooks.dat" For Random As #intBookFile _ Len = lngRecLength ' Determine number of records lngTotalRecords = NumRecords(intBookFile, lngRecLength) ' View record if valid If lngTotalRecords = 0 Then ' No records in file MsgBox "No records to view" cmdViewBook.Enabled = False ' Dim View Book button Else lngBookID = txtBookID.Text ' Get Book ID If lngBookID > 0 And lngBookID <= lngTotalRecords Then Get #intBookFile, lngBookID, udtBookToView txtTitle.Text = udtBookToView.strTitle txtAuthor.Text = udtBookToView.strAuthor txtInStock.Text = udtBookToView.intInStock cmdChangeBook(0).Enabled = True ' Enable Update button cmdChangeBook(1).Enabled = True ' Enable Delete button Else Call InitializeForm MsgBox "Enter a valid book ID" End If End If ' Close file Close #intBookFile End Sub '*************************************************************** ' Updates or deletes an existing BookRecord record in a file ' ' pre: A valid book ID is displayed in the txtBookID text box ' post: The displayed record is updated or deleted '*************************************************************** Private Sub cmdChangeBook_Click(Index As Integer) Dim udtBookToChange As BookRecord Dim intBookFile As Integer, lngRecLength As Long Dim lngBookID As Long ' Open file intBookFile = FreeFile lngRecLength = LenB(udtBookToChange) Open "Randbooks.dat" For Random As #intBookFile _ Len = lngRecLength lngBookID = txtBookID.Text ' Update record If Index = 0 Then udtBookToChange.strTitle = txtTitle.Text udtBookToChange.strAuthor = txtAuthor.Text udtBookToChange.intInStock = txtInStock.Text Put #intBookFile, lngBookID, udtBookToChange ' Delete record ElseIf Index = 1 Then udtBookToChange.strTitle = "DELETED" udtBookToChange.strAuthor = "" udtBookToChange.intInStock = 0 Put #intBookFile, lngBookID, udtBookToChange End If ' Reset form Call InitializeForm ' Close file Close #intBookFile End Sub '****************************************************************** ' Returns the number of records in the file with intFileNum file ' number containing records of length lngRecLength ' ' pre: intFileNum is open ' post: Number of records in intFileNum returned '****************************************************************** Function NumRecords(ByVal intFileNum As Integer, _ ByVal lngRecLength As Long) As Integer If LOF(intFileNum) Mod lngRecLength = 0 Then NumRecords = (LOF(intFileNum) \ lngRecLength) Else NumRecords = (LOF(intFileNum) \ lngRecLength) + 1 End If End Function Private Sub cmdDone_Click() Unload Me End Sub