473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 :

Expand|Select|Wrap|Line Numbers
  1. Public Employee As Object
  2. Public EmployeeCollection As New Collection
  3. Dim counter As Integer
  4.  
  5. Private Sub cmdEdit_Click(Index As Integer)
  6. Dim counter As Integer
  7.  
  8. counter = EmployeeCollection.Count
  9.  
  10. counter = 1
  11.  
  12. Open "c:\temp\Storage.txt" For Output As #1
  13.  
  14. For counter = 1 To forcounter
  15.  
  16. Print #1, txtFullname.Text
  17.  
  18.  
  19. Write #1, txtAddress.Text
  20. Write #1, txtArea.Text
  21. Write #1, txtPostcode.Text
  22. Write #1, txtContactno.Text
  23. Write #1, txtPos.Text
  24. Write #1, txtSalary.Text
  25.  
  26. forcounter = forcounter + 1
  27.  
  28. Next
  29.  
  30. Close #1
  31.  
  32. End Sub
  33.  
  34. Private Sub cmdExit_Click(Index As Integer)
  35.  
  36.  
  37. Dim counter As Integer
  38.  
  39. counter = EmployeeCollection.Count
  40.  
  41. counter = 1
  42.  
  43. Open "c:\temp\Storage.txt" For Output As #1
  44.  
  45. For counter = 1 To counter
  46.  
  47. Print #1, txtFullname.Text
  48. Write #1, txtArea.Text
  49. Write #1, txtPostcode.Text
  50. Write #1, txtContactno.Text
  51. Write #1, txtPos.Text
  52. Write #1, txtSalary.Text
  53.  
  54.  
  55. ' or Print #1, EmployeeCollection.item(forcounter).Fullname
  56.  
  57. forcounter = forcounter + 1
  58.  
  59. Next
  60.  
  61. Close #1
  62.  
  63. End
  64.  
  65. End Sub
  66.  
  67.  
  68. Private Sub cmdFirst_Click(Index As Integer)
  69.  
  70.     txtEmployeeno.Text = 1 'set the record number to first employee number
  71.  
  72.     txtFullname.Text = EmployeeCollection.Item(1).Fullname
  73.     txtArea.Text = EmployeeCollection.Item(1).Area
  74.     txtPostcode.Text = EmployeeCollection.Item(1).Postcode
  75.     txtAddress.Text = EmployeeCollection.Item(1).Address
  76.     txtContactno.Text = EmployeeCollection.Item(1).Contactno
  77.     txtPos.Text = EmployeeCollection.Item(1).Pos
  78.     txtSalary = EmployeeCollection.Item(1).Salary
  79.  
  80. End Sub
  81.  
  82. Private Sub cmdLast_Click(Index As Integer)
  83.  
  84.     counter = EmployeeCollection.Count
  85.  
  86.     txtEmployeeno.Text = counter
  87.  
  88.     txtFullname.Text = EmployeeCollection.Item(counter).Fullname
  89.     txtArea.Text = EmployeeCollection.Item(counter).Area
  90.     txtPostcode.Text = EmployeeCollection.Item(counter).Postcode
  91.     txtAddress.Text = EmployeeCollection.Item(counter).Address
  92.     txtContactno.Text = EmployeeCollection.Item(counter).Contactno
  93.     txtPos.Text = EmployeeCollection.Item(counter).Pos
  94.     txtSalary = EmployeeCollection.Item(counter).Salary
  95.  
  96. End Sub
  97.  
  98. Private Sub cmdNew_Click(Index As Integer)
  99.  
  100. Set Employee = New clsEmployee
  101.  
  102. Employee.Fullname = txtFullname.Text
  103. Employee.Address = txtAddress.Text
  104. Employee.Area = txtArea.Text
  105. Employee.Postcode = txtPostcode.Text
  106. Employee.Contactno = txtContactno.Text
  107. Employee.Pos = txtPos.Text
  108. Employee.Salary = txtSalary.Text
  109.  
  110.  
  111. EmployeeCollection.Add clsEmployee
  112. txtEmployeeno.Text = EmployeeCollection.Count
  113.  
  114. Set Employee = Nothing
  115.  
  116. txtFullname.Text = " "
  117. txtAddress.Text = " "
  118. txtArea.Text = " "
  119. txtPostcode.Text = " "
  120. txtContactno.Text = " "
  121. txtPos.Text = " "
  122. txtSalary = " "
  123.  
  124. End Sub
  125.  
  126. Private Sub cmdNext_Click(Index As Integer)
  127.  
  128. counter = txtEmployeeno.Text
  129. counter = counter + 1
  130. txtEmployeeno = counter
  131.  
  132.     txtFullname.Text = EmployeeCollection.Item(1).Fullname
  133.     txtArea.Text = EmployeeCollection.Item(1).Area
  134.     txtPostcode.Text = EmployeeCollection.Item(1).Postcode
  135.     txtAddress.Text = EmployeeCollection.Item(1).Address
  136.     txtContactno.Text = EmployeeCollection.Item(1).Contactno
  137.     txtPos.Text = EmployeeCollection.Item(1).Pos
  138.     txtSalary = EmployeeCollection.Item(1).Salary
  139.  
  140. End Sub
  141.  
  142. Private Sub cmdPrev_Click(Index As Integer)
  143.  
  144. counter = txtEmployeeno.Text
  145. counter = counter - 1
  146. txtEmployeeno = counter
  147.  
  148.     txtFullname.Text = EmployeeCollection.Item(1).Fullname
  149.     txtArea.Text = EmployeeCollection.Item(1).Area
  150.     txtPostcode.Text = EmployeeCollection.Item(1).Postcode
  151.     txtAddress.Text = EmployeeCollection.Item(1).Address
  152.     txtContactno.Text = EmployeeCollection.Item(1).Contactno
  153.     txtPos.Text = EmployeeCollection.Item(1).Pos
  154.     txtSalary = EmployeeCollection.Item(1).Salary
  155.  
  156. End Sub
  157.  
  158. Private Sub Form_Load()
  159.  
  160.     Dim FileFullname As String
  161.     Dim FileArea As String
  162.     Dim FileAddress As String
  163.     Dim FilePostcode As String
  164.     Dim FileContactno As String
  165.     Dim FilePos As String
  166.     Dim FileSalary As String
  167.  
  168. Set EmployeeCollection = New Collection
  169. Set Employee = New clsEmployee
  170.  
  171. Open "c:\temp\Storage.txt" For Input As #1
  172.  
  173. While EOF(1) = False
  174.  
  175.     Input #1, FileFullname
  176.     Input #1, FileAddress
  177.     Input #1, FileArea
  178.     Input #1, FilePostcode
  179.     Input #1, FileContactno
  180.     Input #1, FilePos
  181.     Input #1, FileSalary
  182.  
  183.     Employee.Fullname = FileFullname
  184.     Employee.Address = FileAddress
  185.     Employee.Area = FileArea
  186.     Employee.Postcode = FilePostcode
  187.     Employee.Contactno = FileContactno
  188.     Employee.Pos = FilePos
  189.     Employee.Salary = FileSalary
  190.     EmployeeCollection.Add Employee
  191.  
  192. Wend
  193. Close #1
  194.  
  195.     txtEmployeeno.Text = EmployeeCollection.Count
  196.     txtFullname.Text = EmployeeCollection.Item(EmployeeCollection.Count).Fullname
  197.     txtAddress.Text = EmployeeCollection.Item(EmployeeCollection.Count).Address
  198.     txtArea.Text = EmployeeCollection.Item(EmployeeCollection.Count).Area
  199.     txtPostcode.Text = EmployeeCollection.Item(EmployeeCollection.Count).Postcode
  200.     txtContactno.Text = EmployeeCollection.Item(EmployeeCollection.Count).Contactno
  201.     txtPos.Text = EmployeeCollection.Item(EmployeeCollection.Count).Pos
  202.     txtSalary.Text = EmployeeCollection.Item(EmployeeCollection.Count).Salary
  203. End Sub
  204.  
Mar 10 '08 #1
9 1872
QVeen72
1,445 Expert 1GB
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
Mar 10 '08 #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

How would i go about doing this ? im new to VB and not really any good with it
Mar 10 '08 #3
QVeen72
1,445 Expert 1GB
Hi,

Change the Code in Cmd_Exit to this :

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExit_Click(Index As Integer)
  2.  
  3. Dim i As Integer
  4. Dim counter As Integer
  5.  
  6. counter = EmployeeCollection.Count
  7.  
  8. Open "c:\temp\Storage.txt" For Output As #1
  9.  
  10. For i = 1 To counter
  11.  
  12.     Print #1, EmployeeCollection.Item(i).Fullname
  13.     Print #1, EmployeeCollection.Item(i).Area
  14.     Print #1, EmployeeCollection.Item(i).Postcode
  15.     Print #1, EmployeeCollection.Item(i).Address
  16.     Print #1, EmployeeCollection.Item(i).Contactno
  17.     Print #1, EmployeeCollection.Item(i).Pos
  18.     Print #1, EmployeeCollection.Item(i).Salary
  19. Next
  20.  
  21. Close #1
  22.  
  23. End
  24.  
  25. End Sub
  26.  
  27.  
Regards
Veena
Mar 10 '08 #4
its giving out now an error 424 on

Print #1, EmployeeCollection.Item(i).Fullname

object required , sorry to be a pain but im really greatfull for ypur help:)
Mar 10 '08 #5
VBWheaties
145 100+
Will the following work?

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Dim employeeObject As clsEmployee
  3.  
  4.     Set employeeObject = EmployeeCollection.Item(i) 
  5.  
  6.     Print #1, employeeObject.Fullname
  7.     Print #1, employeeObject.Area
  8.     Print #1, employeeObject.Postcode
  9.     Print #1, employeeObject.Address
  10.     Print #1, employeeObject.Contactno
  11.     Print #1, employeeObject.Pos
  12.     Print #1, employeeObject.Salary
  13.  
Mar 10 '08 #6
Will the following work?

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Dim employeeObject As clsEmployee
  3.  
  4.     Set employeeObject = EmployeeCollection.Item(i) 
  5.  
  6.     Print #1, employeeObject.Fullname
  7.     Print #1, employeeObject.Area
  8.     Print #1, employeeObject.Postcode
  9.     Print #1, employeeObject.Address
  10.     Print #1, employeeObject.Contactno
  11.     Print #1, employeeObject.Pos
  12.     Print #1, employeeObject.Salary
  13.  


nope giving out error 9 on click of exit button

Set employeeObject = EmployeeCollection.Item(i)

Script out of range ?
Mar 10 '08 #7
VBWheaties
145 100+
nope giving out error 9 on click of exit button

Set employeeObject = EmployeeCollection.Item(i)

Script out of range ?
This may be your issue:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNew_Click(Index As Integer)
  2.  
  3. Set Employee = New clsEmployee
  4.  
  5. Employee.Fullname = txtFullname.Text
  6. Employee.Address = txtAddress.Text
  7. Employee.Area = txtArea.Text
  8. Employee.Postcode = txtPostcode.Text
  9. Employee.Contactno = txtContactno.Text
  10. Employee.Pos = txtPos.Text
  11. Employee.Salary = txtSalary.Text
  12.  
  13.  
  14. EmployeeCollection.Add clsEmployee   
  15.  
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.
Mar 10 '08 #8
ok i got some thing working but still kinda over writting in the txt file now using

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmdExit_Click(Index As Integer)
  3.  
  4. Dim i As Integer
  5. Dim counter As Integer
  6.  
  7. counter = EmployeeCollection.Count
  8.  
  9. Open "c:\temp\Storage.txt" For Append As #1
  10. Seek #1, (counter + 1) * 7
  11.     Print #1, txtFullname.Text
  12.     Print #1, txtAddress.Text
  13.     Print #1, txtArea.Text
  14.     Print #1, txtPostcode.Text
  15.     Print #1, txtContactno.Text
  16.     Print #1, txtPos.Text
  17.     Print #1, txtSalary.Text
  18.  
  19.  Close #1
  20.  End
  21.  
  22. End Sub
  23.  
has anyone got any ideas to stop it over writting ?
Mar 10 '08 #9
Killer42
8,435 Expert 8TB
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.
Mar 11 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: mchl gdbt | last post by:
Hi, I have several thousand tiffs generated by application A which are read by application B. I need to remove a few colours from the tiffs and I decided to try with the python imaging library....
7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
7
by: Philipp Lenssen | last post by:
How do I load and save a UTF-8 document in XML in ASP/VBS? Well, the loading* is not the problem actually -- the file is in UTF-8, and understood correctly -- but once saved, the UTF-8 is...
6
by: Vijay | last post by:
I need to generate HTML files based on the some codes. One HTML file per code. I have the link (ex:http://123.234.345.456/WebPages/GetTestData.aspx?SomeCode=25), by passing the code as parameter I...
2
by: Peder Y | last post by:
My code is something like this: --------------- Image img = Image.FromFile("somefile.bmp"); FileStream fStream = new FileStream("someBinaryFile.dat"); BinaryWriter bw = new...
5
by: Thaynann | last post by:
I have an app that (at the moment) moves through files that are on a web site, and deletes them, wat i want to do for the next stage, is to be able to download each file before i delete it. i...
1
by: M Keeton | last post by:
I currently have a picture which is stored in a "System.Drawing.Image" variable and I want to save it as a bitmap file. I have tried 2 different approaches and both give me the following error: ...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
0
by: Speilman_54 | last post by:
Hi, I'm converting an excel Macro into visual basic 2005 express, as I don't have a copy of VB 6 and trying to make and executable from it, I know this version doesn't have the save file as .exe,...
1
by: Allie | last post by:
Hi, all. This might be a silly question... but I am very new to programming in SQL so please bear with me :) So. I'm using MS SQL Server 2005 Management Studio Express. I have a table that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.