Solved

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

Posted on 2004-04-18
9
162 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
Independent Software Vendors: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month6 days, 7 hours left to enroll

634 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