0
Private Sub CmdOpenInsp1201_Click()
Dim xlApp As Excel.Application
Dim xlWkb As Excel.Workbook
Dim xlWks As Excel.Worksheet
Dim SQLCpt As String
Dim SQLLt As String
Dim rsCpt As DAO.Recordset
Dim rsLt As DAO.Recordset
SQLCpt = " SELECT [FirstName] & "" "" & [LastName] AS FullName, TblMembers.Position " & _
" FROM TblMembers " & _
" WHERE TblMembers.Position='Capt #1' "
SQLLt = " SELECT [FirstName] & "" "" & [LastName] AS FullName, TblMembers.Position " & _
" FROM TblMembers " & _
" WHERE TblMembers.Position='Lt #1' "
Set rsCpt = CurrentDb.OpenRecordset(SQLCpt, dbOpenSnapshot)
Set rsLt = CurrentDb.OpenRecordset(SQLLt, dbOpenSnapshot)
Set xlApp = New Excel.Application
Set xlWkb = xlApp.Workbooks.Open(CurrentProject.Path & "\Trk_Insp_1201_10_08_2016.xlsx")
Set xlWks = xlWkb.Sheets("Oct")
xlApp.Visible = True
With xlWks
Do While Not rsCpt.EOF
.Range("U42").Value = Nz(rsCpt!FullName, "")
rsCpt.MoveNext
Loop
End With
With xlWks
Do While Not rsLt.EOF
.Range("AA42").Value = Nz(rsLt!FullName, "")
rsLt.MoveNext
Loop
End With
SubExit:
On Error Resume Next
rsCpt.Close
rsLt.Close
Set rsCpt = Nothing
Set rsLt = Nothing
Exit Sub
SubError:
MsgBox "Error Number: " & Err.Number & "=" & Err.Description, vbCritical + vbOKOnly, "An error occured"
GoTo SubExit
End Sub
這工作並按預期運行,但我不確定錯誤處理。我需要在子結尾處打開文件,但是我發佈了rs,這是否需要?是做這個子的正確方法。錯誤處理分
你會這樣做嗎?
感謝迄今爲止所有的幫助。
d
在錯誤處理程序,它應該是'恢復SubExit'取代'轉到SubExit'。 – Andre
@這很有趣。我不知道你可以這樣做,但它不能按預期工作。使用'Resume SubExit'會繼續拋出相同的錯誤。 OP想要做的是獲得一個錯誤消息,以清除對象之前拋出的任何錯誤。他收到錯誤消息後,希望關閉連接和記錄集並退出子。 – 2016-11-17 06:49:09
我想你誤會了。一個錯誤處理程序塊應該以'Resume