Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can I open a CSV file in VB6 and extract contents??

Posted on 2004-04-18
9
Medium Priority
?
175 Views
Last Modified: 2010-05-02
Hi,

I'd like my program to open a CSV file, and from the 4th record downwards, start extracting strings from the 3rd column only.

Is there a way to open a CSV file in VB6?
Can I store what record I'm up to (i.e line number) so that with each iteration of a loop I can work my way down the CSV file?

Thanks!

Cambot
0
Comment
Question by:cambot76
  • 4
  • 2
9 Comments
 
LVL 8

Expert Comment

by:ampapa
ID: 10852746
Sure something like the following will get you started.

dim cntLINE as long
dim strRecord,wantedData as string
dim usableData as variant
 
Open "c:\test.CSV" For Input As #1
 
cntLINE = 0

Do While Not EOF(1)
 
   Line Input #1, strRecord

   If cntLINE >4 then  
         usableData = split(strRecord,",")
         wantedData = usableData(2)
         msgbox wantedData
   end if

cntLine = cntLine +
Loop

Close #1
0
 
LVL 8

Expert Comment

by:ampapa
ID: 10852748
sorry, forgot the "1"

cntLine = cntLine +1
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 10853285
I think cambot76 wanted to include the 4th line in what is considered usable data.

Your code ampapa, actually starts allowing data to be "usable" beginning with the sixth line of data since your incrementer is after the check and you've used >4 instead of >=4.

So modifying your code, I think this is what you need:

Dim cntLINE As Long
Dim strRecord As String
Dim wantedData As String
Dim usableData As Variant

cntLINE = 0
Open "C:\Test.CSV" For Input As #1
Do While Not EOF(1)
   Line Input #1, strRecord
   cntLine = cntLine + 1
   If cntLINE >=4 Then  
         usableData = Split(strRecord,",")
         wantedData = usableData(2)
         msgbox wantedData
   End If
Loop
Close #1

cambot76, if you use this code then please accept ampapa's remarks as the answer, not mine.

Regards,

Idle_Mind
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ampapa
ID: 10853546
You are correct Idle_Mind my count could have started at 1 instead of 0 and cntLINE >4 shoud have been cntLINE >2. I misread where cambot76 wanted to start reading records.
0
 
LVL 8

Expert Comment

by:ampapa
ID: 11412623
Thanks for the kindness but my code wouldn't have worked... Being quick isn't the same as being correct. Idle_mind is entitled to the points.

ampapa,
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11412977
Disperse the points as you see fit DanRollins.  I have unlimited available points so it's no big deal to me.  =)

Idle_Mind
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question