Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading data from .txt or .cvs file via VB 6.0

Status
Not open for further replies.

MavrickStl

Programmer
Feb 20, 2001
71
0
0
US
Guys,

I am trying to write a VB 6.0 program to read data from .txt or .cvs files and save it into different variables. Later I want to write this data into an Oracle database.


Thanks, Mavrick
 
Heres a simple example of reading data from a csv file.
Code:
------ Assume The csv file has this format ---------
Smith, John, 45, 55000, Database Administrator
------ ---------------------------------------------

If you wanted to put this info into variables named
Lastname, FirstName, Age, Salary, and Title

Code:
Dim FileLine As String 
Dim LastName As String 
Dim FirstName As String 
Dim Age As Integer 
Dim Salary As Currency 
Dim Title As String 
Dim StartPos As Integer 
Dim CommaPos As Integer 
Dim Count As Integer

Open "C:\test.csv" For input as #1 

While Not EOF(1) 

If EOF(1) Then GoTo ReadAll 

Line Input #1, FileLine 

StartPos = 1 
'This for 1 to 5 loop would actually be For 1 To Number of vars 
For Count = 1 To 5
   'For all but the last variable, find the ending comma
   'for the last variable just go from the last comma + 1 
   'to the end of the line 
   If Count < 5 Then 
      CommaPos = InStr(StartPos, FileLine, &quot;,&quot;) 
      CurVar = Mid(FileLine, StartPos, CommaPos - StartPos - 1) 
      StartPos = CommaPos + 1 
   Else 
      CurVar = Mid(FileLine, CommaPos + 1) 
   End If 
   'now put the section of the fileline into the correct variable
   Select Case Count 
      Case 1 
         LastName = Trim(CurVar) 
      Case 2 
         FirstName = Trim(CurVar) 
      Case 3 
         Age = Val(Trim(CurVar)) 
      Case 4 
         Salary = Val(Trim(CurVar)) 
      Case 5
         Title = Trim(CurVar) 
   End Select 
         
Next Count 

Wend 

ReadAll:
Close #1 
MsgBox &quot;FirstName = '&quot; & FirstName & &quot;'&quot;
MsgBox &quot;LastName = '&quot; & LastName & &quot;'&quot;
MsgBox &quot;Age = '&quot; & Age & &quot;'&quot;
MsgBox &quot;Salary = '&quot; & Salary & &quot;'&quot;
MsgBox &quot;Title = '&quot; & Title & &quot;'&quot;

I hope this helps. You can just paste this code into the event for a command button and paste the file line example into
notepad and save it as test.csv. Then just run the project with F8 and step through it to see exactly how it works. Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
Alternatively you could use the FileSystemObject and the Split function. For example:

Code:
' Assuming the file contained the following:
' Smith, John, 45, 55000, Database Administrator

Dim FSO As New FileSystemObject
Dim tsInput As TextStream
Dim varData As Variant

set tsInput = FSO.OpenTextFile(&quot;<file name>&quot;,ForReading)

varData = Split(tsInput.ReadAll,&quot;,&quot;)

' The information separated by the commands is now held in the 5 element array varData.

' Code here to insert into an Oracle database.

If this process was to be used for a file with multiple lines you would need to loop through each line in the file, possible using
Code:
 While not tsInput.AtEndOfStream
, then within the loop substitute ReadAll with ReadLine.

Hope this is a viable alternative for you. John Whyte
jwhyte@skipton.co.uk
 
You could also take a look at some of the posts to my thread at
thread222-55379 for a few more ideas on the problem...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top