Aaron Young
Nov 19th, 1999, 12:20 AM
If the file was a Random Access File, (All Lines/Records are the Same Length) then you could use the Open.. For Random Access Read Statement and the Get Method to randomly pick a line from the File.
Otherwise, you will need to read the whole file in, parse it into it's individual lines, then seelct one at random, eg.
Dim iFile As Integer
Dim iPos As Long
Dim sFile As String
dim iLines As Integer
Dim aLines() As String
iFile = FreeFile
Open "C:\MyFile.txt" For Input As iFile
sFile = Input(LOF(iFile), iFile)
Close iFile
While Len(sFile)
iPos = Instr(sFile, vbCrLf)
If iPos = 0 Then iPos = Len(sFile) + 1
ReDim Preserve aLines(iLines)
aLines(iLines) = Left(sFile, iPos - 1)
sFile = Mid$(sFile, iPos + 2)
iLines = iLines + 1
Wend
Randomize Timer
Msgbox aLines(Int(Rnd * Ubound(aLines)))
------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net
Otherwise, you will need to read the whole file in, parse it into it's individual lines, then seelct one at random, eg.
Dim iFile As Integer
Dim iPos As Long
Dim sFile As String
dim iLines As Integer
Dim aLines() As String
iFile = FreeFile
Open "C:\MyFile.txt" For Input As iFile
sFile = Input(LOF(iFile), iFile)
Close iFile
While Len(sFile)
iPos = Instr(sFile, vbCrLf)
If iPos = 0 Then iPos = Len(sFile) + 1
ReDim Preserve aLines(iLines)
aLines(iLines) = Left(sFile, iPos - 1)
sFile = Mid$(sFile, iPos + 2)
iLines = iLines + 1
Wend
Randomize Timer
Msgbox aLines(Int(Rnd * Ubound(aLines)))
------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net