Link to home
Start Free TrialLog in
Avatar of hcougar
hcougar

asked on

Text in a Text Box

What I would like to do is show data from a txt file in a text box.  But that's not all.  I would like to look at the previous line or the next line of the text file in the text box.  This txt file has multiple lines of text and my project has multiple text boxes, so I'm not only bringing in data to one text box.  Here is a sample of a text file:

12345   SMITH   JOE      B
12345   EVANS   BARBARA  C

So, I would like to show each of these fileds in a different txt box and be able to go from the BOF to the EOF by looking at the data in the text boxes.  Thanks in advance for the help.
Avatar of Marine
Marine

i don't undersanad your questin really. But wouldn't it be better to load this data from text file to a flexgrid control ? i think it would be a better solution for you. Here is the code that would split the file and populate the msFelxGrid.


Dim str As String, sArray() As String, i
Open "C:\windows\desktop\val.txt" For Input As #2
msGrid.Rows = 1
i = 0
Do Until EOF(2)
    Input #2, str
    sArray = Split(str, "  ", -1, vbTextCompare)
    msGrid.AddItem sArray(0) & vbTab & sArray(1) & vbTab & sArray(2) & vbTab & sArray(3)
    i = i + 1
Loop
Close #2

'sarray=Split(str,"",-1,vbtextcompare)
note the second argument is a delemiter. You specify how the fields are separated. If the fields in a file sperated by a coma or an <!> or any other cherecter write it there. Best of luck
Avatar of hcougar

ASKER

Thanks for the input Marine, but that won't work for what I need.  I really need the text to be in text boxes.  All I want to do is put the first column in a text box, the second column in a different text box, etc., etc..  
this text file looks like this...
 
12345 SMITH JOE B
12345 EVANS BARBARA C

this just gives you an idea. use split function for your problem. that way, you can extract a certain field using a certain element in the array.

Private Sub Command1_Click()
    Dim s1 As String
    Dim tmparray() As String
    Open "C:\test.txt" For Input As #1
    While Not EOF(1)
    Line Input #1, s1
    Wend
    tmparray = Split(s1, " ")
    Text1.Text = tmparray(0)
    Text2.Text = tmparray(1)
    Text3.Text = tmparray(2)
    Text4.Text = tmparray(3)
    Close #1
End Sub
I'll post you another answer but using grid is just better its still my opinion no more extra text boxes cause every column would match the column in your text file.
Avatar of hcougar

ASKER

Marine, I'll wait for your answer since you were the first to answer me.  Thanks.
Avatar of hcougar

ASKER

Ruchi, your code only picks up the first row.  I can't advance to the next row in the text file.
it'd be more easier if you use database, not flat file, for "going from the BOF to the EOF"
Avatar of hcougar

ASKER

I'll tell you, I would use a database but the only problem is that my file has a header and a footer in the file.  
Ok this wll work
Dim str As String, sArray() As String, i
Open "C:\windows\desktop\val.txt" For Input As #2
i = 0
Do Until EOF(2)
    Input #2, str
    i = i + 1
    sArray = Split(str, "  ", -1, vbTextCompare)
    Text1.SelText = sArray(0) & vbCrLf
    Text2.Text = sArray(1) & vbCrLf
Loop
Close #2

Just make srue that both text boxes are set to multi line =true and scroll bars are also availabel
forgot to change the second textbox text2.seltext instead of .text property i tested it worked just fine.
another thin forget the counter i had it in my own example doing somthing.
Avatar of hcougar

ASKER

Marine, it's only printing out the first record.  It doesn't show the other records.
Hmm that can't be true. I tested it and it worked fine. You have to make sure that the TextBox multiLine property is set to True and that ScrollBars are set to either both or vertical. If you want i can email my sample. It worked just fine. Just make sure you set these properties.
Avatar of hcougar

ASKER

Yeah, I set the properties and it only brings in the first line.  My email is jellis8538@aol.com
ASKER CERTIFIED SOLUTION
Avatar of Marine
Marine

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
Avatar of hcougar

ASKER

It wasn't quite what I was wanting but I at least have something to work with.  Thanks.