VERSION 5.00 Begin VB.Form frmAddBook Caption = "Add Book" ClientHeight = 2244 ClientLeft = 2100 ClientTop = 3084 ClientWidth = 5604 LinkTopic = "Form1" ScaleHeight = 2244 ScaleWidth = 5604 Begin VB.TextBox txtBookID Enabled = 0 'False Height = 288 Left = 1200 TabIndex = 1 Top = 240 Width = 492 End Begin VB.CommandButton cmdDone Caption = "Done" Height = 372 Left = 4200 TabIndex = 9 Top = 1680 Width = 1092 End Begin VB.CommandButton cmdAddBook Caption = "Add Book" Height = 372 Left = 4200 TabIndex = 8 Top = 1200 Width = 1092 End Begin VB.TextBox txtInStock Height = 288 Left = 1200 TabIndex = 7 Top = 1680 Width = 612 End Begin VB.TextBox txtAuthor Height = 288 Left = 1200 TabIndex = 5 Top = 1200 Width = 2292 End Begin VB.TextBox txtTitle Height = 288 Left = 960 TabIndex = 3 Top = 720 Width = 2892 End Begin VB.Label lblBookID Caption = "Book ID:" Height = 252 Left = 360 TabIndex = 0 Top = 240 Width = 732 End Begin VB.Label lblInStock Caption = "In Stock:" Height = 252 Left = 360 TabIndex = 6 Top = 1680 Width = 732 End Begin VB.Label lblAuthor Caption = "Author:" Height = 252 Left = 360 TabIndex = 4 Top = 1200 Width = 732 End Begin VB.Label lblTitle Caption = "Title:" Height = 252 Left = 360 TabIndex = 2 Top = 720 Width = 492 End End Attribute VB_Name = "frmAddBook" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False ' Chapter 12 Add 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() Dim udtBook As BookRecord Dim intBookFile As Integer, lngRecLength As Long, lngNextBookID As Long ' Open file intBookFile = FreeFile lngRecLength = LenB(udtBook) Open "Randbooks.dat" For Random As #intBookFile Len = lngRecLength ' Determine next record number lngNextBookID = NumRecords(intBookFile, lngRecLength) + 1 txtBookID.Text = lngNextBookID ' Display next book ID txtBookID.Enabled = False ' User not allowed to change ID ' Close file Close #intBookFile End Sub '****************************************************************** ' Adds a BookRecord record to a file ' ' pre: The book ID for the next record is displayed ' post: A new book record has been added to a file and the ' next book ID displayed on the form '****************************************************************** Private Sub cmdAddBook_Click() Dim udtNewBook As BookRecord Dim intBookFile As Integer, lngRecLength As Long, lngBookID As Long ' Open file intBookFile = FreeFile lngRecLength = LenB(udtNewBook) Open "Randbooks.dat" For Random As #intBookFile Len = lngRecLength ' Add record lngBookID = txtBookID.Text udtNewBook.strTitle = txtTitle.Text udtNewBook.strAuthor = txtAuthor.Text udtNewBook.intInStock = txtInStock.Text Put #intBookFile, lngBookID, udtNewBook ' Reset text boxes txtBookID.Text = lngBookID + 1 txtTitle.Text = "" txtAuthor.Text = "" txtInStock.Text = "" ' 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