Follow Us:

Call us:  (306) 986-8888

Listing Files From A Directory In An Access Form

Listing Files From A Directory In An Access Form


How to create a box on a Microsoft Access form

A recent challenge i ran into was how to display files from a folder in an Access database form.  After a LOT of googling, I discovered there is not much out there on this topic, and what was there was overly complex and not what I wanted.  Hence my blog today.  The basic steps are this:

  1.  Create your form.
  2.  Insert a Web Control Object on the form.
  3.  Set the control source of the web control to the folder you are want to display.

Now, that sounds easy enough right!  noooo.  There are a few more changes I wanted to make:

  1.  Display the filenames in full list view, not icons horizontally across the screen
  2.  Make it so clicking a button would change the folder being displayed

To do this was tricky – you have to

  1. Create a button that sets the control source to the new path
  2. account for the = symbol and ” symbols of the path being included
  3. turn on a timer for a few milliseconds so the webcontrol can load the data
  4. set the webcontrol to change the view to list (or other options) after the timer is expired, turn off the timer.

This is NOT well documented, in fact – I didn’t find any references on line about using a timer for this to make it work, and had to do a lot of testing to make it do what I wanted.  And what’s neat about this vs a listbox or other methods, is your browser properly shows the icons, allows sorting by clicking column headers, and generally behaves rather nicely!  I was impressed.  No special references needed, no complex module.

In this case, WB1 is my webcontrol box, and I used a button than gets a UNC to a server with a shared folder in this example.  You could easily replace it with something like “C:\Windows”.  You can add more buttons, or make a dropdown that gives you a list of paths, etc…lots of options.  This example is just what will display the results for you.

YES, you need the goofy quotes in the beginning and end of the code,  and the “=”.  Otherwise it will NOT work, you’ll be missing required elements of the control source.

The options for currentviewmode are:

1 = Icons
2 = Small icons
3 = List
4 = Details
5 = Thumbnails
6 = Tile
7 = Thumbnail strip

I played around with adding an HourGlass timer, as sometimes the form loading content took more than a few seconds.  You don’t need it though.

So here is the resulting form code (without all the fancy error handling and what have you):

' this is my button

Private Sub Command52_Click()
Me.WB1.Object.Document.currentviewmode = 3
Me.TimerInterval = 500
End Sub

'I do this so when the record is loaded it sets the view properly

Private Sub Form_Current()
DoCmd.Hourglass True
DoCmd.Hourglass False
Me.TimerInterval = 500
End Sub

'this is the timer code, which is actually VERY simple

Private Sub Form_Timer()
Me.WB1.Object.Document.currentviewmode = 3
Me.TimerInterval = 0
End Sub

No Comments

Sorry, the comment form is closed at this time.