Separate strings from a file

Posted on 2000-05-14
Medium Priority
Last Modified: 2010-05-02
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?
Question by:Mean

Author Comment

ID: 2808692
Adjusted points from 50 to 70
LVL 28

Expert Comment

ID: 2808702
how are the string separated in your text file?
LVL 28

Expert Comment

ID: 2808713
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
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set txtStream = filesys.OpenTextFile("C:\mypath\myfile.txt", _
    fileStr = txtStream.ReadAll
    arr = Split(fileStr)
    For i = 0 To Label1.ubound
        Label1(i).Caption = arr(i)
End Sub

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.


Expert Comment

ID: 2808736
Open "Z:\read.txt" For Input As #1
Dim line(5)
For x = 1 To 5
 Line Input #1, line(x)
Label1.Caption = line(1)
Label2.Caption = line(2)
Label3.Caption = line(3)
Label4.Caption = line(4)

Expert Comment

ID: 2808960
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
  Close #FileNum

Author Comment

ID: 2808970
Its part of a quiz game,
containing 4 questions
The file would look like this:
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..
LVL 28

Expert Comment

ID: 2808973
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)

Author Comment

ID: 2809010
Thanks you very much it helped me a lot,
just what it needs to do, nothing more.
LVL 28

Accepted Solution

AzraSound earned 280 total points
ID: 2809030
there is an "Accept comment as Answer" button you can click to accept an answer to this question to move it out of the "Questions waiting to be Answered" area

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

597 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