Link to home
Start Free TrialLog in
Avatar of Mean
Mean

asked on

Separate strings from a file

I need to read in a text file
containing 4 strings,
each of those strings need to be placed
in 4 separate labels, how do i do that?
Avatar of Mean
Mean

ASKER

Adjusted points from 50 to 70
how are the string separated in your text file?
with a file that looked like this i wrote this up

File ->  
string1 string2 string3 string4

Code ->
1) Add a label to the form and make its index property 0, then paste this code:


Option Explicit

Private Sub Form_Load()
    Const ForReading = 1
    Dim filesys, txtStream As Object
    Dim fileStr As String
    Dim arr As Variant
    Dim i As Integer
    For i = 1 To 3
        Load Label1(i)
        Label1(i).Left = Label1(i - 1).Left
        Label1(i).Top = Label1(i - 1).Top + Label1(i).Height
        Label1(i).Visible = True
    Next
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set txtStream = filesys.OpenTextFile("C:\mypath\myfile.txt", _
        ForReading)
    fileStr = txtStream.ReadAll
    arr = Split(fileStr)
    For i = 0 To Label1.ubound
        Label1(i).Caption = arr(i)
    Next
End Sub


Open "Z:\read.txt" For Input As #1
Dim line(5)
For x = 1 To 5
 Line Input #1, line(x)
 Next
Label1.Caption = line(1)
Label2.Caption = line(2)
Label3.Caption = line(3)
Label4.Caption = line(4)
Close
Here hope this might help you out I used an Array of Five Labels

Dim FileNum As Integer
Dim Counter As Integer
Dim StrBuffer As String
On Error Resume Next
FileNum = FreeFile
 Counter = 0
Open "C:\test.txt" For Input As FileNum
 Do While Not EOF(FileNum)
 Input #FileNum, StrBuffer
 Counter = Counter + 1
 Label1(Counter).Caption = StrBuffer
  Loop
  Close #FileNum
Avatar of Mean

ASKER

Its part of a quiz game,
containing 4 questions
The file would look like this:
Q#3,Text1,Text2,Text3,Text4
or something similar..

Where number 3 is the right question
in the row

Still figuring out a way how
to give the questions a number
so i can use 1 textfile
and doing a random choice
of all the question's in the file

sorry for any spelling mistakes (if any)
i'm dutch..

Thanks for your comments..
use the exact same code i posted above except change these lines:


arr = Split(fileStr, ",")
For i = 0 To Label1.ubound
    Label1(i).Caption = arr(i + 1)
Next
Avatar of Mean

ASKER

Thanks you very much it helped me a lot,
just what it needs to do, nothing more.
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America 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