Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filesearch an exact name

Status
Not open for further replies.

flaviooooo

Programmer
Feb 24, 2003
496
FR
Hi,

I have a form where we enter a code. When we press a button, I'd like to have a code that will search for a PDF file with the exact same name as the code, in a specific folder C:\studio\PDF.

For this, I use this code:
Dim rs As Recordset
Dim rstReq As Recordset
Dim strsql As String
Dim strinput As String
Dim varItem As Variant
Dim fs As Object
Dim AppX

DoCmd.SetWarnings False

Set fs = Application.FileSearch
With fs
.Lookin = "Q:\STUDIO\PDF\"
.filename = Me.DRUKCODE & ".pdf"
.SearchSubFolders = True
If .Execute() > 0 Then
strinput = .FoundFiles(1)

AppX = "C:\Program Files\Internet Explorer\iexplore.exe " & strinput
Call Shell(AppX, 1)

Else
MsgBox ("PDF File " & Me.DRUKCODE & ".pdf niet gevonden in Q:\STUDIO\PDF\ !")
End If
End With

DoCmd.SetWarnings True


Unfortunately, there seems to be a problem with this...

When for example I search for code TNC222146, it should look for PDF file 'TNC222146.pdf'. This does not exist, so I should receive an error message that it was not found. The file 'TNC222146sep part B.pdf' on the other hand does exist, and this is returned.

Is there a way to make the filesearch on an exact name??

Thanks in advance

Kind regards,
Fabian
 
You may try this property:
.MatchTextExactly = True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the quick reaction, but unfortunately it still doesn't work :(
 
the foundfiles collection is zero indexed so use .foundfiles(0)

you don't need all those variables for this routine.

also maybe you need to use Me.DRUKCODE.Text or .Value ?
 
Hi p27br,

I tried this, but unfortunately I get a runtime error 9 - Subscript out of range when it reaches .foundfiles(0)
 
are you sure you've not got a wildcard character in drukcode

--------------------
Procrastinate Now!
 
No, the drukcode that is having the problem in the example is TNC222146
 
hmm, have you considered trying something like:

dim fs = CreateObject("Scripting.FileSystemObject")
if fs.FileExsists(yourPath & ".pdf") then
'do stuff
else
'do stuff
endif

--------------------
Procrastinate Now!
 
Does this have the same flexibility as Filesearch? The PDF files in Q:/Studio/PDF are all divided in seperate folders per customer. So I never know the entire path. For example the TNC222146.pdf is located in Q:\STUDIO\PDF\Anderen\The Nut Company

 
I have solved my problem with a workaround now:

Dim rs As Recordset
Dim lengte As Integer
Dim strsql As String
Dim strinput As String
Dim varItem As Variant
Dim fs As Object
Dim AppX

DoCmd.SetWarnings False

Set fs = Application.FileSearch
With fs
.Lookin = "Q:\STUDIO\PDF\"
.filename = Me.DRUKCODE & ".pdf"
.SearchSubFolders = True
.MatchTextExactly = True
If .Execute() > 0 Then

lengte = Len(Me.DRUKCODE) + 4
For Each vItem In .FoundFiles
If Right(vItem, lengte) = Me.DRUKCODE & ".pdf" Then
strinput = vItem

AppX = "C:\Program Files\Internet Explorer\iexplore.exe " & strinput
Call Shell(AppX, 1)

Exit Sub
End If
Next vItem
End If
End With

'this line is only reached if no appropriate PDF is found
MsgBox ("PDF File " & Me.DRUKCODE & ".pdf niet gevonden in Q:\STUDIO\PDF\ !")

DoCmd.SetWarnings True


As you can see I'm fiddling a bit with the length of my searched PDF that I'm 'cutting' from the right of the found PDFs.

If for example TNC222146.pdf is searched and it finds TNC222146.pdf and TNC222146sep B.pdf then the extra control will filter out the right one.

I still find it ridiculously difficult, when this should be fairly standard? Especially when there is a property .MatchTextExactly

But oh well, it works now :)

Thanks for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top