HI im having problems to saving to a .txt file, i need to be able to store a number of employees to this file (and beable to open and these files to load in to text boxes)and retrieve then for later use to search and print and them add more at another stage , currently i can save and read from these files but only the last person entered is stored and over written each time . i have to use classes and Collections , im using Visual basic 6
my code so far is :
[CODE=vb]
Public Employee As Object
Public EmployeeCollect ion As New Collection
Dim counter As Integer
Private Sub cmdEdit_Click(I ndex As Integer)
Dim counter As Integer
counter = EmployeeCollect ion.Count
counter = 1
Open "c:\temp\Storag e.txt" For Output As #1
For counter = 1 To forcounter
Print #1, txtFullname.Tex t
Write #1, txtAddress.Text
Write #1, txtArea.Text
Write #1, txtPostcode.Tex t
Write #1, txtContactno.Te xt
Write #1, txtPos.Text
Write #1, txtSalary.Text
forcounter = forcounter + 1
Next
Close #1
End Sub
Private Sub cmdExit_Click(I ndex As Integer)
Dim counter As Integer
counter = EmployeeCollect ion.Count
counter = 1
Open "c:\temp\Storag e.txt" For Output As #1
For counter = 1 To counter
Print #1, txtFullname.Tex t
Write #1, txtArea.Text
Write #1, txtPostcode.Tex t
Write #1, txtContactno.Te xt
Write #1, txtPos.Text
Write #1, txtSalary.Text
' or Print #1, EmployeeCollect ion.item(forcou nter).Fullname
forcounter = forcounter + 1
Next
Close #1
End
End Sub
Private Sub cmdFirst_Click( Index As Integer)
txtEmployeeno.T ext = 1 'set the record number to first employee number
txtFullname.Tex t = EmployeeCollect ion.Item(1).Ful lname
txtArea.Text = EmployeeCollect ion.Item(1).Are a
txtPostcode.Tex t = EmployeeCollect ion.Item(1).Pos tcode
txtAddress.Text = EmployeeCollect ion.Item(1).Add ress
txtContactno.Te xt = EmployeeCollect ion.Item(1).Con tactno
txtPos.Text = EmployeeCollect ion.Item(1).Pos
txtSalary = EmployeeCollect ion.Item(1).Sal ary
End Sub
Private Sub cmdLast_Click(I ndex As Integer)
counter = EmployeeCollect ion.Count
txtEmployeeno.T ext = counter
txtFullname.Tex t = EmployeeCollect ion.Item(counte r).Fullname
txtArea.Text = EmployeeCollect ion.Item(counte r).Area
txtPostcode.Tex t = EmployeeCollect ion.Item(counte r).Postcode
txtAddress.Text = EmployeeCollect ion.Item(counte r).Address
txtContactno.Te xt = EmployeeCollect ion.Item(counte r).Contactno
txtPos.Text = EmployeeCollect ion.Item(counte r).Pos
txtSalary = EmployeeCollect ion.Item(counte r).Salary
End Sub
Private Sub cmdNew_Click(In dex As Integer)
Set Employee = New clsEmployee
Employee.Fullna me = txtFullname.Tex t
Employee.Addres s = txtAddress.Text
Employee.Area = txtArea.Text
Employee.Postco de = txtPostcode.Tex t
Employee.Contac tno = txtContactno.Te xt
Employee.Pos = txtPos.Text
Employee.Salary = txtSalary.Text
EmployeeCollect ion.Add clsEmployee
txtEmployeeno.T ext = EmployeeCollect ion.Count
Set Employee = Nothing
txtFullname.Tex t = " "
txtAddress.Text = " "
txtArea.Text = " "
txtPostcode.Tex t = " "
txtContactno.Te xt = " "
txtPos.Text = " "
txtSalary = " "
End Sub
Private Sub cmdNext_Click(I ndex As Integer)
counter = txtEmployeeno.T ext
counter = counter + 1
txtEmployeeno = counter
txtFullname.Tex t = EmployeeCollect ion.Item(1).Ful lname
txtArea.Text = EmployeeCollect ion.Item(1).Are a
txtPostcode.Tex t = EmployeeCollect ion.Item(1).Pos tcode
txtAddress.Text = EmployeeCollect ion.Item(1).Add ress
txtContactno.Te xt = EmployeeCollect ion.Item(1).Con tactno
txtPos.Text = EmployeeCollect ion.Item(1).Pos
txtSalary = EmployeeCollect ion.Item(1).Sal ary
End Sub
Private Sub cmdPrev_Click(I ndex As Integer)
counter = txtEmployeeno.T ext
counter = counter - 1
txtEmployeeno = counter
txtFullname.Tex t = EmployeeCollect ion.Item(1).Ful lname
txtArea.Text = EmployeeCollect ion.Item(1).Are a
txtPostcode.Tex t = EmployeeCollect ion.Item(1).Pos tcode
txtAddress.Text = EmployeeCollect ion.Item(1).Add ress
txtContactno.Te xt = EmployeeCollect ion.Item(1).Con tactno
txtPos.Text = EmployeeCollect ion.Item(1).Pos
txtSalary = EmployeeCollect ion.Item(1).Sal ary
End Sub
Private Sub Form_Load()
Dim FileFullname As String
Dim FileArea As String
Dim FileAddress As String
Dim FilePostcode As String
Dim FileContactno As String
Dim FilePos As String
Dim FileSalary As String
Set EmployeeCollect ion = New Collection
Set Employee = New clsEmployee
Open "c:\temp\Storag e.txt" For Input As #1
While EOF(1) = False
Input #1, FileFullname
Input #1, FileAddress
Input #1, FileArea
Input #1, FilePostcode
Input #1, FileContactno
Input #1, FilePos
Input #1, FileSalary
Employee.Fullna me = FileFullname
Employee.Addres s = FileAddress
Employee.Area = FileArea
Employee.Postco de = FilePostcode
Employee.Contac tno = FileContactno
Employee.Pos = FilePos
Employee.Salary = FileSalary
EmployeeCollect ion.Add Employee
Wend
Close #1
txtEmployeeno.T ext = EmployeeCollect ion.Count
txtFullname.Tex t = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Fullname
txtAddress.Text = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Address
txtArea.Text = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Area
txtPostcode.Tex t = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Postcode
txtContactno.Te xt = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Contactno
txtPos.Text = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Pos
txtSalary.Text = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Salary
End Sub
[/CODE]
my code so far is :
[CODE=vb]
Public Employee As Object
Public EmployeeCollect ion As New Collection
Dim counter As Integer
Private Sub cmdEdit_Click(I ndex As Integer)
Dim counter As Integer
counter = EmployeeCollect ion.Count
counter = 1
Open "c:\temp\Storag e.txt" For Output As #1
For counter = 1 To forcounter
Print #1, txtFullname.Tex t
Write #1, txtAddress.Text
Write #1, txtArea.Text
Write #1, txtPostcode.Tex t
Write #1, txtContactno.Te xt
Write #1, txtPos.Text
Write #1, txtSalary.Text
forcounter = forcounter + 1
Next
Close #1
End Sub
Private Sub cmdExit_Click(I ndex As Integer)
Dim counter As Integer
counter = EmployeeCollect ion.Count
counter = 1
Open "c:\temp\Storag e.txt" For Output As #1
For counter = 1 To counter
Print #1, txtFullname.Tex t
Write #1, txtArea.Text
Write #1, txtPostcode.Tex t
Write #1, txtContactno.Te xt
Write #1, txtPos.Text
Write #1, txtSalary.Text
' or Print #1, EmployeeCollect ion.item(forcou nter).Fullname
forcounter = forcounter + 1
Next
Close #1
End
End Sub
Private Sub cmdFirst_Click( Index As Integer)
txtEmployeeno.T ext = 1 'set the record number to first employee number
txtFullname.Tex t = EmployeeCollect ion.Item(1).Ful lname
txtArea.Text = EmployeeCollect ion.Item(1).Are a
txtPostcode.Tex t = EmployeeCollect ion.Item(1).Pos tcode
txtAddress.Text = EmployeeCollect ion.Item(1).Add ress
txtContactno.Te xt = EmployeeCollect ion.Item(1).Con tactno
txtPos.Text = EmployeeCollect ion.Item(1).Pos
txtSalary = EmployeeCollect ion.Item(1).Sal ary
End Sub
Private Sub cmdLast_Click(I ndex As Integer)
counter = EmployeeCollect ion.Count
txtEmployeeno.T ext = counter
txtFullname.Tex t = EmployeeCollect ion.Item(counte r).Fullname
txtArea.Text = EmployeeCollect ion.Item(counte r).Area
txtPostcode.Tex t = EmployeeCollect ion.Item(counte r).Postcode
txtAddress.Text = EmployeeCollect ion.Item(counte r).Address
txtContactno.Te xt = EmployeeCollect ion.Item(counte r).Contactno
txtPos.Text = EmployeeCollect ion.Item(counte r).Pos
txtSalary = EmployeeCollect ion.Item(counte r).Salary
End Sub
Private Sub cmdNew_Click(In dex As Integer)
Set Employee = New clsEmployee
Employee.Fullna me = txtFullname.Tex t
Employee.Addres s = txtAddress.Text
Employee.Area = txtArea.Text
Employee.Postco de = txtPostcode.Tex t
Employee.Contac tno = txtContactno.Te xt
Employee.Pos = txtPos.Text
Employee.Salary = txtSalary.Text
EmployeeCollect ion.Add clsEmployee
txtEmployeeno.T ext = EmployeeCollect ion.Count
Set Employee = Nothing
txtFullname.Tex t = " "
txtAddress.Text = " "
txtArea.Text = " "
txtPostcode.Tex t = " "
txtContactno.Te xt = " "
txtPos.Text = " "
txtSalary = " "
End Sub
Private Sub cmdNext_Click(I ndex As Integer)
counter = txtEmployeeno.T ext
counter = counter + 1
txtEmployeeno = counter
txtFullname.Tex t = EmployeeCollect ion.Item(1).Ful lname
txtArea.Text = EmployeeCollect ion.Item(1).Are a
txtPostcode.Tex t = EmployeeCollect ion.Item(1).Pos tcode
txtAddress.Text = EmployeeCollect ion.Item(1).Add ress
txtContactno.Te xt = EmployeeCollect ion.Item(1).Con tactno
txtPos.Text = EmployeeCollect ion.Item(1).Pos
txtSalary = EmployeeCollect ion.Item(1).Sal ary
End Sub
Private Sub cmdPrev_Click(I ndex As Integer)
counter = txtEmployeeno.T ext
counter = counter - 1
txtEmployeeno = counter
txtFullname.Tex t = EmployeeCollect ion.Item(1).Ful lname
txtArea.Text = EmployeeCollect ion.Item(1).Are a
txtPostcode.Tex t = EmployeeCollect ion.Item(1).Pos tcode
txtAddress.Text = EmployeeCollect ion.Item(1).Add ress
txtContactno.Te xt = EmployeeCollect ion.Item(1).Con tactno
txtPos.Text = EmployeeCollect ion.Item(1).Pos
txtSalary = EmployeeCollect ion.Item(1).Sal ary
End Sub
Private Sub Form_Load()
Dim FileFullname As String
Dim FileArea As String
Dim FileAddress As String
Dim FilePostcode As String
Dim FileContactno As String
Dim FilePos As String
Dim FileSalary As String
Set EmployeeCollect ion = New Collection
Set Employee = New clsEmployee
Open "c:\temp\Storag e.txt" For Input As #1
While EOF(1) = False
Input #1, FileFullname
Input #1, FileAddress
Input #1, FileArea
Input #1, FilePostcode
Input #1, FileContactno
Input #1, FilePos
Input #1, FileSalary
Employee.Fullna me = FileFullname
Employee.Addres s = FileAddress
Employee.Area = FileArea
Employee.Postco de = FilePostcode
Employee.Contac tno = FileContactno
Employee.Pos = FilePos
Employee.Salary = FileSalary
EmployeeCollect ion.Add Employee
Wend
Close #1
txtEmployeeno.T ext = EmployeeCollect ion.Count
txtFullname.Tex t = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Fullname
txtAddress.Text = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Address
txtArea.Text = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Area
txtPostcode.Tex t = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Postcode
txtContactno.Te xt = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Contactno
txtPos.Text = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Pos
txtSalary.Text = EmployeeCollect ion.Item(Employ eeCollection.Co unt).Salary
End Sub
[/CODE]
Comment