访问 - 60秒后退出重新查询

时间:2013-07-18 08:43:15

标签: sql sql-server ms-access

我在访问中有一个子窗体,它使用查询定义来执行存储过程(在SQL Server上)作为其记录源,主要是为了搜索的目的。用户键入标识符,Access修改查询定义(exec [procname] [identifier]),重新查询子表单,然后使用过程结果填充子表单。

问题是,如果在60秒之后如果没有任何东西回来,它会放弃。没有错误,没有超时警报,没有警告。只是给人的印象是它收到了一个空的结果集。单步执行代码就可以确认它 - 它在Subform.Requery行上停留了整整60秒(我已经计时了)然后“很好地完成了这一点”并继续前进,甚至没有提醒问题。 此子中没有错误处理(没有狡猾的On Error Resume Next或其他任何东西 - 如果有任何问题,它应该炸弹)。

我已经直接从调试器中复制了查询定义,同时逐步执行代码并将其解压缩到SQL Server Management Studio中,它确实有效。它只需要超过60秒。

为什么60秒后访问权限会被丢弃?我怎么能强迫它等待更长时间?

1 个答案:

答案 0 :(得分: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 
相关问题