Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomise list of words.. 2

Status
Not open for further replies.

basepointdesignz

Programmer
Jul 23, 2002
566
0
0
GB
Hi,

I'm writing a program that i want to generate random 2 word names from a list..

I have a string array of words and a userform with a 'generate name' button and 2 Labels, with which the generated words for name1 and name2 will be the Label's caption..

What's the easiest way to randomise these string values so that everytime i click the 'generate name' button, the two label's captions will state the 2 word name, which of course will be different each time i press the button..

Also, if i had a list of words in a .txt file, can i access this for the list of words to be used in the generator?



Cheers,

Paul
basepointdesignzltd..
XP Pro..
Pentium Core 2 Q6600 Quad Core
ASUS P5N-E SLI Motherboard
4GB DDR2 RAM
2 x SLI NVIDIA 8500GT SLi 1024MB DDR2 PCI-Express Graphics Cards
 

Look up Rnd in VB help.

You will get all what you need.


Have fun.

---- Andy
 
Ok, got that working, but in answer to my second question,

...if i had a list of words in a .txt file, can i access them as the list of strings required?

if so, i would need to redim my array to suit the number of lines in the txt file (would be a different word on each line of course)

i think i've got the opening up the txt file thing sorted as i've done it before, but how can i (if i can of course) count the number of lines in the txt file, so i can re-array my variable?

Cheers,

Paul
basepointdesignzltd..
XP Pro..
Pentium Core 2 Q6600 Quad Core
ASUS P5N-E SLI Motherboard
4GB DDR2 RAM
2 x SLI NVIDIA 8500GT SLi 1024MB DDR2 PCI-Express Graphics Cards
 
Why do that? Read the entire contents of the text file into a string variable and then use the split function to split the entries into a dynamic array.

Code:
Private Sub Form_Load()
Dim fso As FileSystemObject
Dim InStream As TextStream
Dim aryTemp() As String
Dim InputData As String

Set fso = New FileSystemObject
Set InStream = fso.OpenTextFile("C:\YOURTEXTFILE.TXT", ForReading)
InputData = InStream.ReadAll
aryTemp = Split(InputData, vbCrLf)
InStream.Close
Set InStream = Nothing
Set fso = Nothing
' Randomize your array or whatever else you want to do
End Sub

Swi
 

Or, you can use VB's Open statement:
Code:
Private Sub Form_Load()
Dim aryTemp() As String
Dim strInputData As String
dim strTextLine As String

Open "C:\TextFile.txt" For Input As #1
Do While Not EOF(1)             [green]' Loop until end of file.[/green]
   Line Input #1, strTextLine   [green]' Read line into variable.[/green]
   strInputData = strInputData & strTextLine & "*"
Loop
Close #1

aryTemp = Split(strInputData, "*")

Have fun.

---- Andy
 
Hi,

Well, begged and borrowed Andrzejek's code and it works fine for me, thanks to both of you and to both of you , a star..

Cheers,

Paul
basepointdesignzltd..
XP Pro..
Pentium Core 2 Q6600 Quad Core
ASUS P5N-E SLI Motherboard
4GB DDR2 RAM
2 x SLI NVIDIA 8500GT SLi 1024MB DDR2 PCI-Express Graphics Cards
 
If you like the non-FSO version better you could modify Andrzejek's code a bit to read the entire file at once.

Code:
Private Sub Form_Load()
Dim aryTemp() As String
Dim strInputData As String
Dim strTextLine As String
Open "C:\TextFile.txt" For Input As #1
strInputData = Input$(LOF(1), 1)
Close #1
aryTemp = Split(strInputData, vbCrLf)
End Sub

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top