|
-
Oct 8th, 2006, 12:47 PM
#1
Thread Starter
Lively Member
[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
-
Oct 9th, 2006, 10:36 AM
#2
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:
Dim temp As String, MyArray() As Variant
-
Oct 9th, 2006, 11:06 AM
#3
Thread Starter
Lively Member
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
-
Oct 9th, 2006, 11:11 AM
#4
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:
Dim MyArray() [B]As String[/B]
Open "C:\MY3C0540.daf" For Input As #1
MyArray = Split(Input(LOF(1), #1), vbCrLf)
Close #1
MsgBox MyArray(2) 'the third line in the file (because the array is 0-based)
-
Oct 9th, 2006, 12:27 PM
#5
Re: how to get data from a specific line?
 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?
-
Oct 9th, 2006, 12:37 PM
#6
Re: how to get data from a specific line?
 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:
Dim MyArray() [B]As String[/B]
Open "C:\MY3C0540.daf" For Input As #1
MyArray = Split(Input(LOF(1), #1), vbCrLf)
Close #1
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:
Option Explicit
Private Function GetLineFromFile(ByVal FilePath As String, ByVal LineNumber As Long) As String
Dim lonFF As Long, lonLine As Long
Dim strLine As String
lonFF = FreeFile
Open FilePath For Input As #lonFF
Do While Not EOF(lonFF)
Line Input #lonFF, strLine
lonLine = lonLine + 1
If lonLine = LineNumber Then
GetLineFromFile = strLine
Exit Do
End If
Loop
Close #lonFF
End Function
Private Sub Form_Load()
Text1.Text = GetLineFromFile("C:\MY3C0540.daf", 2)
End Sub
-
Oct 9th, 2006, 02:24 PM
#7
Re: how to get data from a specific line?
 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
-
Oct 9th, 2006, 02:33 PM
#8
Re: how to get data from a specific line?
Oh, of course.
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
|