Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Starting out with Visual Basic 2012 6th edition The directions for the project area also in your book as Programming Challenges 1, 2, and 10

Starting out with Visual Basic 2012 6th edition

The directions for the project area also in your book as Programming Challenges 1, 2, and 10 of Chapter 9.

I have done the first two parts but i cant seem to get the third part.

Employee Data, Part 3

Create an application that performs the following operations with the employee file created by the application in Programming Challenge 1: Uses an Open dialog box to allow the user to select the file Allows the user to enter a new employee record and then saves the record to the file Allows the user to enter an employee number and searches for a record contain-ing that employee number. If the record is found, the record is displayed. Displays all records, one after the other (this means all records in one listbox) Prints an employee record Equip your application with either a menu system or a set of buttons to perform these operations.

The code I have is.

Form1

Imports System.IO

Public Class Form1

Public Property InputFile As StreamReader Public Property pdPrint As Object Public Property ofdOpenFile As Object

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub Function OpenInputFile() As Boolean 'the status flg to false Dim blnStatus As Boolean = False ofdOpenFile.Filter = "Text Files (*.txt)|*.txt|All files(*.*)|*.*" If ofdOpenFile.ShowDialog = Windows.Forms.DialogResult.Ok Then 'set the file name g_strFilename = ofdOpenFile.FileName 'open the file InputFile = File.OpenText(g_strFilename) 'set teh status flag to true blnStatus = True End If 'return status Return blnStatus End Function 'the readNextRecord procedure reads and displays data from the file Sub ReadNextRecord() 'read the next record and display it lblFirstName.Text = InputFile.ReadLine lblMiddleName.Text = InputFile.ReadLine lblLastName.Text = InputFile.ReadLine lblEmployeeNum.Text = InputFile.ReadLine lblDept.Text = InputFile.ReadLine lblTelephone.Text = InputFile.ReadLine lblExtension.Text = InputFile.ReadLine lblEmail.Text = InputFile.ReadLine End Sub 'the FindRecordByNum procedure searches the file for customer data based on the employee number Sub FindRecordByNum(ByVal searchNum As String) Dim blnFound As Boolean = False 'found flag set to false 'determine if the file has a name If g_strFilename = String.Empty Then 'Display message to user that no file is open MessageBox.Show("No file is open", "Error") Else 'close the file InputFile.Close() If File.Exists(g_strFilename) Then 'open the file InputFile = System.IO.File.OpenText(g_strFilename) 'search until the end of file or a match is found Do Until (InputFile.Peek = -1) Or blnFound 'read the next record ReadNextRecord() 'is match If searchNum.ToUpper = lblEmployeeNum.Text.ToUpper Then blnFound = True 'set the found flag to true End If Loop 'the record was found If blnFound Then 'display the customer record ReadNextRecord() Else 'display a message record was not found MessageBox.Show(searchNum & "was not found", "Record Not Found") End If Else 'display a message that there was an error opening the file MessageBox.Show("Cannot open file", "Error") End If End If End Sub

Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) 'print the header e.Graphics.DrawString("Employee Record." & Now.ToString, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 10) 'print the date and time e.Graphics.DrawString("Date and Time." & Now.ToString, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 38) 'print the employee data e.Graphics.DrawString("Last Name:" & lblLastName.Text, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 80) e.Graphics.DrawString("First Name:" & lblFirstName.Text, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 94) e.Graphics.DrawString("Middle Name:" & lblMiddleName.Text, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 108) e.Graphics.DrawString("Employee Number:" & lblEmployeeNum.Text, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 122) e.Graphics.DrawString("Department:" & lblDept.Text, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 136) e.Graphics.DrawString("Telephone:" & lblTelephone.Text, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 150) e.Graphics.DrawString("Extension:" & lblExtension.Text, New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 10, 178) End Sub

Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click 'open the file If OpenInputFile() Then 'read the record from the file and display it. If InputFile.Peek <> -1 Then ReadNextRecord() Else 'display a message that no records exist MessageBox.Show("No records in file.") End If End If End Sub

Private Sub mnuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click pdPrint.Print() End Sub

Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click 'close the form Me.Close() End Sub

Private Sub mnueditAddRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnueditAddRecord.Click Dim frmAdd As New AddForm 'create an instance of the AddForm 'determine if the file has a name If Not g_strFilename = String.Empty Then 'close the file InputFile.Close() 'display the add form AddForm.ShowDialog() 'determine if the file has a name If File.Exists(g_strFilename) Then 'reopen the file, if one was open InputFile = File.OpenText(g_strFilename) 'read the record from the file and display it ReadNextRecord() Else 'display a message that the does not exist MessageBox.Show("The file " & g_strFilename & "does not exist.", "Error") End If End If End Sub

Private Sub mnuSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSearch.Click 'search for record by employee number 'the employee number initialized to an empty string Dim searchNum As String = String.Empty 'get the employee number from the user searchNum = InputBox("Enter the employee number to search for", "Search By Employyee Number") 'determine that the employee number is not an empty string If Not searchNum = String.Empty Then FindRecordByNum(searchNum.Trim) End If End Sub

Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click 'displsy an about window MessageBox.Show("Employee Data Application version 3", "About") End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click 'determine if the file contains a value If Not g_strFilename = String.Empty Then 'determine if there is data in the file If InputFile.Peek <> -1 Then 'display the employee data record ReadNextRecord() Else 'display a message that no more recors exist MessageBox.Show("No more records.") End If End If End Sub End Class

for form 2

Imports System.IO

Public Class AddForm Public ofdOpenFile As Object Public outputFile As StreamWriter

Private Sub AddForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'make sure a file has been opened and it exits If g_strFilename = String.Empty Then 'set the Save File Dialog filter ofdOpenFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 'get the file name If ofdOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then g_strFilename = ofdOpenFile.FileName 'determine if the file exists If Not File.Exists(g_strFilename) Then

'open the file to create it outputFile = System.IO.File.AppendText(g_strFilename) 'now close the file it will be reopened when the save is preformed outputFile.Close() End If Else 'set the file name to an empty string g_strFilename = String.Empty 'close the form Me.Close() End If ElseIf Not File.Exists(g_strFilename) Then 'display a message indicating that the file does not exist MessageBox.Show("The file" & g_strFilename & "does not exist", "Error") End If End Sub Sub SaveRecord() 'write the currently displayed data to the file outputFile.WriteLine(txtFirstName.Text) outputFile.WriteLine(txtMiddleName.Text) outputFile.WriteLine(txtLastName.Text) outputFile.WriteLine(txtEmployeeNum.Text) outputFile.WriteLine(cboDept.Text) outputFile.WriteLine(txtTelephone.Text) outputFile.WriteLine(txtExtension.Text) outputFile.WriteLine(txtEmail.Text) End Sub 'the ClearForm procedure clears the text boxes Sub ClearForm() txtFirstName.Text = String.Empty txtMiddleName.Text = String.Empty txtLastName.Text = String.Empty txtEmployeeNum.Text = String.Empty cboDept.Text = String.Empty txtTelephone.Text = String.Empty txtExtension.Text = String.Empty txtEmail.Text = String.Empty 'give the focus to the txtFirstName text box txtFirstName.Focus() End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 'open the file outputFile = File.AppendText(g_strFilename) 'save the record SaveRecord() 'display message that the record was saved MessageBox.Show("Record saved.") 'clear the Text on the AddForm ClearForm() 'close the file outputFile.Close() End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click 'close the AddForm Me.Close() End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 'clear the form ClearForm() End Sub End Class

and Module EmployeeDataModule

Module EmployeeDataModule 'global variables Public g_strFilename As String End Module

This all comes from the solutions pages for this box.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago