Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is my Visual Basic code for saving employee data. When I try to record the

ID: 3811986 • Letter: T

Question

This is my Visual Basic code for saving employee data. When I try to record the data im getting an error. It seems there is an issue with the code pertaining to the file name, please help. The bold text is where i think the problem is:

'This program will demonstrate how to save the employee details into file

'Import the package

Imports System.IO

Public Class Form1

    'Global variable declarations

    Dim txtFile As StreamWriter

    Dim FileName As String

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

        'Get the file name from the user

        FileName = InputBox("Enter the File Name:", "File Name")

    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        'Append the text file

        txtFile = File.AppendText(FileName)

        'Try block

        Try

            'Write employee first name to a specified file

            txtFile.WriteLine(txtFirstName.Text)

            'Write employee middle name to a specified file

            txtFile.WriteLine(txtMiddleName.Text)

            'Write employee last name to a specified file

            txtFile.WriteLine(txtLastName.Text)

            'Write employee number to a specified file

            txtFile.WriteLine(Convert.ToInt32(txtEmpNumber.Text))

            'Write employee department to a specified file

            txtFile.WriteLine(cboDepartment.SelectedItem.ToString())

            'Write employee phone number to a specified file

            txtFile.WriteLine(txtTelephone.Text)

            'Write employee extension to a specified file

            txtFile.WriteLine(Convert.ToInt32(txtExtension.Text))

            'Write employee email id to a specified file

            txtFile.WriteLine(txtEmail.Text)

            'Close the file

            txtFile.Close()

            'Display the successful message

            MessageBox.Show("Record Saved")

            'Catch block

        Catch ex As Exception

            'If any exception occurs it will display exception details

            MessageBox.Show(ex.ToString())

        End Try

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

        'Clear the all text field details

        txtFirstName.Text = ""

        txtMiddleName.Text = ""

        txtLastName.Text = ""

        txtEmpNumber.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

        'Close the form

        Me.Close()

    End Sub

End Class

Explanation / Answer

This program will take the input from the User and save it to file. It is doing the entire operation as you are trying to do
with the given snippet.

Imports System.IO

Public Class EmployeeData
' 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 EmployeeData_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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote