essaytee
Need a good one-liner.
- Local time
- Today, 12:57
- Joined
- Oct 20, 2008
- Messages
- 514
I cannot error trap a FileDialog routine, where the user selects an Excel spreadsheet for the purposes of copying the contents of the spreadsheet into a a table.
If the user selects an Excel file that is already open in Excel, the FileDialog messagebox appears advising that the file is in use and for the user to close the other application. The problem is, I cannot trap for that error using the 'On Error' handling routine. I want to log and record the fact the Excel file was already open and should be closed in the other application. Sample code below:
Any advice appreciated, it's been a while since I last tinkered with my application.
If the user selects an Excel file that is already open in Excel, the FileDialog messagebox appears advising that the file is in use and for the user to close the other application. The problem is, I cannot trap for that error using the 'On Error' handling routine. I want to log and record the fact the Excel file was already open and should be closed in the other application. Sample code below:
Code:
Private Sub cmd_Import_Click()
On Error GoTo Error_In_Code
Dim strFileName As String
Dim dlg As FileDialog
Dim rst As Recordset
Dim strTable As String
Dim lngCount As Long
Dim strSqlDelete As String
Dim strMsg As String
strTable = "tbl_Brief_Charges_Spreadsheet"
strSqlDelete = "Delete * from " & strTable & ";"
Set dlg = Application.FileDialog(msoFileDialogFilePicker)
With dlg
.Title = "Select the Excel file to import"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx", 1
.Filters.Add "All Files", "*.*", 2
If .Show = -1 Then
strFileName = .SelectedItems(1)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strFileName, True
Set rst = CurrentDb.OpenRecordset(strTable, dbOpenDynaset)
lngCount = NumberOfRecords(rst)
If lngCount > 0 Then
strMsg = "You are about to import " & Str(lngCount) & " Charges, Proceed."
If MsgBox(strMsg, vbYesNo, "Spreadsheet Import") = vbYes Then
AppendCharges (BuildSQL_AppendCharges())
' Delete temporary imported charges, they have already been added to the brief
CurrentDb.Execute strSqlDelete
LogImportSession "Import", "", strFileName, lngCount
Else
' empty the temporary charges table
CurrentDb.Execute strSqlDelete
LogImportSession "Import Cancelled", "", strFileName, 0
End If
Else
' no charges have been imported, notify the user
strMsg = "No charges have been imported, check the spreadsheet"
MsgBox strMsg, , "Spreadsheet charges"
LogImportSession "Import", "Empty spreadsheet", strFileName, 0
End If
Else
LogImportSession "Import", "Spreadsheet file not found", "", 0
End If
End With
Exit_Code:
Exit Sub
Error_In_Code:
Select Case Err.Number
Case 2391 ' Field doesn't exist
MsgBox "The spreadsheet you selected is in the wrong format, the row headings are not correct. Download again from #######"
LogImportSession "Import Error", "Spreadsheet wrong format", strFileName, 0
Case Else
MsgBox Err.Number & " " & Err.Description
LogImportSession "Import Error", Err.Number & " " & Err.Description, strFileName, 0
End Select
Resume Exit_Code
End Sub
Any advice appreciated, it's been a while since I last tinkered with my application.