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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why isn't this random file function working???

Status
Not open for further replies.

JESTAR

Programmer
Feb 13, 2002
213
GB
It used to work. Now it doesn't. I can't see why, as the code hasn't been changed.

[tt]
Public Function RandomFile()
Dim Rand As Integer
Randomize
Rand = Int(((FileList.ListCount) * Rnd) + 0)
FileList.ListIndex = Rand
Address = DirList.Path & "\" & FileList.FileName
End Function[/tt]

When the Random button is clicked, it should run that function, which it does... then carry on from there. A random integer is selected between 0 and FileList.ListCount. Then the ListIndex of the filelistbox(FileList) is set to the random number. Address is an address bar.

I get the error:

Run time error 381
Invalid property array index


... on FileList.ListIndex = Rand. It's saying that FileList.ListIndex = 0, and that Rand = whatever random number it generated. But nothing happens.
 
Hi Jestar there are couple of thoughts I have going here.
First is this, has the timer been corrupted or deleted? If there is no timer control then the "Randomize" function can not initialize, if I understand the functionality of the control propeprly. Next, does the file structure in which you are testing your code against actually have the necessary files or any files with in it. Hidden files are not indexed upon.
HTH - AntonV
 


Hi AntonV:

The timer that the Randomize function uses is Windows System Timer, not a timer control that one adds to the VB program.

Cassie
 


Hi Jestar:

Suggestion: Put a breakpoint on the FileList.ListIndex = Rand line and check to see what value Rand has. Knowing what value is being assigned to Rand, one can then narrow the hunt for the bug.

By the way, your statement for Rand should return values in the range 0 to (FileList.ListCount - 1), which is what you really want.

Cassie


 

Hi Jestar:

One thought comes to mind:

If the FileList is empty, then you will get an error. Trying to set FileList.ListIndex to anything when FileList is empty will generate an error.

Cassie
 
Thanks C_2k2,
I don't use that funtion at all and it was evident in my response. I was familiar the controlled version and not playing off the internal timer version.


So much time, so little time, what to do, what to do????
That is our question in this life now isn't it?

AntonV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top