locate a file using vba - I am stack

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everyone and Happy New Year.

I want to write some code in MS-Access in order to see if a file is located
in a specific folder in Hard Disk. If is present then the user will be able
to enter the program.

Thank you in advance,

George ([email protected])
 
Try

Dim strPath As String
strPath = "c:\FileName.doc"
If Dir(strPath) = "" Then
MsgBox "Can't find file"
Else
....
End If
 
Hi Ofer

I have the same question but Im glad that I found your response. However, I
have a follow question to George's question:

In your code:
Dim strPath As String
strPath = "c:\FileName.doc"
If Dir(strPath) = "" Then
MsgBox "Can't find file"
Else
....
End If

How do I list (or display on the screen) all the files under my
C:\0Templates folder when the user click the button? It's like the windows
explorer but I want it to just list what's on the C:\0Templates folder.

Thanks in advance
Rob
 
Hi Ofer

Sorry, I need to revise my question:

How do I list (or display on the screen) all the .mdb/xls/csv/txt files
under my
C:\0Templates folder when the user click the button? It's like the windows
explorer but I want it to just list what's on the C:\0Templates folder and
only those extensions. If this is harder, then .mdb is just fine.

Thanks again

Rob
 
Try something like:

Public Sub test()

Dim strWildcard As String
Dim strFolder As String
Dim strFileName As String

strWildcard = "*.mdb"
strFolder = "c:\0Templates\"

strFileName = Dir(strFolder & strWildcard)
Do While strFileName <> ""
Debug.Print strFileName
strFileName = Dir
Loop

End Sub
 
Does the user just want to look at the names, or do you want them to be
able to do something useful, such as select a file for some further
operation? If so, why not use the standard Windows File dialog (see
www.mvps.org/access/api/api0001.htm).

If you just want to look at the names, do they need to be sorted in
alphabetical order (or some other order), or is it OK for them to come
any old how?
 
Thanks guys for your responses. Actually, what I want to do is, when a user
click the button, it will list ALL files stored in C:\0Templates folder and
will be displayed on a form. Is this possible?

Thanks again,
Rob
 
Thanks guys for your responses. Actually, what I want to do is, when a user
click the button, it will list ALL files under the C:\0Template folder and
will be displayed in a form that I will create. Is this possible?

Thanks again,
Rob
 
Thanks guys for your responses. Actually, what I want to do is, when a user
click the button, it will list ALL files under the C:\0Template folder and
will be displayed in a form that I will create. Is this possible?

Thanks again,
Rob
 
Yes, it's possible. Since you don't seem to care what order the files
are listed in, or want to be able to do anything with the names except
look at them, something like this should do. Put a textbox on the form,
of a suitable size, and call it txtFileList.

Then in the button's Click event procedure, put something like this (air
code)

Dim strFiles As String
Dim strThis As String
Const FOLDER = "C:\0Template"

strThis = Dir (FOLDER & "\*.*")
Do While Len(strThis) > 0
strFiles = strFiles & strThis & vbCrLf
strThis = Dir()
Loop

Me.txtFileList.Value = strFiles
 
Thank you very much. It works just fine.

Thank you again

Ο χÏήστης "Ofer" έγγÏαψε:
 
Back
Top