【问题标题】:Access - Requery quits after 60 seconds访问 - 重新查询在 60 秒后退出
【发布时间】:2013-07-18 08:43:15
【问题描述】:
我有一个访问子表单,它使用查询定义来执行存储过程(在 SQL Server 上)作为其记录源,主要用于搜索。用户键入标识符,Access 修改查询定义 (exec [procname] [identifier]),请求子表单,然后它应该用过程的结果填充子表单。
问题是,如果 60 秒后没有任何东西返回,它就会放弃。没有错误,没有超时警报,没有警告。只是给人的印象是它收到了一个空的结果集。单步执行代码确认了它 - 它在Subform.Requery 行上停留了整整 60 秒(我已经计时了),然后“好吧,这就够了”,然后继续前进,甚至没有提醒问题。 在这个 sub 中没有错误处理(没有狡猾的 On Error Resume Next 或任何东西 - 如果有任何问题,它应该炸弹)。
我已经从调试器中直接复制了查询定义,同时单步执行代码并在 SQL Server Management Studio 中触发它,它确实有效。只需超过 60 秒。
为什么访问在 60 秒后就认输了?我怎样才能强迫它等待更长的时间?
【问题讨论】:
标签:
sql
sql-server
ms-access
【解决方案1】:
在设计视图中打开您的查询。在视图菜单下,选择属性。
当“查询属性”窗口出现时,将“ODBC 超时”属性设置为 0。
默认设置为 60,表示查询将在 60 秒后超时。通过将 ODBC 超时值更改为 0,Access 将永远不会超时。
此属性特定于您正在处理的查询。因此,如果您对其他查询有任何问题,则需要对每个查询重复相同的步骤。
您也可以使用类似这样的方式在代码中设置它(您可以将其减少到您需要的 1 或 2 行...:
Sub ODBCTimeoutX()
Dim dbsCurrent As Database
Dim qdfStores As QueryDef
Dim rstStores As Recordset
Set dbsCurrent = OpenDatabase("Northwind.mdb")
' Change the default QueryTimeout of the Northwind
' database.
Debug.Print "Default QueryTimeout of Database: " & _
dbsCurrent.QueryTimeout
dbsCurrent.QueryTimeout = 30
Debug.Print "New QueryTimeout of Database: " & _
dbsCurrent.QueryTimeout
' Create a new QueryDef object.
Set qdfStores = dbsCurrent.CreateQueryDef("Stores", _
"SELECT * FROM stores")
' Note: The DSN referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the SQL Server.
qdfStores.Connect = _
"ODBC;DATABASE=pubs;DSN=Publishers"
' Change the ODBCTimeout setting of the new QueryDef
' object from its default setting.
Debug.Print "Default ODBCTimeout of QueryDef: " & _
qdfStores.ODBCTimeout
qdfStores.ODBCTimeout = 0
Debug.Print "New ODBCTimeout of QueryDef: " & _
qdfStores.ODBCTimeout
' Execute the query and display the results.
Set rstStores = qdfStores.OpenRecordset()
Debug.Print "Contents of recordset:"
With rstStores
Do While Not .EOF
Debug.Print , .Fields(0), .Fields(1)
.MoveNext
Loop
.Close
End With
' Delete new QueryDef because this is a demonstration.
dbsCurrent.QueryDefs.Delete qdfStores.Name
dbsCurrent.Close
End Sub