Results 1 to 8 of 8

Thread: [RESOLVED] how to get data from a specific line?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Resolved [RESOLVED] how to get data from a specific line?

    I want to get data from a file? how to put data in line 2 to a textbox, data in line 3 to a textbox and continue..........

    i using code below but come an error "type mismatch" and ReDim Preserve MyArray(i) was highlighted.

    Dim temp As String, MyArray As Variant
    Open "filename.txt" For Input As #1
    While Not EOF(1)
    ReDim Preserve MyArray(i)
    Line Input #1, MyArray(i)
    i = i + 1
    Wend
    Close

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: how to get data from a specific line?

    Moved from FAQ forum

    Welcome to VBForums

    The problem is that you did not declare it as an array, you only declared it as a variant. You should have it like this instead (but preferably using a data type other than Variant):
    VB Code:
    1. Dim temp As String, MyArray() As Variant

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: how to get data from a specific line?

    i have change the code as below.But,still got problem.An error "subscript out of range" occurred & {Msgbox "" & MyArray(2)}was highlight.

    Dim temp As String, MyArray() As Variant
    Open "C:\MY3C0540.daf" For Input As #1
    While Not EOF(1)
    ReDim Preserve MyArray(i)
    Line Input #1, MyArray(i)
    i = i + 1
    MsgBox "" & MyArray(2)
    Wend
    Close

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: how to get data from a specific line?

    depending on what you're doing - you'll probably find it easier to load the whole file in at once and then split it:
    VB Code:
    1. Dim MyArray() [B]As String[/B]
    2. Open "C:\MY3C0540.daf" For Input As #1
    3.     MyArray = Split(Input(LOF(1), #1), vbCrLf)
    4. Close #1
    5.  
    6. MsgBox MyArray(2) 'the third line in the file (because the array is 0-based)

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: how to get data from a specific line?

    Quote Originally Posted by junlo
    i have change the code as below.But,still got problem.An error "subscript out of range" occurred & {Msgbox "" & MyArray(2)}was highlight.

    Dim temp As String, MyArray() As Variant
    Open "C:\MY3C0540.daf" For Input As #1
    While Not EOF(1)
    ReDim Preserve MyArray(i)
    Line Input #1, MyArray(i)
    i = i + 1
    MsgBox "" & MyArray(2)
    Wend
    Close
    MyArray(2) would be the 3rd line in the file. Are there at least 3 lines?

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: how to get data from a specific line?

    Quote Originally Posted by bushmobile
    depending on what you're doing - you'll probably find it easier to load the whole file in at once and then split it:
    VB Code:
    1. Dim MyArray() [B]As String[/B]
    2. Open "C:\MY3C0540.daf" For Input As #1
    3.     MyArray = Split(Input(LOF(1), #1), vbCrLf)
    4. Close #1
    5.  
    6. MsgBox MyArray(2) 'the third line in the file (because the array is 0-based)
    This would be a faster method than loading it line by line. However, if the text file is really large than I would do it line by line.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Function GetLineFromFile(ByVal FilePath As String, ByVal LineNumber As Long) As String
    4.    
    5.     Dim lonFF As Long, lonLine As Long
    6.     Dim strLine As String
    7.    
    8.     lonFF = FreeFile
    9.    
    10.     Open FilePath For Input As #lonFF
    11.        
    12.         Do While Not EOF(lonFF)
    13.             Line Input #lonFF, strLine
    14.             lonLine = lonLine + 1
    15.            
    16.             If lonLine = LineNumber Then
    17.                 GetLineFromFile = strLine
    18.                 Exit Do
    19.             End If
    20.        
    21.         Loop
    22.    
    23.     Close #lonFF
    24.    
    25. End Function
    26.  
    27. Private Sub Form_Load()
    28.     Text1.Text = GetLineFromFile("C:\MY3C0540.daf", 2)
    29. End Sub

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: how to get data from a specific line?

    Quote Originally Posted by MartinLiss
    MyArray(2) would be the 3rd line in the file. Are there at least 3 lines?
    Not right after the first line was read, Martin. The error will occur until the 3rd line is read and the array has 3 elements. (And this is code with no Option Explicit, too.)
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  8. #8

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