Results 1 to 18 of 18

Thread: Read a Text file in to Multiable Text Boxs

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    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.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Read a Text file in to Multiable Text Boxs

    Quote Originally Posted by sbarber007 View Post
    ...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?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    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.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Read a Text file in to Multiable Text Boxs

    Thank you!

    4x2y's solution should work just fine for you.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    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

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    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

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Read a Text file in to Multiable Text Boxs

    Replace "c:\users\steve" with "c:\users\steve\"



  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Read a Text file in to Multiable Text Boxs

    f = Split(List1.List(List1.ListIndex)), ",") should be
    f = Split(List1.List(List1.ListIndex), ",")

  11. #11
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Read a Text file in to Multiable Text Boxs

    @SamOscarBrown
    good eye



  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    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

  13. #13
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    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

    Quote Originally Posted by 4x2y View Post
    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

  15. #15
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    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.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    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.

  17. #17
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Read a Text file in to Multiable Text Boxs

    Quote Originally Posted by sbarber007 View Post
    @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.



  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    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
  •  



Click Here to Expand Forum to Full Width