Using Visual Basic, write a program that will allow the user to enter the follow employee data: First Name, Middle Name, Last Name, Employee Number, Department, Telephone Number, Telephone Extension, and Email Address. The valid selections for department are Accounting, Administration, Marketing and MIS, and Sales. Once the data has been entered, the user should be able to save it to a sequential file of type LSV (Line Separated Values). The following figure shows the form.
Suggested Control Names and Attributes:
| Name Property | Text Property | Container | Control Type | Notes |
| frmEmployeeDataPart1 | Employee Data | N/A | Form | Holds Controls |
| grpEmployeeData | Enter Employee Data | frmEmployeeDataPart1 | GroupBox | Contains controls for input |
| txtFirstName | grpEmployeeData | TextBox | Captures first name | |
| txtMiddleName | grpEmployeeData | TextBox | Captures middle name | |
| txtLastName | grpEmployeeData | TextBox | Captures last name | |
| txtEmployeeNumber | grpEmployeeData | TextBox | Captures employee number | |
| cboDept | grpEmployeeData | ComboBox | Select department * | |
| txtTelephone | grpEmployeeData | TextBox | Captures phone number | |
| txtExtension | grpEmployeeData | TextBox | Captures phone extension | |
| txtEmail | grpEmployeeData | TextBox | Captures email address | |
| btnSave | &Save and Record | frmEmployeeDataPart1 | Button | Triggers event to save record |
| btnClear | C&lear | frmEmployeeDataPart1 | Button | Triggers event to clear form |
| btnExit | E&xit | frmEmployeeDataPart1 | Button | Triggers event to exit program |
* Set the DropDownStyle property to DropDownList.
Hints:
- Use the name Employees.txt for your text file.
- When you create the text file, use only the file name and not the folder. Visual Basic assumes that the file location is in the bin/Debug folder.
- Note that all of the controls for input are contained in a GroupBox. The buttons are not.
- The input for departments should be a DropDownList with style ComboBox. This means the user must choose an option and not type in a response.
- After each record has been input, the form needs to be cleared before entering the next record by pressing the “Clear” button.
- After each record has been input, the user needs to press the “Save” button in order to write that data to the file.
Write the Code:
' Project: Employee Data Part 1
' Description: Program receives input from the user and assigns the values to variables.
' When the user clicks save, the input is added to a text file until the user exits the program.
Imports System.IO
Public Class frmEmployeeDataPart1
' Declare global variables
Dim fileName As String
Dim firstName As String
Dim middleName As String
Dim lastName As String
Dim employeeNumber As Integer
Dim department As String
Dim telephone As String
Dim extension As Integer
Dim emailAddress As String
Dim valid As Boolean = True
Private Sub frmEmployeeDataPart1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Do
fileName = InputBox("Input Needed", "Enter the name of the file.")
If fileName = Nothing Or fileName = "" Then
MessageBox.Show("No file name entered.")
Else
Exit Do
End If
Loop
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
valid = True ' reset value to account for prior invalid input
InputData()
If valid = True Then
WriteDataToFile()
Else
InputData()
End If
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtFirstName.Text = ""
txtMiddleName.Text = ""
txtLastName.Text = ""
txtEmployeeNumber.Text = ""
cboDepartment.SelectedIndex = -1
txtTelephone.Text = ""
txtExtension.Text = ""
txtEmail.Text = ""
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Ends the program when the user clicks on the Exit button
End
End Sub
Sub InputData()
' populate the variables
firstName = txtFirstName.Text
If (firstName.ToUpper >= "A") And (firstName.ToUpper <= "Z") Then
firstName = firstName
Else
valid = False
MessageBox.Show("First Name must start with a letter")
End If
middleName = txtMiddleName.Text
If (middleName.ToUpper >= "A") And (middleName.ToUpper <= "Z") Then
middleName = middleName
Else
valid = False
MessageBox.Show("Middle Name must start with a letter")
End If
lastName = txtLastName.Text
If (lastName.ToUpper >= "A") And (lastName.ToUpper <= "Z") Then
lastName = lastName
Else
valid = False
MessageBox.Show("Last Name must start with a letter")
End If
Try
employeeNumber = CInt(txtEmployeeNumber.Text)
Catch
MessageBox.Show("You must enter an integer for Employee Number.")
valid = False
End Try
If cboDepartment.SelectedIndex <> -1 Then
department = cboDepartment.Text
Else
valid = False
MessageBox.Show("You must select a department.")
End If
telephone = txtTelephone.Text
Try
extension = CInt(txtExtension.Text)
Catch
MessageBox.Show("You must enter an integer for Extension.")
valid = False
End Try
emailAddress = txtEmail.Text
End Sub
Sub WriteDataToFile()
' write the data to the file
Dim sw As StreamWriter = File.AppendText(fileName)
sw.WriteLine(firstName)
sw.WriteLine(middleName)
sw.WriteLine(lastName)
sw.WriteLine(employeeNumber)
sw.WriteLine(department)
sw.WriteLine(telephone)
sw.WriteLine(extension)
sw.WriteLine(emailAddress)
sw.Close()
MessageBox.Show("Record Saved. Please clear the form and enter additional records or exit.")
End Sub
End Class