Solved

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

Posted on 2004-04-18
9
153 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 85

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 85

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

777 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