If Imported file not available - what to do

P

pele

I have put togetehr an Access application that requires
the USER to click a command button to import data to be
loaded/added to a table. The import macro will ask the
User if he is sure the data is available in the pre-
defined subdirectory in the C:/ drive. If the User says
yes, then the macro goes ahead and imports the file.

The problem arises if the User says yes BUT if the
expected file is not there. The Macro just seems to halt
and then shows the database window.

What can I do to enhance the macro such that, I can handle
situations where the file does not exist in the predefined
directory and the macro gives the focus back to the
switchboard instead of halting. Any help will be
appreciated.

Pele
 
J

Joe Fallon

Macros do not have error handling.
I recommend you use VBA code instead.
Once you get used to code you won't need to use macros.
In code you can check to see that the file exists and then do something else
if it does not.
 
J

Joe Fallon

Here is one way:

Add this code to a module:

Public Function FileExists(strPath As String, Optional lngType As Long) As
Boolean
On Error Resume Next
FileExists = Len(Dir(strPath, lngType)) > 0
End Function

Then anywhere in your code you can call it like this:

If FileExists("C:\mydocs\MyFile.txt") Then
'do something with the file
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top