Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic I/O Programs (ProjRead1, ProjRead2, ProjWrite1, ProjPay) Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Visual Basic I/O Programs (ProjRead1, ProjRead2, ProjWrite1, ProjPay) Please use speaker notes for additional information!"— Presentation transcript:

1 Visual Basic I/O Programs (ProjRead1, ProjRead2, ProjWrite1, ProjPay) Please use speaker notes for additional information!

2 ProjRead1

3 Private Sub Form_Load() Open "A:\StudentA" For Input As #1 End Sub When the form is loaded, the StudentA file will be opened as Input (meaning it can be read). It is opened as #1. If a second file was opened, it could be opened as #2.

4 ProjRead1 Private Sub cmdRead_Click() Dim IdNo As String, Name As String, Major As String, GPA As String, NumCr As String Dim Ans As String Input #1, IdNo, Name, Major, GPA, NumCr If IdNo = "999" Then Rem Ans = MsgBox("End of File Reached", vbOKOnly, "EOF") MsgBox "End of File Reached", vbOKOnly, "EOF" Rem END and Close here terminate the program Close #1 End Else txtIdNo.Text = IdNo txtName.Text = Name txtMajor.Text = Major txtGPA.Text = GPA txtNumCr.Text = NumCr End If End Sub In cmdRead, I define the fields on the file. In the example, I defined them as all string. Since the last two fields were numeric, I could also have defined that as Single and/or Integer as shown below. Dim IdNo As String, Name As String, Major As String, GPA As Single, NumCr As Integer Input #1 relates to the file that was opened For Input as #1. This statement refers to the fields by the names that were defined in the Dim statements above. IdNo is checked to see if it is 999. If it is, processing will end. If IdNo is not 999, the data from the input record is moved to the text boxes on the form.

5 The results of clicking Read are shown above. Only the first three records are shown. After the last record has been read, I pressed Read again. The record with Idno = 999 was read and this indicated that end of file had been reached. The message was displayed.

6 ProjRead2 Note that because I am testing for EOF(1) instead of IdNo=999, the 999 record will show. When I press Read with the 999 record on the screen, the EOF message is displayed. IF EOF(1) tests to see if end of file has been reached on the file opened as #1.

7 ProjWrite1

8 Private Sub Form_Load() Open "A:\PayX" For Output As #1 End Sub When the form is loaded, a file is opened for Output called PayX. The file is opened as #1 since it is the only file being used in the code. Private Sub cmdWrite_Click() Write #1, txtName.Text, txtPayHour.Text, txtHours.Text, txtExempt.Text, txtMarital.Text, txtPriorPay.Text End Sub When the Write button is clicked, the write command writes the data that was inputted into the text boxes on the form. Private Sub cmdExit_Click() Close #1 End End Sub When the Exit button is clicked, file #1 is closed and processing is ended. Private Sub cmdClear_Click() txtName.Text = "" txtPayHour.Text = "" txtHours.Text = "" txtExempt.Text = "" txtMarital.Text = "" txtPriorPay.Text = "" txtName.SetFocus End Sub When the Clear button is clicked, the data in the text boxes on the form is cleared out and the fields are set to null values with “”.

9 ProjWrite1

10 ProjPay When the form is loaded, not data is displayed. Clicking the Read button will display the record on the screen.

11 ProjPay

12 The last record has been displayed and the Read button is clicked. At that point the EOF message box is displayed.


Download ppt "Visual Basic I/O Programs (ProjRead1, ProjRead2, ProjWrite1, ProjPay) Please use speaker notes for additional information!"

Similar presentations


Ads by Google