【问题标题】:Stoping Macro if Connection is Lost如果连接丢失,则停止宏
【发布时间】:2014-04-03 15:05:01
【问题描述】:

我有一系列代码在指定时运行,第一个是连接检查。如果成功,则允许代码继续,否则将完全停止。但是,我担心在此过程后连接丢失时会发生什么。在此过程中,本地表上有数据上传到我们的 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

如您所见,我在 END IF 之前放置了一个 END,所以如果没有连接,它就会完全结束。

上传代码是

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 秒才能完成
  • 基本上,当子例程中发生任何错误时,您添加的代码会将您发送到您的特殊代码(错误陷阱),然后您可以显示错误并选择下一步做什么。 IE。您测试 err.number = 6 并修复源/原因或忽略或结束。请参阅此链接:support.microsoft.com/kb/141571 或 google 了解如何处理 vba 错误

标签: sql sql-server ms-access vba


【解决方案1】:

您尝试过交易吗?在明确提交事务之前,将插入脚本包装在事务中不会更改数据库。如果在插入过程中数据连接丢失,则永远不会调用提交,也不会更改 SQL Server 数据。

http://msdn.microsoft.com/en-us/library/office/ff196400%28v=office.15%29.aspx

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多