Link to home
Start Free TrialLog in
Avatar of Computer Guy
Computer Guy

asked on

VB Question

Hi,

Have some code here:

It reads a file for employee numbers and loads them to a list, and if a user tried to enter a duplicate one, an error would populate.

Say employee numbers 1,2,3,4,5 are in the file.

Then I try to add 5, it would call an error. But if I add 6, it goes right in since it is not there. The problem is when I don't leave the form and add 6 again. The record will be added since It's not part of the list. When I write the record to the file, I want to have something that will also add the txtEmployeeNumber.text to the empNum list that is in the load part. So if I were to add 6 in again, there would be an error.

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Read File And Store Employee Numbers
        empNumList = New List(Of String)
        Dim strfileName As String = "text.txt"
        Dim empNum As String = txtEmployeeNumber.Text

        Dim Lines As String() = File.ReadAllLines(strFileName)
        'Dim Found As Boolean = False
        For i As Integer = 3 To Lines.Length - 1 Step 11
            empNumList.Add(Lines(i))
            i -= 1
        Next
        For j As Integer = 0 To empNumList.Count - 1
            Console.WriteLine(empNumList(j))
        Next
    End Sub    
	
	Private Sub txtEmployeeNumber_LostFocus(sender As Object, e As EventArgs) Handles txtEmployeeNumber.LostFocus
        'Check For Duplicate Employee Numbers
        Dim Found As Boolean = False
        Dim empNum = txtEmployeeNumber.Text
        For i As Integer = 0 To empNumList.Count - 1
            If empNumList(i).Equals(empNum) Then
                found = True
                Exit For
            End If
        Next
        If Found Then
            'Show Error If Dupicate
            MessageBox.Show("Employee Id Already Entered" & vbCrLf & _
                                "Please Enter A Unique Employee Number")
            txtEmployeeNumber.Text = ""
            txtEmployeeNumber.Focus()
        Else
        End If
    End Sub
	
	           
  Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
      
                'Will Append To Current File Instead Of Create It From Scratch Again
                employeeFile = File.AppendText(strFileName)
                employeeFile.WriteLine(strFirstName)
                employeeFile.WriteLine(strMiddleName)
                employeeFile.WriteLine(strLastName)
                employeeFile.WriteLine(txtEmployeeNumber.Text)
                employeeFile.WriteLine(strDepartment)
                employeeFile.WriteLine(strTelephone)
                employeeFile.WriteLine(txtExtension.Text)
                employeeFile.WriteLine(strEmail)
                employeeFile.WriteLine(intBaseSalary)
                employeeFile.WriteLine()
                'File Will Close After Every Added Record
                employeeFile.Close()
                'Display Message Box When Data Is Entered, Then Clear For New Data
                MessageBox.Show("Data Entered Successfully")
                Clear_Stuff()
            Catch
                'Display Error If There Is A Problem With File (For Extra Measures)
                MessageBox.Show("File Can't Be Written To")
            End Try
        End If
		End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So After
 MessageBox.Show("Data Entered Successfully")
AddEmployeesToList()

Open in new window

You new  empNumList will also have the last updated  empNo.....

Hope it helps



Yiannis
Just a note..
Don't forget to call the sub also in form Load event...
Avatar of Computer Guy
Computer Guy

ASKER

:-)