Hoping to reach Ken Snell one more time...
I'd like to use similar code on another form to import multiple spreadsheets
based on the value of checkboxes, but in some cases, the spreadsheet the user
is asking to import doesn't exist yet and they see an error message. I need
to add error handling so it skips over that spreadsheet and goes on to the
next.
Here's my code:
-----------------------------------------------
Private Sub Command46_Click()
Select Case Yes
Case Me.chkQCT_YN01.value = Yes
DoCmd.OpenQuery "DSPOC_AccessMW", acNormal, acEdit
Case Me.chkQCT_YN02.value = Yes
DoCmd.OpenQuery "DSPOC_AccessSE", acNormal, acEdit
Case Me.chkQCT_YN03.value = Yes
DoCmd.OpenQuery "DSPOC_AccessSW", acNormal, acEdit
Case Me.chkQCT_YN04.value = Yes
DoCmd.OpenQuery "DSPOC_AccessW", acNormal, acEdit
End Select
Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
Dim lngCount As Long, lngControlLoop As Long
Dim objExcel As Object, objWorkbook As Object
Dim colWorksheets As Collection
Dim strPathFile As String
Dim strPassword As String
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set objExcel = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
blnHasFieldNames = True
For lngControlLoop = 1 To 24
If Len(Me.Controls("txtQCT" & Format(lngControlLoop, "00")).value & "") > 0
And _
Me.Controls("chkQCT_YN" & Format(lngControlLoop, "00")).value = True Then
strPathFile =
"
https://spsf07.web.att.com/sites/NSRC/Shared Documents/QCT_Major_Outage_Templates/" & _
Me.Controls("txtQCT" & Format(lngControlLoop, "00")).value
strPassword = vbNullString
blnReadOnly = True
Set colWorksheets = New Collection
Set objWorkbook = objExcel.Workbooks.Open(strPathFile, , blnReadOnly, , _
strPassword)
For lngCount = 1 To objWorkbook.Worksheets.Count
colWorksheets.Add objWorkbook.Worksheets(lngCount).Name
Next lngCount
objWorkbook.Close False
Set objWorkbook = Nothing
For lngCount = colWorksheets.Count To 1 Step -1
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl" &
colWorksheets(lngCount), strPathFile, blnHasFieldNames,
colWorksheets(lngCount) & "$"
Next lngCount
Set colWorksheets = Nothing
End If
Next lngControlLoop
If blnEXCEL = True Then objExcel.Quit
Set objExcel = Nothing
DoCmd.OpenQuery "SPOC_Name_&_TourApp", acNormal, acEdit
MsgBox "SPOC data imported successfully!"
End Sub