Wildcards in VBA (1 Viewer)

rangersedge

Registered User.
Local time
Yesterday, 22:52
Joined
Jun 13, 2014
Messages
82
I'm trying to use a code to check if a file exists. The code I have already works and gives a MsgBox telling if it exists. It gets the file address through a query that combines multiple fields. This works great when I have the file type entered in the proper field. How do I get this to work if I don't know the file type?

The b variable was meant to find the file without the file type as all my file types are only 3 characters long.

Dim FilePath As String
Dim TestStr As String

Dim a As String
Dim b As String

a = Me.Address
b = LResult = Left(Me.Address, Len(Me.Address) - 3)

FilePath = a

TestStr = ""
On Error Resume Next
TestStr = Dir(FilePath)
On Error GoTo 0
If TestStr = "" Then
MsgBox "File does not exist"
Else
MsgBox "File exist"
End If
 

llkhoutx

Registered User.
Local time
Yesterday, 22:52
Joined
Feb 26, 2001
Messages
4,018
Test the file type too, i.e. return the name and extension, stiriping off each as needed.
 

rangersedge

Registered User.
Local time
Yesterday, 22:52
Joined
Jun 13, 2014
Messages
82
How do I do that?
 

rangersedge

Registered User.
Local time
Yesterday, 22:52
Joined
Jun 13, 2014
Messages
82
That would be useful since my next task is to have it automatically input the file type.
 

rangersedge

Registered User.
Local time
Yesterday, 22:52
Joined
Jun 13, 2014
Messages
82
This almost works. It will find the file if I have no extension but not if the file extension is in the address. Also the TestStr returns only the file name and extension into the address box and not the full file path. The FileType doesn't fill with anything. I don't think parts b and c are looking at the right address.... maybe?

Dim FilePath As String
Dim TestStr As String

Dim a As String
Dim b As String
Dim c As String

a = Me.Address
b = Left$(Me.Address, Len(Me.Address) - InStrRev(Me.Address, "."))
c = Split(Me.Address, ".")(UBound(Split(Me.Address, ".")))

FilePath = a

TestStr = ""
On Error Resume Next
TestStr = Dir(FilePath & "*")
On Error GoTo 0
If TestStr = "" Then
MsgBox "File does not exist"
Else
MsgBox "File exist"
Me.Address = TestStr
Me.Collected = 1
Me.FileType = c

End If
 

essaytee

Need a good one-liner.
Local time
Today, 13:52
Joined
Oct 20, 2008
Messages
512
Use the wildcard character "*" as the extension and simply loop through the results. First time through you will have to provide the full path.

Dir function here.

Steve.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 04:52
Joined
Sep 12, 2006
Messages
15,651
what does the file type have to do with determining whether the file exists?

get the file type using instrev. This will work. easier than determining whether your split syntax works.

filetype = mid(address,instrrev(address,".")+1)
 

Users who are viewing this thread

Top Bottom