简体   繁体   English

MS Access VBA:使用workspace.OpenDatabase通过ODBC连接连接到不可用的SQL Server-优雅的恢复?

[英]MS Access VBA: Using workspace.OpenDatabase to connect to an unavailable SQL server via an ODBC connection - elegant recovery?

In an application that uses a MS Access form as a front-end to some SQL databases, I use DBEngine.CreateWorkspace to get a workspace, then workspace.OpenDatabase to connect to my remote SQL server via a defined ODBC System DSN. 在使用MS Access表单作为某些SQL数据库的前端的应用程序中,我使用DBEngine.CreateWorkspace来获取工作空间,然后使用workspace.OpenDatabase通过已定义的ODBC System DSN连接到远程SQL服务器。 This all works quite nicely, until someone disconnects the remote SQL machine from the network, or shuts it down, or something else similarly ridiculous. 这一切都很好,直到有人断开远程SQL机器与网络的连接,或将其关闭,或其他类似的荒谬之举。 (Note: I know there's a lot ridiculous about this setup, but unfortunately it's an inevitability at this point) (注意:我知道此设置有很多荒谬之处,但不幸的是,这是不可避免的)

My question is: Is there a way to elegantly deal with the timeout and subsequent 'SQL Server does not exist or access denied' error messages that come up, within the VBA code? 我的问题是:在VBA代码中,是否有办法优雅地处理超时和随后出现的“ SQL Server不存在或访问被拒绝”错误消息? workspace.OpenDatabase throws an error that I can catch and deal with, but not before two popups come up and stop my VBA code until an operator clicks OK. workspace.OpenDatabase引发了一个我可以捕获并处理的错误,但是在出现两个弹出窗口并停止我的VBA代码之前,不会出现此错误,直到操作员单击“确定”为止。

DoCmd.SetWarnings False doesn't affect it as the error popups are not actually coming from Access itself - I think they're from the underlying ODBC process or the Jet Engine that drives it. DoCmd.SetWarnings False不会影响它,因为错误弹出窗口实际上并非来自Access本身-我认为它们是来自基础ODBC进程或驱动它的Jet引擎。

Any ideas? 有任何想法吗?

I eventually found something that works by searching 'Suppress ODBC connection failure warnings'. 我最终通过搜索“抑制ODBC连接失败警告”找到了可行的方法。

Courtesy of Trevor Best from http://bytes.com/topic/access/answers/201502-how-suppress-odbc-connection-dialog 由Trevor Best提供, 网址http://bytes.com/topic/access/answers/201502-how-suppress-odbc-connection-dialog

Some code that uses ADO to make the database connection in a way that allows VBA error trapping to catch the error before the system throws any popups at you. 一些使用ADO进行数据库连接的代码,允许VBA错误捕获在系统向您抛出任何弹出窗口之前捕获错误。

Function CanOpenSQLDbLB(pstrServer As String, pstrDb As String, pstrUser
As String, pstrPassword As String, Optional pfReportError As Boolean =
True) As Boolean

On Error GoTo CanOpenSQLDbLB_Err

Dim objConn As Object
Dim strConn As String
Dim strError As String, lngErr As Long
Const cstrSQLErr = "[Microsoft][ODBC SQL Server Driver][SQL Server]"

Set objConn = CreateObject("ADODB.Connection")

strConn = strConn & "DRIVER=SQL Server"
strConn = strConn & ";SERVER=" & pstrServer
strConn = strConn & ";APP=" & Application.Name
strConn = strConn & ";WSID=AWorkstation"
strConn = strConn & ";DATABASE=" & pstrDb

objConn.Open strConn, pstrUser, pstrPassword

CanOpenSQLDbLB = True

CanOpenSQLDbLB_Exit:
On Error Resume Next
objConn.Close
Set objConn = Nothing
Exit Function
CanOpenSQLDbLB_Err:
lngErr = Err.Number
strError = Err.Description

If InStr(1, strError, cstrSQLErr) Then
strError = "Error reported by server" & vbCr & vbCr &
Replace(strError, cstrSQLErr, "")
End If

Select Case lngErr
Case Else
If pfReportError Then
MsgBox strError, 16, "Error #" & Err & " Attempting to
open server database"
End If
End Select
Resume CanOpenSQLDbLB_Exit

End Function

A good solution can be found here: 一个很好的解决方案可以在这里找到:

ACC2000: How to Trap ODBC Logon Error Messages http://support.microsoft.com/kb/210319 ACC2000:如何捕获ODBC登录错误消息http://support.microsoft.com/kb/210319

The above is from Access 2000 and is 14 years old, but as such it still works fine today. 上面的内容来自Access 2000,已有14年历史了,但是今天它仍然可以正常工作。 The other possible advantage is you don't have to adopt and introduce ADO into your application. 另一个可能的优点是您不必采用ADO并将其引入您的应用程序。 For applications that already use or have ADO, then no big deal, but if your application sticks to one data object model, then you not have to potentially introduce ADO. 对于已经使用或具有ADO的应用程序,没什么大不了的,但是如果您的应用程序坚持一个数据对象模型,那么您就不必引入ADO了。

The other BIG bonus of the above is this effectively logs you into the database and thus you avoid having user name and passwords in the linked tables. 上面的另一个BIG好处是,这有效地将您登录到数据库,因此避免了链接表中包含用户名和密码。 This means you can have different users and logons, and NOT have to re-link or embed the user name or password in your linked tables. 这意味着您可以拥有不同的用户和登录名,而不必在链接表中重新链接或嵌入用户名或密码。

This wonderful trick and result of the above connection trick is outlined here: 这里概述了这个奇妙的技巧和上述连接技巧的结果:

Power Tip: Improve the security of database connections 电源提示:提高数据库连接的安全性

http://blogs.office.com/b/microsoft-access/archive/2011/04/08/power-tip-improve-the-security-of-database-connections.aspx http://blogs.office.com/b/microsoft-access/archive/2011/04/08/power-tip-improve-the-security-of-database-connections.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM