|
-
Sep 10th, 2001, 12:11 PM
#1
Thread Starter
Lively Member
Random Words
Hi, i really need to get a random word from a txt file. my txt file looks a little somthing like this...
line one
another line
and other
.....etc etc
i had a look on here andi found this....
Code:
'Load the file into vWords
Dim vWords() As String
Dim iCount As Integer
iCount = 0
Open "Dictionary.txt" For Input As #1
Do Until EOF(1)
Line Input #1, tmp
Redim vWords(iCount)
vWords(iCount) = tmp
iCount = iCount + 1
Loop
Close #1
''Get a random value
Dim I As Integer
I = Int(Rnd * UBound(vWords)) + 1
MsgBox "The Random word is: " & vWord(I)
By Megtron
and i think its what i need but it dont work for me
wen i placed it in Form_Load and hit run it highlights the very last word on the last line (the vWord) and says..
Complie Error:
Sub or Function not defined
maybe its because im using VB5? i dunno knowing me its prob somthing really silly
thanx for reading
-
Sep 10th, 2001, 12:22 PM
#2
Looks like a typo.
There is no such thing vWord defined
Change the line to this:
MsgBox "The Random word is: " & vWords(I)
-Lou
-
Sep 10th, 2001, 07:42 PM
#3
Thread Starter
Lively Member
-
Sep 10th, 2001, 08:04 PM
#4
Fanatic Member
Hmm I've done this with a few things in my game. How about this:
VB Code:
Dim rndWords() As String
Dim sFile As String
Dim FF As Integer
Randomize
FF = FreeFile()
Open "C:\Words.Txt" For Input As #FF
sFile = Input$(LOF (FF), FF)
Close #FF
rndWords = Split(sFile, vbCrLf)
MsgBox "The Random Word is: " & rndWords(int(rnd * UBound(rndWords)))
-
Sep 10th, 2001, 08:15 PM
#5
Thread Starter
Lively Member
Thanx but is there anyway of doing it with out the spilt funcation? as im using VB5
-
Sep 10th, 2001, 08:42 PM
#6
Fanatic Member
Sure, try this:
VB Code:
Private Sub Form_Load()
Dim Fred() As String
Dim X As Integer
Fred = vb5Split("this" & vbCrLf & "is" & vbCrLf & "only" & vbCrLf & "a" & vbCrLf & "test", vbCrLf)
For X = 0 To UBound(Fred)
MsgBox Fred(X)
Next X
End Sub
Public Function vb5Split(ByVal sText As String, sDelim As String) As String()
Dim sTemp() As String
Dim X As Integer
X = -1
sText = sText & sDelim
Do While InStr(1, sText, sDelim) > 0
X = X + 1
ReDim Preserve sTemp(X)
sTemp(X) = Left(sText, InStr(1, sText, sDelim) - 1)
sText = Mid(sText, Len(sTemp(X)) + Len(sDelim) + 1)
Loop
vb5Split = sTemp
End Function
There you have it a split function. The Form_Load Junk is an example of how to use it. Have fun.
-
Sep 10th, 2001, 08:57 PM
#7
Thread Starter
Lively Member
-
Sep 10th, 2001, 09:01 PM
#8
Fanatic Member
Nop A Troblem
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
|