I have a file (20 Lines long), and I want to input one of the lines into a text box. How do I do that!
Printable View
I have a file (20 Lines long), and I want to input one of the lines into a text box. How do I do that!
play with that to get one lineCode:Dim MyChar
Open "TESTFILE" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
MyChar = Input(1, #1) ' Get one character.
Debug.Print MyChar ' Print to the Immediate window.
Loop
Close #1 ' Close file.
Code:Dim MyStringOpen "TESTFILE" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Input #1, MyString' Read data into variables.
msgbox "Line is " & MyString
Loop
Close #1 ' Close file
What if I want a specific line, let's say line 6. I want to get everything on line six without getting any thing from the lines before that or after that.
Code:Dim MyChar
dim counter as integer
Dim MyStringOpen "TESTFILE" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
counter = counter + 1
if counter = 6 then Input #1, MyString' if 6 gives you line 7, type 5, so on and so forthLoop
Close #1 ' Close file
Check your code, that doesn't give me anything!!!
Code:Option Explicit
Private Sub Command1_Click()
Dim myLine As String, myFile As String
Dim intNum As Integer, myCounter As Integer
Dim i As Integer
intNum = FreeFile
myFile = "c:\my documents\myfile.txt"
Open myFile For Input As intNum
Do While Not EOF(intNum)
Line Input #intNum, myLine
i = i + 1
If i = 6 Then
MsgBox myLine
Exit Do
End If
Loop
Close intNum
End Sub
tested thatCode:Dim s As String
Dim c As Integer
Dim fileNum As Integer
fileNum = FreeFile
Open "f:\test.txt" For Input As #fileNum
Do Until EOF(1)
c = c + 1
If c = 7 Then
MsgBox s
Exit Sub
End If
Input #fileNum, s
Loop
Close #fileNum
n it works
What if i wanted it to choose a random line everytime
then randomize where the 7 is
instead put ((20 * Rnd) + 1)
that will give you random num between 1 and 20
Code:'change it here
dim myRandom as integer
Randomize
myRandom = int(Rnd*50)'50 could be any number
'
If i = myRandom Then
MsgBox myLine
Exit Do
End If
have a look at your other thread, should give you the lines in an array wherefrom you can pick any lines:
http://forums.vb-world.net/showthrea...threadid=28550