Hey does anyone know how when i calick a button on my form it will read a random line from a text file that i created and insert it in to a text box?
Thanks For Any Help.:p
Printable View
Hey does anyone know how when i calick a button on my form it will read a random line from a text file that i created and insert it in to a text box?
Thanks For Any Help.:p
Is your file small enough to fit it all into memory at the same time?
If so, load each line of your file into an array.
Generate a random number between 0 and the last element of the array.
That's your text!
If you don't want repeats, set the array element to null after you use it. (If you pick a null in future, guess again.)
If your file is too big to fit in memory, open it and get the length. Generate a random number less than the length of the file. Go there. Go forward until you hit a chr$(13) (or run off the end, in which case go to the start. Read from there util the next chr$(13) (or the end). That's your string.
Any bits of that you need the code for, or is the principle enough?
im afraid i didnt understand any of the above so if you could maybe show some examples that would be great
Thanks alot
Try this
VB Code:
Dim strFile() As String Dim sFile As String Open "MyFile" For Input As #1 sFile = Input(LOF(1), 1) strFile = Split(sFile, vbCrLf) Close #1 For I = 1 To 10 Randomize Timer nLine = Int(Rnd * UBound(sFile)) text1 = text1 & strFile(nLine) & vbCrLf Next I
OK Thanks alot i tried that but it say "expected array" on the word "UBound" could you tell me what this means and what i have to do to fix the problem please.
Try changing UBound(sFile) to UBound(strFile)
Hope it workz!
Thanx,
Mikelo2k
:):):)
Thanks alot for the help that all works except it reads a few lines instead of just one. Does anyone know how to let it know the different lines?
Although a lot of people don't like the filesystemobject I'd suggest you have a look into the fso's textstream-object. You will have much less to worry about and easier control. Need to have a reference to the microsoft scripting runtime which will use a few kb of diskspace and memory, but oh well neither is a very rare ressource.
Helger
Try this. It should be a lot more memory efficient that Megatron's example (esp with large files) as it only ever stores 1 line instead of the whole file
VB Code:
Dim intFF As Long Dim strLine As String Dim intLine As Long Dim intLines As Long Dim intCurrent As Long intFF = FreeFile intLines = 0 ' get number of lines in file Open "c:\winnt\win.ini" For Input As #intFF Do While Not EOF(intFF) Line Input #intFF, strLine intLines = intLines + 1 Loop 'now we know number of lines, we can generate a random number Randomize intLine = Int((intLines - 1 + 1) * Rnd + 1) 'we now want line# intline Seek #intFF, 1 intCurrent = 1 Do While intCurrent <= intLine Line Input #intFF, strLine intCurrent = intCurrent + 1 Loop Close #intFF MsgBox "Line # " & intLine & " contains: " & Chr(34) & strLine & Chr(34)
thanks for the code. it works.