Access vba Check if file exists
36,948
Create a small function to check if a file exists and call it when needed.
Public Function FileExists(ByVal path_ As String) As Boolean
FileExists = (Len(Dir(path_)) > 0)
End Function
Since the backend database paths dont change, why dont you declare two constants and simply check their value?
Sub Exist()
Const workFolder As String = "C:\Work Folder\backend.accdb"
Const personalFolder As String = "D:\Personal Folder\backend.accdb"
If FileExists(workFolder) Then
'Work folder exists
End If
'....
If FileExists(personalFolder) Then
'Personal folder exists
End If
End Sub
Comments
-
YvetteLee almost 4 years
I have a database split in FrontEnd and BackEnd.
I have it running: i) in my office ii) updating/testing in my personal computer.
My BackEnd file is running in different Folder location according to computer running.
I want to place a code and check if file exist.
Code:
Sub FileExists() Dim strPath As String '<== Office. Dim strApplicationFolder As String Dim strPathAdmin As String '<== Admin. strPath = "\\iMac\Temp\" strApplicationFolder = Application.CurrentProject.Path strPathAdmin = strApplicationFolder & "\Temp\" If Dir(strApplicationFolder & "SerialKey.txt") = "" Then '===> Admin User. If Dir(strPathAdmin & "*.*") = "" Then '===> Empty Folder. Else '===> Files on folder. End If Else '===> Office User. If Dir(strPath & "*.*") = "" Then '===> Empty Folder. Else '===> Files on folder. End If End If End Sub()
I have this until now.
Any help.
Thank you...
-
YvetteLee almost 7 yearsThank you. I think that it is what I need. Can I add in the function the two const? I am going to use the code in many places for security.
-
Kostas K. almost 7 yearsIf you're planning to use it in many places, you should keep the function as is and call it
If FileExists(fileName)
. If you want to have access to the constant variables, put them on the top of a Module and mark themPublic
to access from anywhere in the app orPrivate
to access them from anywhere in that module.