我有一系列指定時運行的代碼,第一種是連接檢查。如果它成功,那麼它允許代碼繼續,如果不是,它會完全停止代碼。然而,我擔心在這個過程之後連接丟失時會發生什麼。在這個過程中,如果連接終止中間下載,那麼在本地表上有數據上傳到我們的SQL服務器,有時數據仍然被傳輸,但如果它立即發生,數據不會傳輸。如果連接丟失,則使用宏接口
代碼的第二部分刪除所有包含員工信息的本地表內容,然後下載新數據,以便在有任何更新時提供最新信息。
我想弄清楚是否有一種方法或代碼可以實現告訴查詢在連接丟失時立即停止運行,或者如果有方法可以在發生連接時將其撤消。
或者將連接代碼與上傳和刪除代碼結合起來,這樣每次啓動流程之前都會運行它是不是一個好主意?
運行之初的連接代碼:
Public Function StartUp()
Dim cnn As ADODB.Connection
Dim localrst As New ADODB.Recordset
Dim remoterst As New ADODB.Recordset
On Error Resume Next
Set cnn = New ADODB.Connection
cnn.Open "Provider=PRO; Data Source=SOURCE; Initial Catalog=CAT;" _
& "User Id=ID; Password=PW;"
If cnn.State = adStateOpen Then
MsgBox ("You have an established connection with the L&TD SQL Server Database and the CDData table has been uploaded to the server.")
Else
MsgBox ("Cannot connect to SQL Server. Data will be stored locally to CDData Table until application is opened again with an established connection.")
End
End If
On Error GoTo 0
' MsgBox ("Please wait while the database is updating, this may take a moment.")
End Function
正如你所看到的,我把一個結束結束前如果是這樣,如果沒有連接,它只是完全結束。
上傳代碼是
Public Function Update()
Dim cdb As DAO.Database, qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim err As DAO.Error
' Const DestinationTableName = "AC_CDData"
Const ConnectionString = _
"ODBC;" & _
"Driver={SQL Server Native Client 10.0};" & _
"Server=SERV;" & _
"Database=DB;" & _
"UID=ID;" & _
"PWD=PWD;"
Set cdb = CurrentDb
Set qdf = cdb.CreateQueryDef("")
Set rs = CurrentDb.OpenRecordset("CDData", dbOpenTable)
qdf.Connect = ConnectionString
Do While Not rs.EOF
qdf.SQL = "INSERT INTO AC_CDData_1(EmployeeID, EmployeeName, Region, District, Function1, Gender, EEOC, Division, Center, MeetingReadinessLevel, ManagerReadinessLevel, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3, DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Notes, Changed, JobGroupCode, JobDesc, JobGroup) " & _
"Values (" & _
"'" & rs!EmployeeID & "', " & _
"'" & rs!EmployeeName & "', " & _
"'" & rs!Region & "', " & _
"'" & rs!District & "', " & _
"'" & rs!Function1 & "', " & _
"'" & rs!Gender & "', " & _
"'" & rs!EEOC & "', " & _
"'" & rs!Division & "', " & _
"'" & rs!Center & "', " & _
"'" & rs!ManagerReadinessLevel & "', " & _
"'" & rs!MeetingReadinessLevel & "', " & _
"'" & rs!EmployeeFeedback & "', " & _
"'" & rs!DevelopmentForEmployee1 & "', " & _
"'" & rs!DevelopmentForEmployee2 & "', " & _
"'" & rs!DevelopmentForEmployee3 & "', " & _
"'" & rs!DevelopmentForEmployee4 & "', " & _
"'" & rs!DevelopmentForEmployee5 & "', " & _
"'" & rs!Justification & "', " & _
"'" & rs!Notes & "', " & _
"'" & rs!Changed & "', " & _
"'" & rs!JobGroupCode & "', " & _
"'" & rs!JobDesc & "', " & _
"'" & rs!JobGroup & "')"
qdf.ReturnsRecords = False
On Error GoTo Update_qdfError
qdf.Execute dbFailOnError
On Error GoTo 0
rs.MoveNext
Loop
rs.Close
Set qdf = Nothing
Set cdb = Nothing
Set rs = Nothing
Exit Function
Update_qdfError:
For Each err In DAO.Errors
MsgBox err.Description, vbCritical, "Error " & err.Number
Next
End Function
那麼,有沒有辦法可以修改連接代碼,並將其添加到更新的代碼(減去消息框),因此如果連接切斷它將終止碼?
你可以捕獲錯誤,然後重試連接和恢復 - 假設你保存你輸入的「最後成功的關鍵」。此外,這個過程需要很長時間嗎?您可能想要更改爲'rsOut.Addnew'...'rsOut.Update'... –
我如何捕獲錯誤,並且大約需要30秒才能完成 – user2119980
基本上,您添加的代碼會將您發送給您的代碼特殊代碼(錯誤陷阱),當子程序發生任何錯誤時,您可以顯示錯誤並選擇下一步做什麼。即你測試err.number = 6並修復源/原因或忽略,或者結束。請參閱此鏈接:http://support.microsoft.com/kb/141571或谷歌如何處理vba錯誤 –