Random File

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Random File

Post by D Willett »

Hi

I'm just going over some old code from ages ago and came across the below code to show a random file from a specific folder.
I didn't really use it but now have a use for it. I lost my notes as this was from the old Woody's site and cannot for the life of me remember how to use the code.
The module is straight forward as it is already in my modUtils. I can't remember where to put the calling code or timer settings etc.
Would someone mind looking and advising? I know who wrote it but seeing a previous post would be rude to direct the request directly.

Posted 2006-10-28 02:24
Put this function in a module:

Code: Select all

Function SelectRandomFile( _
ByVal strFolder As String, _
ByVal strExt As String) As String
Dim strFile As String
Dim intCount As Integer
Dim arr() As String
Dim i As Integer

On Error GoTo ErrHandler

' Trailing backslash is obligatory
If Not Right(strFolder, 1) = "" Then
strFolder = strFolder & ""
End If
strFile = Dir(strFolder & "*." & strExt)
Do While Not strFile = ""
intCount = intCount + 1
ReDim Preserve arr(1 To intCount)
arr(intCount) = strFile
strFile = Dir
Loop

If intCount = 0 Then
Exit Function
End If

Randomize
i = Int(1 + Rnd * intCount)
strFile = arr(i)
SelectRandomFile = strFolder & strFile

ExitHandler:
Erase arr
Exit Function

ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Function

Use like this:

Me.AcrobatPath = SelectRandomFile("L:MMPDFUtilities", "pdf")
Me.Pdf1.LoadFile Me.AcrobatPath 
Regards
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78600
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Random File

Post by HansV »

No idea who wrote this... :innocent: :liar:

You can call the code wherever you need it: from the On Click event of a command button, or from the On Timer event of a form in Access, or...

You specify a folder path and an extension, and the function will return the full path of a random file with the specified extension in the specified folder. It's up to you as programmer to do something with that string: open the file, or delete it, or whatever you want.

For example, on an Access form with an image control imgControl and a command button cmdShowPicture:

Code: Select all

Private Sub cmdShowPicture()
  Dim strFullname As String
  strFullName = SelectRandomFile("C:\Images", "jpg")
  If strFullname <> "" Then
    Me.imgControl.Picture = strFullname
  End If
End Sub
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Random File

Post by D Willett »

lol.................
Cheers ...

Dave.

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Random File

Post by Don Wells »

[quote="D Willett"][/quote]
Your original post.
Regards
Don

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Random File

Post by D Willett »

Hans

How would I capture user inactivity? I wouldn't want this firing off if a user is using the mouse, keyboard etc, only if inactive for a few minutes..
Cheers ...

Dave.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Random File

Post by D Willett »

Cheers Don, exactly what I found .. but can't find my notes or the inactivity projct..

Cheers
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78600
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Random File

Post by HansV »

Perhaps you can find something useful here: How to automatically close app after x time if no mouse/kb events.
Best wishes,
Hans