|
-
Oct 21st, 2006, 11:08 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Add records to existing Csv File..
Hi, a problem im facing is that in word using vba I add records in a csv file saved in another location. The records go down, for example,
A1 would be record 1
A2 would be record 2
A3 would be record 3 etc.
The problem is that whenever i run the program again, and insert new records they get saved on top of the old records. what i want is the new records to get saved next to the old one. For example:
B1 would have a new record
B2 would have a new record
B3 would have a new record.
any suggestions please?
-
Oct 21st, 2006, 12:08 PM
#2
Re: Add records to existing Csv File..
Using Basic File I/O always writes at the end of the file for new records being added. To get it to add to the top for new records beiing added you need to read the file in completely to an array and then delete or clear the file and write your new records and then write the old array records.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 21st, 2006, 01:32 PM
#3
Thread Starter
Fanatic Member
Re: Add records to existing Csv File..
lol oh so were would i need to look to get help? are there any tutorials or information to help me with arrays, as i have never done anything like this before.
thanks
-
Oct 21st, 2006, 02:07 PM
#4
Re: Add records to existing Csv File..
Well everything is on the forums already but if you need detailed help you just need to ask. 
First link has a detailed tutorial on reading the file into an array.
The other tutorial links are great too.
http://vbforums.com/showthread.php?t...ght=File+I%2FO
http://vbforums.com/showthread.php?t...ght=File+I%2FO
http://vbforums.com/showthread.php?t...ght=File+I%2FO
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 21st, 2006, 02:11 PM
#5
Frenzied Member
Re: Add records to existing Csv File..
Do you mean you want new records added to the end of the existing file, or at the start? When you say "on top of", do you mean they're overwriting the existing records? In that case, Open the file for Append, rather than Output, to add records at the end of the file.
If you want the new ones at the start of the file, you can use Rob's method, or first write the new records to a new file, then read the old file and write the records to the new file.
Tengo mas preguntas que contestas
-
Oct 21st, 2006, 03:36 PM
#6
Thread Starter
Fanatic Member
Re: Add records to existing Csv File..
Oh sorry its a misunderstanding, i changed the output to apend, i thought i needed to have output as this creates a file, and thought apend does NOT create a file if there is no file in the directory.
The help i needed was my current records get saved between a1 and a16, and if i run the userform again and save the records again more records get saved between a17 and a32. I do not want this to happen i want the new records to get saved in to the next cell which is b1 and b17, and if i run this again the next save to happen between c1 and c17. I hope you understand now.
-
Oct 21st, 2006, 06:50 PM
#7
Frenzied Member
Re: Add records to existing Csv File..
Oh, ok, you mean when you open the .csv in Excel. In that case, you would have to do something like Rob suggests. A .csv is just a text file. You can open it in Notepad. Excel just happens to open it in cells.
Tengo mas preguntas que contestas
-
Oct 22nd, 2006, 01:33 AM
#8
Thread Starter
Fanatic Member
Re: Add records to existing Csv File..
Im sorry but if you could give me a quick example id appreciate it, its just I havnt worked with vba much and once I see an example i can take it from there.
So far i know what i want to do is read the file, and add records next to it.
So far i have:
VB Code:
Open "C:\Records.csv" For Append As #1
Print #1, "1,"; genders
Close #1
Open "C:\Records.csv" For Append As #2
Print #2, "2,"; ages
Close #2
(i have declared the genders and ages variables at the top)
This adds to the records. From what i can see i think i need to use something like:
VB Code:
Open strSomeFile For Binary As #1
Dim strBuff As String
strBuff = Space(Lof(1))
Get #1, , strBuff
Close #1
Correct?
Any Suggestions?
-
Oct 22nd, 2006, 03:05 AM
#9
Re: Add records to existing Csv File..
Not sure what else you need but you can do away with the two statements and merge them together.
VB Code:
Open "C:\Records.csv" For Append As #1
Print #1, "1,"; genders
Print #1, "2,"; ages
Close #1
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 22nd, 2006, 09:44 AM
#10
Frenzied Member
Re: Add records to existing Csv File..
 Originally Posted by RobDog888
Not sure what else you need but you can do away with the two statements and merge them together.
VB Code:
Open "C:\Records.csv" For Append As #1
Print #1, "1,"; genders
Print #1, "2,"; ages
Close #1
I don't think that's what he's looking for. I think he wants subsequent Appends to append each new record to the end of the row of existing records, so that he never has more than, say, 16 rows. Each row's just getting longer. That way, when he opens the .csv file in Excel, he never goes past row 16, but the number of columns increases. In other words, he doesn't want to Append to the end of the file, but to the end of each row. So, first .csv file would look like:
Second would look like:
Code:
1, 1,
2, 2,
3, 3,
.
.
16, 16,
not:
Code:
1,
2,
3,
.
.
16,
1,
2,
3,
.
.
16,
At least that's what I'm understanding. It might be easier to automate this in Excel.
Tengo mas preguntas que contestas
-
Oct 22nd, 2006, 12:44 PM
#11
Re: Add records to existing Csv File..
Well going off of his code in post #8 I just simplified it. I too am unsure about what he needs.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 22nd, 2006, 04:05 PM
#12
Thread Starter
Fanatic Member
Re: Add records to existing Csv File..
Rob thanks for making my code simpler. salvelinus that is exactly what i am after. What you have said in your post is what i need. any suggestion please?
-
Oct 22nd, 2006, 06:16 PM
#13
Frenzied Member
Re: Add records to existing Csv File..
Using a .csv file, you'll have to overwrite the file everytime and re-insert all values. That means you'll have to read the values in existing line one, add your new values to that line, read the next line, add new values to that, etc. It can be done, but is awkward. You may want to look at inserting your values directly into an Excel file - .xls - instead, so you can just add new data more diectly.. Don't know enough Excel coding to give you an example offhand, but shouldn't be too hard.
Last edited by salvelinus; Oct 22nd, 2006 at 06:20 PM.
Tengo mas preguntas que contestas
-
Oct 22nd, 2006, 07:42 PM
#14
Thread Starter
Fanatic Member
Re: Add records to existing Csv File..
thing is it was part of an assignment and our teacher requested for the answers to be put in to an csv file which i already have done, and be ready to be opened up in excel which already works with the csv file. I shall show it to my teacher but i think the current form of the csv file is fine, and what i wanted to do was just over the top.
Anyway thanx for your help every1.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|