Provide an error handler to solve this problem (Access 2007 and later):
Sub Importer() On Error GoTo ErrImport DoCmd.RunSavedImportExport "import_name" ExitImporter: Exit Sub ErrImport: MsgBox Error 'Err = 31602 for missing import specification Resume ExitImporter End Sub
A more general solution uses the TransferText command as follows:
Sub Importer() On Error GoTo ErrImport DoCmd.TransferText acImportDelim, "Import_file Import Specification", "import_file", "import_file.txt", False ExitImporter: Exit Sub ErrImport: MsgBox "Error Number = " & Err & ", Message=" & Error ' Error 3011 indicates import_file.txt not found Resume ExitImporter End Sub
source share