Solved

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

Posted on 2004-04-18
9
152 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
Comment Utility
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
Comment Utility
sorry, forgot the "1"

cntLine = cntLine +1
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:ampapa
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now