Need combo box to display photo file names in a directory

P

Phil F

I need to create a combo box that displays all of the files (photo
filenames) that are in a particular directory (on the network server) so
that I can select a file for entry into the form. I have tried basing the
combo box on a query that uses the Dir(path) function but only the first
file is returned. I attempted to create an array to store the filenames
(approximately 100 files are in each directory) and sort them by name. I
can't seem to make it work.

I am a novice with Access. Does anyone have any ideas?

Phil F
 
G

Guest

Hi Phil,

Try this:

1.) Create a new form. Add a combo box control, and name it: cboFiles
2.) Add a command button. Name it: cmdRequery
3.) In form design view, press F4 to view the properties dialog, or click on
View > Properties, if it is not already displayed. Select the Event tab.
4.) Select your new command button. You should see it's name in the blue
title bar of the properties dialog.
5.) Click into the event that is labelled "On Click". Select [Event
Procedure] from the drop down list. Click on the build button (the small
button with three dots). You should see a form module open with four lines of
code:

Option Compare Database
Option Explicit '<---Add this line if you do not see it

Private Sub cmdRequery_Click()

End Sub

6.) Copy and paste the following code into your new form module:

Option Compare Database
Option Explicit

Private Sub cmdRequery_Click()
On Error GoTo ProcError

Dim strFolder As String
Dim strFile As String
Dim strFileList As String

strFolder = CurrentProject.Path & "\"
strFile = Dir$(strFolder & "*.gif")


strFile = Dir$(strFolder & "*.gif")
Do While Len(strFile) > 0
strFileList = strFileList & strFile & ";"
Debug.Print strFileList
strFile = Dir$
Loop


Debug.Print strFile
Me.cboFiles.RowSourceType = "Value List"
Me.cboFiles.RowSource = strFileList

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure cmdRequery_Click..."
Resume ExitProc
End Sub



7.) Click on Debug > Compile ProjectName
Note: ProjectName is a place holder for the name of your VBA project, which
is likely the same name as your database file.

8.) Save your changes. Open your form and test it by clicking on the command
button.

This code will only populate the combo box with the file names (not the file
paths), but it should serve as a starting point for you. A good reference
article for this type of thing is found here:

http://advisor.com/doc/16279

If you had to manually enter Option Explicit, then please refer to this link
to correct this problem:

Always Use Option Explicit
http://www.access.qbuilt.com/html/gem_tips.html#VBEOptions


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
 
G

Guest

Hi Phil F,

Why not just use the Common Dialog control to open up a browse window for
that directory (works the same as a file-->open dialog in Windows)...

I can send code samples if you like, or you could look up help on the Common
Dialog Control...

Hope that helps...

Damian.
 
G

Guest

Upon further consideration, I like Damian's suggestion better than my own.
The code I gave you may serve as an introduction to programming, however, as
written, it is limited to one file extension. Photos can obviously come with
a variety of file extensions.

Here is a link for API based Common Dialog code:

API: Call the standard Windows File Open/Save dialog box
http://www.mvps.org/access/api/api0001.htm


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
 
P

Phil F

Damian,

I tried to use the Microsoft Common Dialog Control, version 6.0 in Access
2003 but received the error: "You don't have the license required to use
this ActiveX control.". When I searched the help files, I could find no
mention of "Common Dialog Control". Can you point me in the right
direction with code or references (I am a novice)? I would like to try your
suggestion.

Phil F
 
R

Rick Brandt

Phil F said:
Damian,

I tried to use the Microsoft Common Dialog Control, version 6.0 in Access 2003
but received the error: "You don't have the license required to use this
ActiveX control.". When I searched the help files, I could find no mention of
"Common Dialog Control". Can you point me in the right direction with code or
references (I am a novice)? I would like to try your suggestion.

The common dialog control is a nightmare to use if you need your app to work on
more than one PC as it is rife with version problems. It is better to use the
API code available at the link below. That will work consistently on just about
any Windows system.

http://www.mvps.org/access/api/api0001.htm
 
P

Phil F

Tom

Your first suggestion using the Dir function worked well. I would also like
for the Date and Time that the file was created to be displayed in a second
column in the combo box if possible. Do you have any ideas on how to do
that?

I was unable to use the suggestion by Damian S of using the Microsoft Common
Dialog Control (see my reply to Damian). If you think that his suggestion
is a better way, please point me in the right direction.

Side question: Why put "$" after Dir in your first suggestion?

Phil F
 
G

Guest

Hi Phil,
I would also like for the Date and Time that the file was created to be
displayed in a second column in the combo box if possible. Do you have
any ideas on how to do that?

Dir (and Dir$) will not return this attribute. You can use the File System
object model to return this information, however, it is much slower versus
using Dir. The other gotcha is that I believe it will not be supported in
Access 2007. Without doing further research, I think you can get this
information using the FindFile API calls (but I'm not positive). Here is a
free article on Access Advisor that shows how to use various methods to find
files:

http://advisor.com/doc/16279

Try the API method and see if you can't figure out how to get the additional
file attributes that you want to display. Although written in VB6 code,
Karen's Power Tools offers a utility to return this information. More
importantly, she includes the source code for free:

http://www.karenware.com/powertools/ptdirprn.asp

So, if you have a copy of VB6 installed, you can reverse engineer her sample
to see how she does it. Then the challenge becomes one of seeing if this
technique in VB6 will work in VBA code.
Side question: Why put "$" after Dir in your first suggestion?
According to the book "VB & VBA in a Nutshell", by Paul Lomax (Published by
O'Reilly), on page 236 the following distinction is given:

"Dir returns a variant of subtype string: Dir$ returns a string data type."

I <i> think </i> Dir$ is a bit more efficient, because the variant does not
need to be converted on the fly to a string.


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
 
D

Douglas J. Steele

To be perfectly honest, it doesn't really matter whether or not you can make
sense of the API code. Just copy it into a new module, and follow the
examples at the beginning of the page.
 
P

Phil F

Tom,

Thank you. Regarding Karen's Power Tools, I have the VB that is installed
with MSFT Office Access 2003 that says 6.3 in the Help section of VB. Will
that work with Karen's utility?


Phil F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top