Link to home
Start Free TrialLog in
Avatar of Carol2009
Carol2009

asked on

Import .txt file into vb6 project

Hello experts, is there any simple code to import text files delimited by TAB into a listbox in my visual basic project?

Lets say my .txt file contains...
========demo.txt==========
Australia [TAB] Greece [TAB] usa [TAB]
=========================

I need to retrieve Australia, greece and usa into a listbox seperate by line.
Avatar of HyperBPP
HyperBPP

ASKER CERTIFIED SOLUTION
Avatar of Rahul Gupta
Rahul Gupta
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is the entire file one long string delimited by tabs, or is it individual lines delimited by tabs and followed by a return?

If it is one continuous stream with no returns, then

DIm F as integer, S as String, L as Long, P As Integer, N as Integer
Redim MyArray(0) As String

F = Freefile
Open "demo.txt" for Binary as F
L = LOF(F)
S = Space$(L) ' This step may not be necessary
Get #F, L, S
Close F
P = Instr(S,chr$(9))
Do While P>0
    N=N+1
    Redim Preserve MyArray(N)
    MyArray(N) = Left$(S, P-1)
    S = Mid$(S,P+1)
    P = Instr(S,chr$(9))
Loop
 N=N+1
 Redim Preserve MyArray(N)
 MyArray(N) = S

If the file consists of individual strings followed by returns

DIm F as integer, S as String, L as Long, P As Integer, N as Integer
Redim MyArray(0) As String

F = Freefile
Open "demo.txt" for Input as F
Do Until EOF(F)
    Line Input #F, S
    P = Instr(S,chr$(9))
    Do While P>0
         N=N+1
         Redim Preserve MyArray(N)
         MyArray(N) = Left$(S, P-1)
         S = Mid$(S,P+1)
         P = Instr(S,chr$(9))
        Loop
        N=N+1
       Redim Preserve MyArray(N)
       MyArray(N) = S
Loop
Close F