Error trapping

mukudu99

Registered User.
Local time
Today, 21:24
Joined
Dec 22, 2006
Messages
15
Hi Gurus
I have a code that copies a database using the common windows dialog box to a temporal import folder from which i transfer a table to my database using:
Function ImportPatients()

On Error GoTo ImportPatients_Err



DoCmd.TransferDatabase acImport, "Microsoft Access", "C:\x\Import\xx.mdb", acTable, "BLP_Data", "x_Imported", False


ImportPatients_Exit:
Exit Function

ImportPatients_Err:
'apam
MsgBox "Could Not Import Data", vbExclamation + vbOKOnly, "Unsuccessful Data Merge"
Resume ImportPatients_Exit

End Function

now if i did not select a valid database the code crushes when it reaches the point:
DoCmd.TransferDatabase acImport, "Microsoft Access", "C:\x\Import\xx.mdb", acTable, "BLP_Data", "x_Imported", False
-----which is expected------
I actually get the run error 3343------again expected because i did not copy a valid database....
Now my question:
Is there a way to trap this error...
By the way this code is running in a class module

Thanks
 
Try this:

Code:
   Dim Msg As String
   On Error GoTo ImportPatients_Err
   DoCmd.TransferDatabase acImport, "Microsoft Access", _
             "C:\x\Import\xx.mdb", acTable, "BLP_Data", "x_Imported", False

ImportPatients_Exit:
   Exit Function

ImportPatients_Err:
   Select Case Err.Number 
      Case 3343 
         Msg = "Can Not Locate Supplied Database!"
      Case Else
         Msg "Could Not Import Data"
   End Select
   MsgBox Msg, vbExclamation + vbOKOnly, _
              "Unsuccessful Data Merge"
      Resume ImportPatients_Exit
End Function

.
 
Thanks for the response
I have tried this but somehow the error is not trapped.
the code goes straight to DoCmd.TransferDatabase statement and the error still pops up
 
I have tried this but somehow the error is not trapped.

This is most likely because you do not have the Break On Unhandled Errors property set ON (Checked Marked) in the VBA Editor. Open the VBA code editor then in the Tools Menu select Options. The Options dialog is opened to you, select the General Tab. Make sure the Break On Unhandled Errors property is check-marked.

Another thing you can do is to check to see if the Database file actually exists before any other code is run. You can use the Function below to see is a DB file (or any file) exists:

Code:
Public Function DoesFileExist (PathStrg As String) As Boolean
    If Len(Dir(PathStrg, 14)) > 0 Then DoesFileExist = True
End Function

To use this Function in the previously posted code:

Code:
   If Not DoesFileExist("C:\x\Import\xx.mdb") then 
      MsgBox "Can Not Locate Supplied Database!", _
              vbExclamation + vbOKOnly, _
              "Unsuccessful Data Merge"
      Exit Sub    '(or Exit Function)
   End If

   Dim Msg As String
   On Error GoTo ImportPatients_Err
   DoCmd.TransferDatabase acImport, "Microsoft Access", _
             "C:\x\Import\xx.mdb", acTable, "BLP_Data", "x_Imported", False

ImportPatients_Exit:
   Exit Function

ImportPatients_Err:
   Select Case Err.Number 
      Case 3343 
         Msg = "Can Not Locate Supplied Database!"
      Case Else
         Msg "Could Not Import Data"
   End Select
   MsgBox Msg, vbExclamation + vbOKOnly, _
              "Unsuccessful Data Merge"
      Resume ImportPatients_Exit
End Function

.
 

Users who are viewing this thread

Back
Top Bottom