Saving to files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yamasassy
    New Member
    • Mar 2008
    • 15

    Saving to files

    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]
    Last edited by debasisdas; Mar 10 '08, 01:31 PM. Reason: added code=vb tags
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    I guess, you are writing the Code To Save in Exit Button, And You are saving the Last Displayed TextBox Values.
    Instead, Loop through all the Collection Items and Save From the Collection to the File..

    Regards
    Veena

    Comment

    • Yamasassy
      New Member
      • Mar 2008
      • 15

      #3
      Originally posted by QVeen72
      Hi,

      I guess, you are writing the Code To Save in Exit Button, And You are saving the Last Displayed TextBox Values.
      Instead, Loop through all the Collection Items and Save From the Collection to the File..

      Regards
      Veena

      How would i go about doing this ? im new to VB and not really any good with it

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        Change the Code in Cmd_Exit to this :

        [CODE=vb]
        Private Sub cmdExit_Click(I ndex As Integer)

        Dim i As Integer
        Dim counter As Integer

        counter = EmployeeCollect ion.Count

        Open "c:\temp\Storag e.txt" For Output As #1

        For i = 1 To counter

        Print #1, EmployeeCollect ion.Item(i).Ful lname
        Print #1, EmployeeCollect ion.Item(i).Are a
        Print #1, EmployeeCollect ion.Item(i).Pos tcode
        Print #1, EmployeeCollect ion.Item(i).Add ress
        Print #1, EmployeeCollect ion.Item(i).Con tactno
        Print #1, EmployeeCollect ion.Item(i).Pos
        Print #1, EmployeeCollect ion.Item(i).Sal ary
        Next

        Close #1

        End

        End Sub

        [/CODE]

        Regards
        Veena

        Comment

        • Yamasassy
          New Member
          • Mar 2008
          • 15

          #5
          its giving out now an error 424 on

          Print #1, EmployeeCollect ion.Item(i).Ful lname

          object required , sorry to be a pain but im really greatfull for ypur help:)

          Comment

          • VBWheaties
            New Member
            • Feb 2008
            • 145

            #6
            Will the following work?

            Code:
                
                Dim employeeObject As clsEmployee
            
                Set employeeObject = EmployeeCollection.Item(i) 
                
                Print #1, employeeObject.Fullname
                Print #1, employeeObject.Area
                Print #1, employeeObject.Postcode
                Print #1, employeeObject.Address
                Print #1, employeeObject.Contactno
                Print #1, employeeObject.Pos
                Print #1, employeeObject.Salary

            Comment

            • Yamasassy
              New Member
              • Mar 2008
              • 15

              #7
              Originally posted by VBWheaties
              Will the following work?

              Code:
                  
                  Dim employeeObject As clsEmployee
              
                  Set employeeObject = EmployeeCollection.Item(i) 
                  
                  Print #1, employeeObject.Fullname
                  Print #1, employeeObject.Area
                  Print #1, employeeObject.Postcode
                  Print #1, employeeObject.Address
                  Print #1, employeeObject.Contactno
                  Print #1, employeeObject.Pos
                  Print #1, employeeObject.Salary


              nope giving out error 9 on click of exit button

              Set employeeObject = EmployeeCollect ion.Item(i)

              Script out of range ?

              Comment

              • VBWheaties
                New Member
                • Feb 2008
                • 145

                #8
                Originally posted by Yamasassy
                nope giving out error 9 on click of exit button

                Set employeeObject = EmployeeCollect ion.Item(i)

                Script out of range ?
                This may be your issue:

                Code:
                Private Sub cmdNew_Click(Index As Integer)
                 
                Set Employee = New clsEmployee
                 
                Employee.Fullname = txtFullname.Text
                Employee.Address = txtAddress.Text
                Employee.Area = txtArea.Text
                Employee.Postcode = txtPostcode.Text
                Employee.Contactno = txtContactno.Text
                Employee.Pos = txtPos.Text
                Employee.Salary = txtSalary.Text
                 
                 
                EmployeeCollection.Add clsEmployee
                The last line above shows you are adding a type to the collection and not the object itself. I think you should be adding Employee and not its type 'clsEmployee' to the collection.

                Comment

                • Yamasassy
                  New Member
                  • Mar 2008
                  • 15

                  #9
                  ok i got some thing working but still kinda over writting in the txt file now using

                  Code:
                  Private Sub cmdExit_Click(Index As Integer)
                   
                  Dim i As Integer
                  Dim counter As Integer
                   
                  counter = EmployeeCollection.Count
                   
                  Open "c:\temp\Storage.txt" For Append As #1
                  Seek #1, (counter + 1) * 7
                      Print #1, txtFullname.Text
                      Print #1, txtAddress.Text
                      Print #1, txtArea.Text
                      Print #1, txtPostcode.Text
                      Print #1, txtContactno.Text
                      Print #1, txtPos.Text
                      Print #1, txtSalary.Text
                     
                   Close #1
                   End
                   
                  End Sub
                  has anyone got any ideas to stop it over writting ?

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    The way you're using that Seek statement, it looks as though you're trying to treat the file as though all entries are exactly the same size. I doubt this is in fact the case. If you want to add onto the end of the file, just remove the Seek statement.

                    Comment

                    Working...