运行时错误“-2147217871(80040e31)”:查询超时已过期

当我尝试运行这个大的查询时出现这个错误。 较小的查询它不。 我已经设置连接超时为0,这并没有帮助。 它看起来像连接,并将其挂在代码的.Open部分。 感谢您的任何帮助。

Public Sub dbConnectTDaY() Dim cn As ADODB.Connection Dim Rec_set As ADODB.Recordset Dim MyConn, varSQL As String Dim Rw As Long, Col As Long, c As Long Dim MyField, Location As Range Dim i As Integer Dim strSQL As String Dim strStDt As String Dim strEnDt As String strStDt = ThisWorkbook.Worksheets("xxx").Range("B6").Value strEnDt = ThisWorkbook.Worksheets("xxx").Range("B5").Value Set cn = New ADODB.Connection Set Rec_set = New ADODB.Recordset Set Location = [A2] Rw = Location.Row Col = Location.Column c = Col strSQL = "" strSQL = strSQL & "SELECT gp.cnt" strSQL = strSQL & ",gp.pod" strSQL = strSQL & ",gp.grp_paddsa" strSQL = strSQL & ",gp.grp_rasdd" ' etc......... cn.ConnectionTimeout = 0 'To wait till the query finishes without generating error cn.Open "DSN=#EDWP;Databasename=INTY;Uid=XXXXX;Pwd=XXXXX;" If cn.State = adStateOpen Then 'If connection is success, continue 'Check for errors... Rec_set.Open strSQL, cn 'Issue SQL statement For i = 0 To Rec_set.Fields.Count - 1 ActiveSheet.Cells(1, i + 1) = Rec_set.Fields(i).Name Next i Do Until Rec_set.EOF For Each MyField In Rec_set.Fields Cells(Rw, c) = MyField c = c + 1 Next MyField Rec_set.MoveNext Rw = Rw + 1 c = Col Loop End If Rec_set.Close cn.Close End Sub 

cn.ConnectionTimeout = 0只影响连接上的超时。

你也想设置CommandTimeout