How to check if saved import exists with VBA

Part of some vba code that I am writing causes a saved import using:

DoCmd.RunSavedImportExport "import_name" 

This works well for me, except when saved imports do not exist. In this case, it fails.

Is there a way to check if there is an import definition so that I can raise an error or take other actions if it is not?

I use Access 2010, but it should work in 2003 as well.

+4
source share
2 answers

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 
+3
source

In Access 2003 they are stored in the hidden system table "MSysIMEXSpecs", not sure if this remains in later versions, but if you can view it with standard SQL:

 SELECT * FROM MSysIMEXSpecs 

The column name is "SpecName", so this is probably more suitable:

 SELECT * FROM MSysIMEXSpecs WHERE SpecName = 'Your Spec Name' 

This will avoid a mistake.

0
source

Source: https://habr.com/ru/post/1400047/


All Articles