Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS Access - How do I trap an error for a bad import

Posted on 2011-09-14
2
Medium Priority
?
551 Views
Last Modified: 2012-05-12
In the below code I want to:
1-display a message box that the import failed if there was a problem with this specific line of code ... DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "tblImportExcel", sOpenFile,
2-Exit the module and go back to my home form

NOTE, I am calling this module from a form.  Thus, if an error occurs I need to make sure I exit this module and I am placed back at my form object where I called Import()



Public Sub Import ()
'Get sheet name
Dim sOpenFile As String
'
'display the open file dialog box
sOpenFile = OpenFileDialog("Import Data File")
If sOpenFile <> "" Then
'the user has selected a file to import, the filename and path
'are contained in the variable sOpenFile
sOpenFile = Trim$(sOpenFile) 'remove all trailing spaces
'make sure it's an Excel file
If Right(sOpenFile, 4) <> ".xls" Then
MsgBox "You must select an Excel (.xls) file.", vbOKOnly
Else
KRunQuery ("qImportDeleteTableExcel")' just a call to the same query running code that Access Wizard generates, which I saved as a subroutine. This query deletes the data in the temporary import table. You would need to create your own delete query, or substitute code to do so.
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "tblImportExcel", sOpenFile, True
MsgBox "Initial transfer from spreadsheet to Import Table is COMPLETE! Proceed to next step.", vbOKOnly
End If
End If
End Sub
0
Comment
Question by:upobDaPlaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 36540213
Give this a shot (see the comments in the code):

Public Sub Import ()
    'Get sheet name
     Dim sOpenFile As String

    On Error goto EH   '<-----***** Add this
'
    'display the open file dialog box
    sOpenFile = OpenFileDialog("Import Data File")
    If sOpenFile <> "" Then
         'the user has selected a file to import, the filename and path
         'are contained in the variable sOpenFile
          sOpenFile = Trim$(sOpenFile) 'remove all trailing spaces
         'make sure it's an Excel file
         If Right(sOpenFile, 4) <> ".xls" Then
                MsgBox "You must select an Excel (.xls) file.", vbOKOnly
        Else
                KRunQuery ("qImportDeleteTableExcel")' just a call to the same query running code that Access Wizard generates, which I saved as a subroutine. This query deletes the data in the temporary import table. You would need to create your own delete query, or substitute code to do so.
                On Error Goto EH_Transfer   '<------- ***** Add this
                DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "tblImportExcel", sOpenFile, True
                MsgBox "Initial transfer from spreadsheet to Import Table is COMPLETE! Proceed to next step.", vbOKOnly
       End If
   End If
Exit Sub   '<----- **** add this

EH:
    msgbox "Error " & err.number & ": " & err.description
    Exit Sub
EH_Transfer:
   msgbox "Error " & err.number & ": " & err.description & vbcrlf & "Transfer spreadsheet statement failed!"



End Sub 

Open in new window

0
 

Author Closing Comment

by:upobDaPlaya
ID: 36540438
Very nice...thank you...this is good as I can trap any non import errors and then trap an error specific to the import..thanks for shring your knowledge..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question