troubleshooting Question

VB Help

Avatar of Computer Guy
Computer Guy asked on
Visual Basic.NET
58 Comments1 Solution295 ViewsLast Modified:

I have some data that is in a txt file. When the application loads, I will have the ability to add more data to the file. Though there is one field that can't be duplicated. So I need to check the existing records against the data I am going to enter.

When I'm in the application I have the lost_focus command on the field I want to check and I store the data into an array. So if I enter a duplicate employee number there will be an error.

The problem is when I open the application it won't check against existing data, just what I enter in now. Any ideas on how I can make this work? I know It's simple I just can't place it.

Lost Focus Code
    Private Sub txtEmployeeNumber_LostFocus(sender As Object, e As EventArgs) Handles txtEmployeeNumber.LostFocus
        'Check For Existing Employee Number
        Dim index As Integer = Array.IndexOf(empArray, CInt(txtEmployeeNumber.Text))
        If index = -1 Then
            ReDim Preserve empArray(empArray.Length)
            empArray(empArray.Length - 1) = CInt(txtEmployeeNumber.Text)
            'Show Error If Dupicate
            MessageBox.Show("Employee Id Already Entered" & vbCrLf & _
                                "Please Enter A Unique Employee Number")
            txtEmployeeNumber.Text = ""
        End If
    End Sub

Here is my code that writes to the file
                Dim strFirstName As String = txtFirstName.Text
                Dim strMiddleName As String = txtMiddleName.Text
                Dim strLastName As String = txtLastName.Text
                Dim intEmployeeNumber As Integer = CInt(txtEmployeeNumber.Text)
                Dim strDepartment As String = ComboBoxDepartment.SelectedItem.ToString()
                Dim strTelephone As String = txtPhoneNumber.Text
                Dim intExtension As Integer = CInt(txtExtension.Text)
                Dim strEmail As String = txtEmailAddress.Text
                Dim intBaseSalary As Integer = CInt(txtBaseSalary.Text)

                employeeFile = File.AppendText(strFileName)
                'Display Message Box When Data Is Entered, Then Clear For New Data
                MessageBox.Show("Data Entered Successfully")
                'Display Error If There Is A Problem With File (For Extra Measures)
                MessageBox.Show("File Can't Be Written To")
            End Try
        End If
John (Yiannis) Toutountzoglou
Instructor Multiengine Pilot

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 58 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 58 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros