Obviously if you do not want the same sequence every time you run your program, and if you do not want to get a new sequence every time you open your form2, you will have to save the random sequence of strings the first time you enter form2.
The only alternative is to seed VB's Randomiser yourself and reuse the same value each subsequent time.
This will allow you to keep the same sequence of numbers without storing them in memory (only storing the seed number). This will keep the same sequence until the next time you restart the application. If this is what you want, then use something like the following code.
Code:' form1 has a single command button Option Explicit Private Sub Command1_Click() Form2.Show End SubHope it helpsCode:' form2 has a single command button and a list box Option Explicit Private Sub Command1_Click() Unload Me End Sub Private Sub Form_Load() Static seed As Double ' note that we use the timer for a seed value which is ' the same as VB does if Randomize is called with ' no parameter. If seed = 0 Then seed = Timer Dim c As Integer Dim d As Integer Dim lenString As Integer Dim tmp As String ' little known 'trick' with rnd and randomize keywords. ' call rnd with any negative parameter Rnd (-1) ' then call randomize with any positive seed Randomize seed ' and the sequence of random numbers will always be the ' same. We ensure the sequence is different each time ' the app is run by using the timer as out seed. If we ' use a constant value instead, then the "random" sequence ' will forever be the same ' generate 10 to 30 strings For c = 1 To Rnd() * 20 + 10 tmp = "" ' get a random length between 5 and 15 chars lenString = Rnd() * 10 + 5 For d = 1 To lenString ' get a random char form A to Z tmp = tmp & Chr(Rnd() * 25 + Asc("A")) Next ' add string to listbox List1.AddItem tmp ' print string also Debug.Print c, tmp Next End Sub




Reply With Quote