-
Jun 3rd, 2013, 06:05 AM
#1
Thread Starter
Hyperactive Member
Read a Text file in to Multiable Text Boxs
Hi All
Wonder if one you could help with a little project I am doing
Basically I want to use a filelistbox and select a text file which then reads the lines in the text file and but them lines in to text boxes
line 1 of text file "Data1" and put this into textbox 1
line 2 of text file "Data2" and put this into textbox 2
line 3 of text file "Data3" and put this into textbox 3
and so on
KInd Regards
steve
Last edited by sbarber007; Jun 3rd, 2013 at 07:41 AM.
-
Jun 3rd, 2013, 07:52 AM
#2
Re: Read a Text file in to Multiable Text Boxs
Try this
Code:
Option Explicit
Private Sub Command1_Click()
Dim j As Long
Dim l() As String
l = Split(ReadFile("C:\myfile.txt"), vbNewLine)
Combo1.Clear
For j = 0 To UBound(l)
Combo1.AddItem l(j)
Next
End Sub
Private Sub Combo1_Click()
Dim f() As String
f = Split(Combo1.Text, ",")
Text1.Text = f(0)
Text2.Text = f(1)
Text3.Text = f(2)
End Sub
Private Function ReadFile(ByVal strFile As String) As String
Dim intFN As Integer
Dim strIN As String
On Error GoTo FileReadingError
intFN = FreeFile
Open strFile For Input As #intFN
strIN = Input(LOF(intFN), intFN)
Close #intFN
ReadFile = strIN
Exit Function
FileReadingError:
Close #intFN
MsgBox "File input error!"
ReadFile = vbNullString
End Function
-
Jun 4th, 2013, 07:00 AM
#3
Re: Read a Text file in to Multiable Text Boxs
Originally Posted by sbarber007
...select a text file...
line 1 of text file "Data1" and put this into textbox 1
line 2 of text file "Data2" and put this into textbox 2
line 3 of text file "Data3" and put this into textbox 3
Just as a point of clarification...your question states you wish to read "A" text file, as in one text file, but your example uses three different file names.
Are you reading one text file or three textfiles?
-
Jun 4th, 2013, 08:03 AM
#4
Thread Starter
Hyperactive Member
Re: Read a Text file in to Multiable Text Boxs
@4x2y
I have change my first post.
The script works great you sent me but want to try the way it reads in my first post now "if you got any idea of a script for that" so I can see what works best
Thank You for your help
@Hack
data1 is on line1 of the text file
data2 is on line2 of the text file
data3 is on line3 of the text file
only one text file
Regards Steve
Last edited by sbarber007; Jun 4th, 2013 at 08:09 AM.
-
Jun 4th, 2013, 08:08 AM
#5
Re: Read a Text file in to Multiable Text Boxs
Thank you!
4x2y's solution should work just fine for you.
-
Jun 4th, 2013, 08:41 AM
#6
Thread Starter
Hyperactive Member
Re: Read a Text file in to Multiable Text Boxs
@hack
4x2y version does work great but for users friendly I would like my first post because the user can select the text file using filelistbox and go by the name of filename but using combox 1 is harder to identify from the line in which one to select
regards steve
-
Jun 4th, 2013, 08:59 AM
#7
Re: Read a Text file in to Multiable Text Boxs
@sbarber007
At first you asked for using Combo box but you have edited your post later.
try this, add File1, List1 and 3 Text box
Code:
Option Explicit
Private Const mstrMyFolder As String = "c:\my_folder\" ' replace with actual path.
Private Sub Form_Load()
File1.Pattern = "*.txt"
File1.Path = mstrMyFolder
End Sub
Private Sub File1_Click()
Dim j As Long
Dim l() As String
l = Split(ReadFile(mstrMyFolder & File1.List(File1.ListIndex)), vbNewLine)
List1.Clear
For j = 0 To UBound(l)
List1.AddItem l(j)
Next
End Sub
Private Sub List1_Click()
Dim f() As String
f = Split(List1.List(List1.ListIndex)), ",")
Text1.Text = f(0)
Text2.Text = f(1)
Text3.Text = f(2)
End Sub
Private Function ReadFile(ByVal strFile As String) As String
Dim intFN As Integer
Dim strIN As String
On Error GoTo FileReadingError
intFN = FreeFile
Open strFile For Input As #intFN
strIN = Input(LOF(intFN), intFN)
Close #intFN
ReadFile = strIN
Exit Function
FileReadingError:
Close #intFN
MsgBox "File input error!"
ReadFile = vbNullString
End Function
Last edited by 4x2y; Jun 4th, 2013 at 09:11 AM.
Reason: Bug fixed
-
Jun 4th, 2013, 09:49 AM
#8
Thread Starter
Hyperactive Member
Re: Read a Text file in to Multiable Text Boxs
@4x2y
Thank you for that script when I copy and paste the whole script this line becomes red:- f = Split(List1.List(List1.ListIndex)), ",")
are these lines correct for entering a path
File1.Path = "c:\users\steve"
l = Split(ReadFile("c:\users\steve" & File1.List(File1.ListIndex)), vbNewLine)
Kind Regards
steve
-
Jun 4th, 2013, 09:52 AM
#9
Re: Read a Text file in to Multiable Text Boxs
Replace "c:\users\steve" with "c:\users\steve\"
-
Jun 4th, 2013, 09:54 AM
#10
Re: Read a Text file in to Multiable Text Boxs
f = Split(List1.List(List1.ListIndex)), ",") should be
f = Split(List1.List(List1.ListIndex), ",")
-
Jun 4th, 2013, 10:04 AM
#11
Re: Read a Text file in to Multiable Text Boxs
@SamOscarBrown
good eye
-
Jun 4th, 2013, 11:08 AM
#12
Thread Starter
Hyperactive Member
Re: Read a Text file in to Multiable Text Boxs
@4x2y
Sorry to be a pain is possible to do a way with the list1.box and just put the lines in the text file straight to the textbox's when clicking on the file in filelistbox so need for commas just what's on each line
regards
steve
-
Jun 4th, 2013, 11:19 AM
#13
Re: Read a Text file in to Multiable Text Boxs
Do you want to pick up only the first line and ignore the rest?
try this
Code:
Private Sub File1_Click()
Dim j As Long
Dim l() As String
Dim f() As String
l = Split(ReadFile(mstrMyFolder & File1.List(File1.ListIndex)), vbNewLine)
f = Split(l(0), ",")' get the first line, you must do check to ensure l(0) contains valid inputs.
Text1.Text = f(0)
Text2.Text = f(1)
Text3.Text = f(2)
End Sub
-
Jun 4th, 2013, 11:53 AM
#14
Thread Starter
Hyperactive Member
Re: Read a Text file in to Multiable Text Boxs
@4x2y
I want all the lines in the text file there is 8 lines that need to go
to 8 texts box's when clicking the text file in filelistbox I.e
Line1 goes to text box 1
Line2 goes to text box 2
Line3 goes to text box 3
And so on up to 8
Regards
Steve
Originally Posted by 4x2y
Do you want to pick up only the first line and ignore the rest?
try this
Code:
Private Sub File1_Click()
Dim j As Long
Dim l() As String
Dim f() As String
l = Split(ReadFile(mstrMyFolder & File1.List(File1.ListIndex)), vbNewLine)
f = Split(l(0), ",")' get the first line, you must do check to ensure l(0) contains valid inputs.
Text1.Text = f(0)
Text2.Text = f(1)
Text3.Text = f(2)
End Sub
-
Jun 4th, 2013, 12:15 PM
#15
Banned
Re: Read a Text file in to Multiable Text Boxs
from what i understand from first post he wants to put each line to each textbox.
-
Jun 4th, 2013, 04:08 PM
#16
Thread Starter
Hyperactive Member
Re: Read a Text file in to Multiable Text Boxs
@4x2y
That works Great Thank You
and Thank you to every one else
Last edited by sbarber007; Jun 5th, 2013 at 10:38 AM.
-
Jun 4th, 2013, 05:12 PM
#17
Re: Read a Text file in to Multiable Text Boxs
Originally Posted by sbarber007
@HACKS
That works Great Thank You
Where is the code of Hack that works great?
The following should work
Code:
Private Sub File1_Click()
Dim j As Long
Dim l() As String
l = Split(ReadFile(mstrMyFolder & File1.List(File1.ListIndex)), vbNewLine)
Text1.Text = l(0)
Text2.Text = l(1)
Text3.Text = l(2)
' and so on
End Sub
Last edited by 4x2y; Jun 4th, 2013 at 06:04 PM.
-
Jun 5th, 2013, 10:37 AM
#18
Thread Starter
Hyperactive Member
Re: Read a Text file in to Multiable Text Boxs
@4x2y
Sorry I meant to put you I changed it thanks again
Reguards
Steve
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|