|
-
Oct 12th, 2005, 12:55 PM
#1
Thread Starter
Member
[RESOLVED] Best way to export text to a CSV file?
Okay, I have my "updated" VB6 app working. Next step is to export certian fields into a CSV so they can be imported into an Access database.
Background -
The VB app hashes a text file something like this... The file is fixed width, and 3 or 4 characters on the right side get read.
EXAMPLE
Select Case cc
Case "DA" To "DD"
NoSupport$ = "yes"
skipcnt2% = skipcnt2% + 1
Case "XA" To "XJ"
NoSupport$ = "yes"
skipcnt2% = skipcnt2% + 1
Nsnap.SF43 = "Card Type not supported " & cctext
Nsnap.Narr = RTrim(Nsnap.Narr) & "[" & Mid(textdata, 18, 63) & "]" &CRLF$ & " "
Case "YX" To "YZ"
NoSupport$ = "yes"
skipcnt2% = skipcnt2% + 1
Nsnap.SF43 = "Card Type not supported " & cctext
Nsnap.Narr = RTrim(Nsnap.Narr) & "[" & Mid(textdata, 18, 63) & "]" & CRLF$
This then gets hacked down into seperate fields displayed on a form based on number of characters and position in the line(I believe)
EXAMPLE
SF1 As String * 4 ' 2 Work Center
SF2 As String * 4 ' 3 Job Seq No
SF3 As String * 11 ' 4 APL / AEL
SF4 As String * 19 ' 5 Equip Noun Name
SF5 As String * 1 ' 6 WND - When Discovered Date
SF6 As String * 1 ' 7 STA - Status
Any suggestions as the best way to extract specific fieldsfrom specific records(as they are displayed) to add to a CSV formatted file?
John
-
Oct 12th, 2005, 01:08 PM
#2
Re: Best way to export text to a CSV file?
I would split the data into arrays, then you can loop thru
I would use WRITE to add the fields to a text file. It will add quotes, and pound signs where required, and also add commas between the variables.
VB Code:
dim ff as integer, x as integer
ff=freefile
open app.path & "\outfile.txt" for output as #ff
for x=0 to ubound(myarray)
write #ff, myarray1(x), myarray2(x)
next x
close #ff
-
Oct 12th, 2005, 01:11 PM
#3
Thread Starter
Member
Re: Best way to export text to a CSV file?
Array not needed. I should have said that only a few of the displayed fields would need to be put into the file.
BUT, this does put me on the right track !
Just because I want to put a rubber ducky into a post -
John
-
Oct 12th, 2005, 01:19 PM
#4
Thread Starter
Member
Re: Best way to export text to a CSV file?
Okay, I'm even less experienced that I orginally thought. What I tried didn't work...
What is the proper way to open say, Wordpad, and put the selected fields in there. Comma seperated of course, appending a new line each time instead of overwriting.
-
Oct 12th, 2005, 01:33 PM
#5
Re: Best way to export text to a CSV file?
if u use append instead of output it will add to the file..
its faster to do it like this that to try to manipulate wordpad etc...
once its done then maybe open wordpad..
open app.path & "\outfile.txt" for append as #ff
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Oct 12th, 2005, 01:53 PM
#6
Thread Starter
Member
Re: Best way to export text to a CSV file?
So I should open the file, and specify the text to add like this -
Dim ff As String, x As Integer
ff = FreeFile
Open App.Path & "\outfile.txt" For Append As #ff
'For x = 0 To UBound(ff)
FreeFile = Form1.fieldboxes(0) & "," & Form1.fieldboxes(1)
Write #ff,
'Next x
Close #ff
-
Oct 12th, 2005, 02:00 PM
#7
Re: Best way to export text to a CSV file?
Try this:
VB Code:
Dim ff As Integer
ff = FreeFile
Open App.Path & "\outfile.txt" For Append As #ff
Write #ff, Form1.fieldboxes(0), Form1.fieldboxes(1)
Close #ff
ff is the filenumber, which you need for the Open, Close, and Write statements. FreeFile determines the next one that you can use.
-
Oct 12th, 2005, 02:13 PM
#8
Thread Starter
Member
Re: Best way to export text to a CSV file?
Ahh, I see.
And the method to make the filepath user defined is? I'm new to VB, can you tell?
John
-
Oct 12th, 2005, 02:30 PM
#9
Re: Best way to export text to a CSV file?
App.Path puts in wherever the program is located. You can hard code the path, by leaving app.path off and adding the path with the filename.
"c:\temp\outfile.txt"
or did you want the user to pick a file location?
Take a look at this example. It's one way to do it.
http://vbforums.com/attachment.php?attachmentid=40809
-
Oct 12th, 2005, 02:38 PM
#10
Thread Starter
Member
Re: Best way to export text to a CSV file?
Thanks for the info!
But you know something, now that I think about it,the user will be adding to this CSV file irregularly while using this application. I should probably hard code the destination file.
John
-
Oct 12th, 2005, 02:42 PM
#11
Re: Best way to export text to a CSV file?
just put the file in the same location as the app and use App.Path & "\outfile.txt"
then, no matter where the user puts the app.. the outfile will be created there & added to there
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Oct 12th, 2005, 02:43 PM
#12
Re: Best way to export text to a CSV file?
You might also want to let the user KILL the file from time to time. Append will create it if it's not there, though.
-
Oct 12th, 2005, 03:07 PM
#13
Thread Starter
Member
Re: Best way to export text to a CSV file?
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
|