Presentation is loading. Please wait.

Presentation is loading. Please wait.

Random Files Please see speaker notes for additional information!

Similar presentations


Presentation on theme: "Random Files Please see speaker notes for additional information!"— Presentation transcript:

1 Random Files Please see speaker notes for additional information!

2 Random Gives a specified length to a string field. Defines a record. This says: modRInvR(modRInvR.bas) We have created a type declaration that we have stored in a BAS module.

3 Random To create a module, go to Project and then Add Module. Existing modules in the random directory.

4 Random

5 Dim fileOut As String Dim recordNum As Integer Dim invenRec As randomInvenRec Option Explicit Private Sub cmdClear_Click() txtItemNo.Text = "" txtItemName.Text = "" txtOnHand.Text = "" txtOnOrder.Text = "" txtReorderPt.Text = "" txtCost.Text = "" txtPrice.Text = "" txtItemNo.SetFocus End Sub Private Sub cmdExit_Click() Close #1 End End Sub Private Sub cmdWrite_Click() invenRec.itemno = txtItemNo.Text invenRec.itemname = txtItemName.Text invenRec.onhand = Val(txtOnHand.Text) invenRec.onorder = Val(txtOnOrder.Text) invenRec.reorderpt = Val(txtReorderPt.Text) invenRec.cost = Val(txtCost.Text) invenRec.price = Val(txtPrice.Text) recordNum = recordNum + 1 Put #1, recordNum, invenRec txtItemNo.SetFocus End Sub Private Sub Form_Load() fileOut = App.Path & "\Invenoutx.txt" Open fileOut For Random As #1 Len = Len(invenRec) End Sub This opens the file as a random output file. The length is specified by saying the length of the description in the BAS module. Here I dim invenRec as of the type randomInvenRec. The type I created when I defined the record in the BAS Module. Public Type randomInvenRec itemno As String * 5 itemname As String * 20 onhand As Integer onorder As Integer reorderpt As Integer cost As Single price As Single End Type

6 Random I keyed in 3 and clicked Retrieve. Record 3 was retrieved.

7 Random Option Explicit Dim fileOut As String Dim recordNum As Integer Dim invenRec As randomInvenRec Dim ErrorString As String Private Sub cmdClear_Click() txtRecRet.Text = "" txtItemNo.Text = "" txtItemName.Text = "" txtOnHand.Text = "" txtOnOrder.Text = "" txtReorderPt.Text = "" txtCost.Text = "" txtPrice.Text = "" txtRecRet.SetFocus End Sub Private Sub cmdExit_Click() Close #1 End End Sub The record number is the record number used on the record and invenRec is of the Type randomInvenRec.

8 Random Private Sub cmdRetrieve_Click() recordNum = Val(txtRecRet.Text) Get #1, recordNum, invenRec If Not EOF(1) Then txtItemNo.Text = invenRec.itemno txtItemName.Text = invenRec.itemname txtOnHand.Text = invenRec.onhand txtOnOrder.Text = invenRec.onorder txtReorderPt.Text = invenRec.reorderpt txtCost.Text = Format(invenRec.cost, "Currency") txtPrice.Text = Format(invenRec.price, "Currency") Else MsgBox "Record # is too big!" txtRecRet.Text = "" End If txtRecRet.SetFocus End Sub Private Sub Form_Load() fileOut = App.Path & "\Invenoutx.txt" Open fileOut For Random As #1 Len = Len(invenRec) End Sub The record number that is keyed in on the form is assigned to recordNum. Then the Get statement is issued to get the record with the matching recordNum. IF EOF is reached, the record is not on the file. The record number is past the record numbers of the records in the file. The Random file is opened and the length is given. Record name.field name

9 Random

10 I retrieved record 3 and changed on order to 15 (it used to be 5). After I keyed in the change, I clicked the Change button. I then retrieved record 3 again and it brought back the record with the change.

11 Random Private Sub cmdChange_Click() invenRec.itemno = txtItemNo.Text invenRec.itemname = txtItemName.Text invenRec.onhand = Val(txtOnHand.Text) invenRec.onorder = Val(txtOnOrder.Text) invenRec.reorderpt = Val(txtReorderPt.Text) invenRec.cost = Val(txtCost.Text) invenRec.price = Val(txtPrice.Text) Put #1, recordNum, invenRec cmdClear.SetFocus End Sub This is the change routine. The values in the fields are assigned to the values on the record. Then the PUT statement writes the record with its record number out on the file.

12 Random I entered in the data for this record and then pressed Add. Note that I did not enter the record number. This will be generated by the code. When I put in 8 under record to update and clicked retrieve, the record came back up.

13 Random Private Sub cmdAdd_Click() invenRec.itemno = txtItemNo.Text invenRec.itemname = txtItemName.Text invenRec.onhand = Val(txtOnHand.Text) invenRec.onorder = Val(txtOnOrder.Text) invenRec.reorderpt = Val(txtReorderPt.Text) invenRec.cost = Val(txtCost.Text) invenRec.price = Val(txtPrice.Text) If delSub = 0 Then lastRec = lastRec + 1 recordNum = lastRec Put #1, recordNum, invenRec Else recordNum = delArray(delSub) Put #1, recordNum, invenRec delSub = delSub - 1 End If cmdClear.SetFocus End Sub This takes the keyed in information and moves it to fields on the record. I keep track of deleted records in an array controlled by delSub. If no records are deleted then the record number becomes the last record written + 1. If there are deleted records in the array then the last record number put in the array provides the record number for this record. Note that since it is used, delSub is decreased by 1.

14 Random I retrieved record 6 and then clicked Delete. I then tried to retrieve record 6 and was told that the record had been deleted.

15 Random Private Sub cmdDelete_Click() invenRec.itemno = " " invenRec.itemname = "Deleted" invenRec.onhand = 0 invenRec.onorder = 0 invenRec.reorderpt = 0 invenRec.cost = 0 invenRec.price = 0 Put #1, recordNum, invenRec delSub = delSub + 1 delArray(delSub) = recordNum cmdClear.SetFocus End Sub The information on the record that is being deleted is resent to spaces and 0 and the name field is set to Deleted. The record on the file is written over with this cleared information. The delSub is then incremented by 1 and the record number of the deleted record is written to the array. Dim delArray(1 To 100) As Integer Dim delSub As Integer This array was defined in the general/declarations area of the program.

16 Random This record is the ninth record that I keyed in. However I deleted record 6 prior to keying this record in. Therefore because of the delete array, this record was assigned a record number of 6.

17 Random Added record. Next available record number is 9 since the only record I delete was record 6 and that number has already been used.

18 Random Option Explicit Dim fileIO As String Dim fileDel As String Dim recordNum As Integer Dim recordNumOfDel As Integer Dim invenRec As randomInvenRec Dim delRec As randomDel Dim delArray(1 To 100) As Integer Dim delSub As Integer Dim ErrorString As String Dim lastRec As Integer These two descriptions refer to the types defined in the BAS modules.

19 Random Private Sub Form_Load() Dim lenFile fileIO = App.Path & "\Invenoutx.txt" Open fileIO For Random Access Read Write As #1 Len = Len(invenRec) lenFile = LOF(1) lastRec = lenFile / Len(invenRec) fileDel = App.Path & "\InvenDelx.txt" Open fileDel For Random Access Read As #2 Len = Len(delRec) delSub = 0 Get #2, 1, delRec If Not EOF(2) Then Do delSub = delSub + 1 delArray(delSub) = Val(delRec.recnoDel) Get #2, delSub + 1, delRec Loop Until EOF(2) End If Close #2 End Sub The form load code opens the data file for Random Access Read Write and gives a length of the invenRec which is of type randomInvenRec. It then uses LOF(1) to find the length of the file and divides this by the length of the record to get the lastRec which is the record number of the last record. The form load code then opens the file that contains the record numbers of the deleted records. It then attempts to read a record. If it is successful (in other words EOF(2) is not true, then a DO loop is performed that will fill the delArray with the record numbers from the file. The name of the field and record.

20 Random Private Sub cmdExit_Click() Close #1 Kill fileDel Open fileDel For Random Access Write As #2 Len = Len(delRec) If delSub > 0 Then For recordNumOfDel = 1 To delSub delRec.recnoDel = Str(delArray(recordNumOfDel)) Put #2, recordNumOfDel, delRec Next recordNumOfDel End If Close #2 End End Sub The file called fileDel is killed which means that it no longer exists. The file is then opened to Write. If there are records in the array, these records are written to the file one at a time using the FOR loop that accomplishes the PUT. The regular data file is closed.

21 Random Private Sub cmdRetrieve_Click() recordNum = Val(txtRecRet.Text) Get #1, recordNum, invenRec If Not EOF(1) Then txtItemNo.Text = invenRec.itemno txtItemName.Text = invenRec.itemname txtOnHand.Text = invenRec.onhand txtOnOrder.Text = invenRec.onorder txtReorderPt.Text = invenRec.reorderpt txtCost.Text = invenRec.cost txtPrice.Text = invenRec.price Else MsgBox "Record # is too big!" txtRecRet.Text = "" End If txtRecRet.SetFocus End Sub The keyed in record number is moved to recordNum and then the get is done using recordNum and the name of the data record. If the read is successful, in other words it is not EOF, the the data on the record is transferred to the screen. If the record number is past EOF, then a message box is displayed.

22 Random Private Sub cmdClear_Click() txtRecRet.Text = "" txtItemNo.Text = "" txtItemName.Text = "" txtOnHand.Text = "" txtOnOrder.Text = "" txtReorderPt.Text = "" txtCost.Text = "" txtPrice.Text = "" txtRecRet.SetFocus End Sub Clears the boxes on the screen to null.


Download ppt "Random Files Please see speaker notes for additional information!"

Similar presentations


Ads by Google