Solved

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

Posted on 2004-04-18
9
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

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!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

710 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