Error Handling

min3010

Registered User.
Local time
Today, 18:41
Joined
Oct 14, 2007
Messages
10
Hi,

I have a procedure to help the user import a file from the c:\ directory into the database. The user basically has to select an option from a combobox and then click on a button to do the import. I anticipate that if they click the button and the expected file is not in the C:\ directory then an error occurs as the file does not exist in the required location and so cannot be imported.

Therefore I have written the following error handler:
Code:
Private Sub btnImport_DblClick(Cancel As Integer)

[B]On Error GoTo Err_handle ' initialize error handling[/B]

If comboActions.ListIndex = 0 Then
DoCmd.RunMacro "USAGE - Import and update data"
ElseIf comboActions.ListIndex = 1 Then
DoCmd.RunMacro "SUBSCRIBERS - Import and update data"
ElseIf comboActions.ListIndex = 2 Then
DoCmd.RunMacro "SERVICE_CHARGE - Import and update data"
End If

[B]Exit_ErrProcedure:
Exit Sub
Err_handle:
MsgBox "Error! Ensure that the correct text file exists in the C:\ directory and try again!"
Resume Exit_ErrProcedure[/B]
End Sub

This is however not working and the error still appears as normal.
Does anyone have any ideas of what I am doing wrong?

Thanks
 
I'm guessing the error is occuring inside the macro. You should be able to what ever your macro is doing with vba code then you'll be able to trap it.

???
ken
 
Simple Software Solutions

By the nature of your code I assume that the names of the files to be imported are different for each option chosen in the combo box, as they are not requested to supply the file name.

If that is the case before you run the if statements first do a select case, such as:-

Dim StrFileName As String
Dim MyMacro As String

Select case me.comboactions.ListIndex
Case 0
Exit Sub
Case 1
StrFileName = "C:\FileName1.Ext"
MyMacro = "USAGE - Import and update data"
Case 2
StrFileName = "C:\FileName2.Ext"
MyMacro = "SUBSCRIBERS - Import and update data"

Case 3
StrFileName = "C:\FileName3.Ext"
MyMacro = "SERVICE_CHARGE- Import and update data"
End Select

If Dir(StrFileName) <> "" Then
DoCmd.RunMacro MyMacro
Else
MsgBox "Error. " & StrFileName & " cound not be found. " & vbcrlf & vbcrlf & "The above file could not be found in the exepected location.",vbExclamation+vbOkOnly,"Import Abandoned"
End If

This will make sure that the correct file exists for the macro and the correct macro is run. This will only apply if the user has first entered a correct choice from the combo box and that the file exists on the root directory.

I suggest though that you move the source location away from the root of C:\. This is primarily for good housekeeping.

Code Master:cool:
 
Thankyou so much DCrake, that worked a treat!
 

Users who are viewing this thread

Back
Top Bottom