find and display files

G

gymphil

Can anyone assist?
I am trying to search a directory that contains pdf files, I have found a
bit of code that I thought might achieve what I want to do:

Set fs = Application.FileSearch
With fs
.LookIn = "\\publication order"
.Filename = Me.Text0

.Execute
If Len(.Filename) > 0 Then
For i = 1 To .foundfiles.Count
Me.List2.Additem (.foundfiles(i))

Me.List2.i
Next i
End If

End With

The idea is to populate a listbox 'list2' with the files found using part of
the file name, I am getting a compile error - method or data member not found
(.Additem)

Many thanks
 
D

Douglas J. Steele

What's the RowSourceType of List2? It must be Value List for the AddItem
method to work.

If that's not the issue, what version of Access are you using? If memory
serves, the AddItem method wasn't added until Access 2002.
 
G

gymphil

Thanks for your prompt response.

I am using access 2000.

Basically I want to find files in a 'C' directory using data from a control
on a form, but I need to combine this with a wild card. I initally wanted to
use hyperlinks but cannot use wildcards in hyperlinks, so I want to extract
the file names and somehow add them to a list box or perhaps a form so the
the user can select the correct and full file as perhaps a hyperlink to open
a pdf file. I am not sure what is the best way to achieve this but need to
use a wildcard as the file names in the database do not match the files names
in the 'C' drive.
Hope this makes sense.
Many thanks
 
D

Douglas J. Steele

As I mentioned else-thread, the AddItem method wasn't introduced until
Access 2002, so you won't be able to use that code.

Can you present them with the standard Windows File Open/File Save dialog
instead? See http://www.mvps.org/access/api/api0001.htm at "The Access Web"
for a complete example.
 
G

gymphil

Doug,

Thanks for your advise, I have had a look a the link you gave me, not sure
what to do with the code but I will try and work it out.

many thanks
 
D

Douglas J. Steele

Create a new module (not a class module nor a module associated with a form
or report).

Copy everything in the shaded area (between Code Start and Code End) into
that module.

When you save the module, make sure you don't name it the same as any subs
or functions in it. (calling it something like mdlFileDialog would probably
be a good idea)

Where you want to invoke the dialog, use code like the sample at the top of
the page. To find only PDF files, you'd use something like

Dim strFilter As String
Dim strInputFileName as String

strFilter = ahtAddFilterItem(strFilter, "PDF Files (*.PDF)", "*.PDF")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

strInputFileName will contain the complete path to the file they select.
 
P

Piet Linden

Can anyone assist?
I am trying to search a directory that contains pdf files, I have found a
bit of code that I thought might achieve what I want to do:

Set fs = Application.FileSearch
    With fs
    .LookIn = "\\publication order"
    .Filename = Me.Text0

    .Execute
    If Len(.Filename) > 0 Then
    For i = 1 To .foundfiles.Count
    Me.List2.Additem (.foundfiles(i))

    Me.List2.i
    Next i
    End If

    End With

The idea is to populate a listbox 'list2' with the files found using partof
the file name, I am getting a compile error - method or data member not found
(.Additem)

Many thanks

You can do the same with DIR. I just posted a bit of code that added
the data to a combobox, but the idea is basically the same.
 
G

gymphil

Doug,

Thanks, I believe I have done what you suggested.
Don't know if I am missing the obvious but basically using a command button
I am now prompted to open pdf files in a directory, I can only select one,
but nothing happens!
What I am looking for is the user to input the first 6 characters of the
file name in a text field and somehow or another to view or browse those
files that begin with those characters, and to select the required file to
open.

Apologies if I did not explain this properly.
 
D

Douglas J. Steele

You're absolutely right. All that code does is prompt the user to a select a
file, and stores the full path to the selected file in a variable
(strInputFileName).

All the code you had before was going to do was put the names of the files
into a combo box. Once the user selected a file from that combo box, nothing
was going to happen unless you did something with the selection.

Assuming what you're trying to do is open the document in the user's default
application (Adobe Reader, FoxIt, whatever), try

Application.FollowHyperlink strInputFileName
 
G

gymphil

Doug,

OK, I added the followhyperlink to the back of the code and it is now
opening the file I selected when prompted, great! Is it possibe (rather than
opening the whole directory) to just return the file (or files) with the
first 6 characters that the user types into a text box? I know that I cannot
use hyperlinks with wildcards and I cannot use AddItem in Access 2000 with a
list box, is there another way? Ideally I would like to add these files to a
combo box or list box or subform so that the user can select the required
file and open the pdf document.
Also, the combo box currently is inactive! it does not store the string
variable.

Appreciate your help, I am still a novice!
 
D

Douglas J. Steele

Sorry, but why? You've already got a way for them to select the PDF file of
interest: why do you need to use a combo box instead?

That being said, the dialog can limit those PDF files returned based on the
first 6 characters typed into a text box named SomeTextBox if you change the
filter to

strFilter = ahtAddFilterItem(strFilter, "PDF Files (*.PDF)", Me!SomeTextBox
& "*.PDF")
 
G

gymphil

Brilliant!

It works perfectly and is just want I wanted to achieve. The user inputs the
first few characters into the textbox and those pdf files are displayed for
selection, superb.

I sincerely appreciate your help and hope this information is helpful to
others.

Thanks again Doug
 
G

gymphil

Sorry to be a pain, one further question - How can I direct the search to
look at a specific directory?
 
D

Douglas J. Steele

The ahtCommonFileOpenSave function has an optional InitialDir parameter.

Dim strFilter As String
Dim strFolder As String
Dim strInputFileName as String

strFolder = "C:\Folder\Subfolder\"
strFilter = ahtAddFilterItem(strFilter, "PDF Files (*.PDF)", _
Me!SomeTextBox & "*.PDF")
strInputFileName = ahtCommonFileOpenSave( _
InitialDir = strFolder, _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)
 
G

gymphil

Doug

I have amended the code but I am getting a compile error:
Named argument already specified
ahtOFN_HIDEREADONLY

Any suggestions?
 
D

Douglas J. Steele

The implication is that somewhere in your application, you've declared that
constant more than once.

Do a search through all your modules for Const ahtOFN_HIDEREADONLY
 
G

gymphil

Doug,

I used the same code with slight variations a number of times on the same
form. I take it that cannot be done? I have now removed all the duplicated
constants. What happens now is that I still get the same compile error,
having searched the modules the only other place this appears is in the code
from: http://www.mvps.org/access/api/api0001.htm, not only that but the
InitialDir parameter is not working now either, it has reverted back to
opening the C directory. One step forward and two back.



Private Sub pdf1_Click()
Dim strFilter As String
Dim strFolder As String
Dim strInputFileName As String

strFolder = "\\jsnet\drawingofficeintranet\zz publication order\"
strFilter = ahtAddFilterItem(strFilter, "PDF Files (*.PDF)", _
Me!SomeTextBox & "*.PDF")
strInputFileName = ahtCommonFileOpenSave( _
InitialDir = strFolder, _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

End Sub
 

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