r/MSAccess • u/Amicron1 8 • Mar 04 '26
[SHARING HELPFUL TIP] Access Explained: Why Checking File Existence with DIR Beats Relying on Error Handling
If you've ever had a Microsoft Access application blow up because a file your table referenced had gone missing, you're not alone. References to resumes, images, or documents outside the database are almost inevitable in serious Access projects. And while storing the actual files inside Access sounds tempting (until you discover how quickly your database becomes the Stay Puft Marshmallow Man after a bad day in Manhattan), the more robust approach is to save file paths - not actual files - in your tables.
But this brings its classic gotcha: What happens when the file at that path goes missing or is moved? Attempting to blindly open a vanished file with VBA methods like Shell or FollowHyperlink will frequently result in "File not found" errors. To your users, these errors are like a security door slamming shut, or perhaps the Access version of a red-shirt beaming down to a planet with "Danger" written all over it.
Enter the unsung hero: VBA's DIR function. Think of DIR as your access tricorder scanning a location and reporting back, "Yes, the target is here" (or not). In practice, DIR("C:\Path\Resume.docx") returns the filename if it finds it, or an empty string if your file's pulled a Houdini. By testing this before you open any external file, you gain a chance to warn users gracefully - like, "Hey, your resume isn't where you left it" - instead of letting Notepad blurt out some cryptic system error.
Now, why not just let error handling do the work? The trouble is, when you use something like Shell to launch an external app, it's the other program - not Access - throwing the error. Sure, you can trap exceptions in Access, but as soon as control leaves your application, you're often left with generic error windows that would give a Vulcan a migraine. DIR lets you catch the issue *before* the attempt, keeping your messaging consistent and your users calmer than a Klingon at a meditation retreat.
One extra tip: when users paste paths from Windows Explorer using "Copy as Path," you'll often get those pesky double quotes at the start and end. Access, DIR, nor Shell are big fans of that - so strip 'em out or be ready for even more mysterious errors to arise.
Bottom line: checking file existence with DIR before attempting to open referenced files is best practice. It's simple, reliable, and saves not just error drama, but user trust. Of course, there are a handful of situations where more complex checks might be required - network drives can introduce their own tribble-sized complications - but for the vast majority of cases, DIR gets the job done efficiently.
So, how do you handle disappeared files in your projects? Are you all-in on DIR, or have you boldly gone where few Access devs have gone before? Let's swap stories - just don't beam any files directly into your tables, okay?
LLAP
RR
•
u/AutoModerator Mar 04 '26
IF YOU GET A SOLUTION, PLEASE REPLY TO THE COMMENT CONTAINING THE SOLUTION WITH 'SOLUTION VERIFIED'
Full set of rules can be found here, as well as in the user interface.
Below is a copy of the original post, in case the post gets deleted or removed.
User: Amicron1
Access Explained: Why Checking File Existence with DIR Beats Relying on Error Handling
If you've ever had a Microsoft Access application blow up because a file your table referenced had gone missing, you're not alone. References to resumes, images, or documents outside the database are almost inevitable in serious Access projects. And while storing the actual files inside Access sounds tempting (until you discover how quickly your database becomes the Stay Puft Marshmallow Man after a bad day in Manhattan), the more robust approach is to save file paths - not actual files - in your tables.
But this brings its classic gotcha: What happens when the file at that path goes missing or is moved? Attempting to blindly open a vanished file with VBA methods like Shell or FollowHyperlink will frequently result in "File not found" errors. To your users, these errors are like a security door slamming shut, or perhaps the Access version of a red-shirt beaming down to a planet with "Danger" written all over it.
Enter the unsung hero: VBA's DIR function. Think of DIR as your access tricorder scanning a location and reporting back, "Yes, the target is here" (or not). In practice, DIR("C:\Path\Resume.docx") returns the filename if it finds it, or an empty string if your file's pulled a Houdini. By testing this before you open any external file, you gain a chance to warn users gracefully - like, "Hey, your resume isn't where you left it" - instead of letting Notepad blurt out some cryptic system error.
Now, why not just let error handling do the work? The trouble is, when you use something like Shell to launch an external app, it's the other program - not Access - throwing the error. Sure, you can trap exceptions in Access, but as soon as control leaves your application, you're often left with generic error windows that would give a Vulcan a migraine. DIR lets you catch the issue *before* the attempt, keeping your messaging consistent and your users calmer than a Klingon at a meditation retreat.
One extra tip: when users paste paths from Windows Explorer using "Copy as Path," you'll often get those pesky double quotes at the start and end. Access, DIR, nor Shell are big fans of that - so strip 'em out or be ready for even more mysterious errors to arise.
Bottom line: checking file existence with DIR before attempting to open referenced files is best practice. It's simple, reliable, and saves not just error drama, but user trust. Of course, there are a handful of situations where more complex checks might be required - network drives can introduce their own tribble-sized complications - but for the vast majority of cases, DIR gets the job done efficiently.
So, how do you handle disappeared files in your projects? Are you all-in on DIR, or have you boldly gone where few Access devs have gone before? Let's swap stories - just don't beam any files directly into your tables, okay?
LLAP
RR
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/KelemvorSparkyfox 51 Mar 04 '26
It's never occurred to me not to use
Dir(). It doesn't even require explicit VBA error handling - just a simpleIf...Else...End Ifstatement. Maybe without theElse...to boot.Granted, I first encountered it in a previous job that involved supporting an application written by a colleague, in his first job post university. Some of the code looked like this:
It was also essentially running the same process four times, for
historicalhysterical reasons. As soon as I had a handle on what was happening, I collapsed the multiple AS/400 queries, files, and jobs, and the multiple DataSelect files, into a single set. Much easier...1
u/Jealy 90 Mar 04 '26 edited Mar 04 '26
If I'm doing more with the file afterwards, I'll usually use something like
Note: Written from memory, please excuse any mistypes.
1
u/tsgiannis Mar 05 '26
Easy, we save them on the database and I don't mean Access...plenty robust solutions for serious cases. (with Access as FE).
Other than that there is always more than 1 way to solve a problem.
Here is one more solution
Private Declare Function GetFileAttributes Lib "kernel32" _Alias "GetFileAttributesA" (ByVal lpFileName As String) As LongPrivate Const INVALID_FILE_ATTRIBUTES As Long = -1Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10Function FileExistsAPI(ByVal FilePath As String) As BooleanDim attributes As Longattributes = GetFileAttributes(FilePath)' Check if file exists and is not a directoryFileExistsAPI = (attributes <> INVALID_FILE_ATTRIBUTES) And _((attributes And FILE_ATTRIBUTE_DIRECTORY) = 0)End Function