wb2

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.lblOps.Caption = "THIS IS THE CONTENTS OF THE FOLDER:"
Me.WB1.ControlSource = "=""\\YOURSERVER\YOURSHARE\SAMPLEDIR"""
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
Me.lblOps.Caption = "THIS IS THE CONTENTS OF THE FOLDER:"
Me.WB1.ControlSource = "=""\\YOURSERVER\YOURSHARE\SAMPLEDIR"""
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

Jeff Shirley

Jeff Shirley

Founder & CEO
Jeff brings over 27 years of experience to the table, along with numerous awards, certifications, and real-world implementations.  His database solutions are currently running in industries including government, mining, agriculture, finance, education, science, research, non-profits, and healthcare businesses around North America and serve thousands of users day to day.  Jeff is a five-time MVP Award recipient for Microsoft Access, acknowledging contributions to community projects, evangelism, and educational outreach on Microsoft technologies.  Today there are less than 50 Access MVP’s worldwide. While Jeff focuses day to day on business management, IoT development, IT integrations and design, database architecture or other typical complex tasks, he also is a co-owner of Blue Heron Gardens, a commercial apiary running over 200 beehives.  Fresh honey is a small perk for many of our clients!

Mitch Redekopp

Partner
Mitch works with our clients day to day ensuring web development and IT projects are done to exceed customer expectations.  His background includes formal training in IT, Microsoft and CompTIA certifications, real-world business experience, and a true dedication to ensuring clients receive timely and professional support on their projects with Rivercity Technology Services.  Given the most difficult requests for IT solutions, Mitchell can assist our clients in finding a path to the best options which deliver results for our clients.  Mitchell is an avid soccer player and Manchester City fan, be sure to talk soccer with him when you can.