473,652 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving to files

15 New Member
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 1882
QVeen72
1,445 Recognized Expert Top Contributor
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
Yamasassy
15 New Member
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 Recognized Expert Top Contributor
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
Yamasassy
15 New Member
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:)
Mar 10 '08 #5
VBWheaties
145 New Member
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
Yamasassy
15 New Member
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 = EmployeeCollect ion.Item(i)

Script out of range ?
Mar 10 '08 #7
VBWheaties
145 New Member
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:

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
Yamasassy
15 New Member
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 Recognized Expert Expert
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
2187
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. Running under debug mode permits saving my tiff files (with Image.DEBUG=0, the save doesn't work! see below) , but it truncates the crazy tags in my tiff and changes the resolution of the image
7
7522
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 would appreciate it if anyone could either help me out, or direct me to an online resource site that has information on saving/loading classes. I have found several tutorials, but they either do not really help (saving text files) or are too...
7
4990
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 replaced by what seems to be iso-8859-1 (which Flash doesn't understand, but that's another problem). Any help greatly appreciated. * Something like this...
6
3281
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 will get the page displayed. But I don't want to display it, instead save those files in one of the network directories that can be accessed by our third party vended web based application. How can I do this accessing and saving HTML files a...
2
3762
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 BinaryWriter(fStream); img.Save(bw.BaseStream, ImageFormat.Bmp);
5
3140
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 have tried POSTin to be able to view the image, then saving it, but it doesnt work, the problem i have is that all the files on the server have the same name, which is http://1.1.1.1/DH/repository/content.tif and for me to be able to view one, i...
1
1992
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: An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll Additional information: A generic error occurred in GDI+.
4
6718
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 code ?? thank you Pedro Leite From Portugal ------------------------------------
0
2012
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, but I have a 3rd party software that will convert vb projects into .exe files. My issue is in the the saving of the excel file. The macro bassicly imports a tab delimeted file and manipulates the file and then saves it, as a template to work...
1
2033
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 was created via an existing .sql file. Included were a bunch of stored procedures. I went in to re-format these procedures. (They were written in haste by another programmer. It is my task to go back and improve their readabilities.) However, I am...
0
8367
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8703
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.