我试图避免发生超时情况。目前该脚本在测试环境中运行100%,但一切都在本地运行。因此,现在安装在实时环境中,它需要更长的时间。但是,它会出现VB超时默认设置为30秒。然而,不像SQL那样100%熟悉VB,那么我不确定要设置它的代码。当前的代码如下:SQL和VB脚本超时
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim dateRows As Variant
Dim i As Integer
Dim today As Date
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
strFile = Workbooks(1).FullName
strCon = "Provider=SQLOLEDB.1; Data Source=ABC;Initial catalog=ABC;Integrated Security=ABC;"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
range1 = Sheets("Line data (Do not alter)").Range("AA9")
cn.Open strCon
strSQL = "Select "order by userid, appointmentdate "
rs.Open strSQL, cn
Sheets("Line data (Do not alter)").Range("AA9").CopyFromRecordset rs
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
我发现代码其中规定comm.CommandTimeout = 10,但不确定放在哪里..
任何帮助表示赞赏。
在你的情况下,它会' cn.CommandTimeout = 10'(或者任何你想设定的超时时间),因为cn是连接对象的变量,而CommandTimeout是这个对象的一个属性。由于您使用的是早期绑定,因此您可以在'cn'后输入'.'来查看Connection对象的所有属性。 –
完美 - 谢谢。你知道如何编写一个按钮,如果我将它设置为0并且它需要很长时间,将强制退出sql脚本? – HAS
*如果我将它设置为0并且需要很长的时间* - 只要设置它,无论什么时候您觉得“花费太长时间”而且您很棒。如果您想使其更清洁并继续前进,而不会将错误传递给用户,则还可能会出现错误信息。 –