PDA

View Full Version : Random Strings


mcorn
10-04-2004, 03:21 PM
Hi,
I would like to try to code a program that contains a command button, label, and an image area. I would like to make the program draw a random text string from a text file or access database, display the string in the label area, and then display some kind of message in the image area.

I am trying to come up with a program that will pick a name for a drawing.

I found some code for random numbers as follows:

Private Sub Command1_Click()
Image1.Visible = false
Label1.Caption = Int(Rnd * 10)
If (Label1.Caption = "7") Then
Image1.Visible = True
End If
End Sub


Is there a way to make this scenario work with strings of text from a text file or an access database?

Thanks for any help!
MCorn

Whiteknight
10-05-2004, 09:09 AM
not that I know of for direct file access (nothing easy at least) but it would work for an array of strings that you would have to load before hand.

now, it's been a while, but if you have an array Strings[], you should be able to call

randnum = int(rand * STRMAX)
outputString = Strings[randnum]

(STRMAX is the length of the Strings[] array)
or something very similar. of course, i cant remember how to set up a string array, nor how to populate it, so you will need more help methinks.

suprize
02-18-2005, 05:00 PM
Dim iFile as Integer
iFile = FreeFile

Open "textfile.txt" For Binary As iFile
strData = Space$(LOF(iFile))
Get iFile,1,strData
Close iFile

Dim aStrings() as String
aStrings = Split(strData,vbCrLf)

Then to randomly pick a string you would get a random int and use it for the element id.

strRandom = aStrings(Int(Rnd * UBound(aStrings)))