Vba 否则,代码部分会一直超时

Vba 否则,代码部分会一直超时,vba,ms-access,Vba,Ms Access,我有一个代码,可以在打开表单时检查access和sql server之间的连接。如果存在连接,则会弹出一个消息框并显示。如果没有,则应该有一个消息框,指示没有连接。相反,我得到了一个错误: Run Time Error '-2147467259 (80004005)': [DBNETLIB][ConnectionOpen (Connect()).]Specified SQL Server Not Found 这不是我想要它做的,是我的代码中的一些东西,还是没有办法让它工作 Public Sub

我有一个代码,可以在打开表单时检查access和sql server之间的连接。如果存在连接,则会弹出一个消息框并显示。如果没有,则应该有一个消息框,指示没有连接。相反,我得到了一个错误:

Run Time Error '-2147467259 (80004005)':
[DBNETLIB][ConnectionOpen (Connect()).]Specified SQL Server Not Found
这不是我想要它做的,是我的代码中的一些东西,还是没有办法让它工作

Public Sub AutoExec()
Dim cnn As ADODB.Connection
Dim localrst As New ADODB.Recordset
Dim remoterst As New ADODB.Recordset

Set cnn = New ADODB.Connection
cnn.Open "Provider=SQLOLEDB; Data Source=DB; Initial Catalog=HRLearnDev;" _
& "User Id=ID; Password=PW;"

If cnn.State = adStateOpen Then

MsgBox ("You have an established connection with the L&TD SQL Server Database.")
Else
MsgBox ("Cannot connect to remote server. Data will be stored locally  to CDData Table until application is opened again.")

End If

cnn.Close



End Sub

在这种情况下,您通常希望在错误转到时使用
构造,然后在发生错误时将代码发送给错误处理程序(您可以进行测试,以确保错误号与
Err.Num
的预期值一致)

但是,在您的情况下,在下一次错误恢复时使用
可能更容易。这会告诉解释器“如果发生错误,请转到下一行。我会找出错误并处理它。”

通常在单个函数调用产生错误或合理值时执行此操作。我经常这样做:

On Error Resume Next
returnValue = -1
returnValue = functionThatReturnsPositiveValue()
If returnValue < 0 Then
  MsgBox "oops - the function failed!"
Else
  ' <<<< do whatever needs doing >>>>
End If
出错时继续下一步
returnValue=-1
returnValue=函数returnsPositiveValue()
如果返回值<0,则
MsgBox“oops-函数失败!”
其他的
' >
如果结束
在你的情况下,这几乎就是你要做的。完整示例:

Public Sub AutoExec()
  Dim cnn As ADODB.Connection
  Dim localrst As New ADODB.Recordset
  Dim remoterst As New ADODB.Recordset

  On Error Resume Next          ' <<<<<< add this line so an error doesn't stop the code
  Set cnn = New ADODB.Connection
  cnn.State = 0 ' <<<<< not sure if you need something like this, or if the New command
                        already set it to some sensible value other than "adStateOpen"

  cnn.Open "Provider=SQLOLEDB; Data Source=DB; Initial Catalog=HRLearnDev;" _
    & "User Id=ID; Password=PW;"

  If cnn.State = adStateOpen Then        ' <<<<<< this will only be true if no error occurred
    MsgBox ("You have an established connection with the L&TD SQL Server Database.")
  Else
    MsgBox ("Cannot connect to remote server. Data will be stored locally  to CDData Table until application is opened again.")
  End If
  On Error GoTo 0     ' <<<<<<<< turn off error handling - we have passed the "tricky" spot.

'  <<<<<< lots more code goes here >>>>>>

  If cnn.State = adStateOpen Then cnn.Close  ' <<<<<<<< only close connection if it was open!!

End Sub
Public Sub AutoExec()
将cnn设置为ADODB.Connection
将localrst设置为新的ADODB.Recordset
将remoterst设置为新ADODB.Recordset

在错误恢复下一步“这看起来怎么样?”?我是一个新手当谈到这个东西,工作得很漂亮,第二个窗口现在弹出。但当我单击ok时,我得到:运行时错误3704。对象关闭时不允许操作。它突出了代码的cnn.close部分,当然会的。无法关闭未打开的连接。我修改了最后一行,以显示这是如何完成的。您还可以将
Exit Function
视为从函数中“提前退出”的一种方式(当数据库未打开时,您可能希望完全转到其他地方-我不知道您的其余代码是如何构造的…)