VERSION 5.00 Begin VB.Form frmViewBook Caption = "View Book" ClientHeight = 2244 ClientLeft = 1764 ClientTop = 2736 ClientWidth = 6048 LinkTopic = "Form1" ScaleHeight = 2244 ScaleWidth = 6048 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 = 9 Top = 1680 Width = 1092 End Begin VB.CommandButton cmdViewBook Caption = "View Book" Height = 372 Left = 4800 TabIndex = 8 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 = "frmViewBook" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False ' Chapter 12 View 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 form ' ' post: Book record text boxes on form are empty '************************************************************* Sub InitializeForm() txtBookID.Text = "" txtTitle.Text = "" txtAuthor.Text = "" txtInStock.Text = "" End Sub Private Sub txtBookID_Change() txtTitle.Text = "" txtAuthor.Text = "" txtInStock.Text = "" 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 Else Call InitializeForm MsgBox "Enter a valid book ID" End If End If ' 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