首页 文章

在Access前端为链接的SQL表/视图设置commandtimeout

提问于
浏览
1

我们已将一些后端数据表从网络驱动器(mbd文件)移到SQL Server数据库上 . 事情通常很有效,但如果员工通过VPN访问事物(这会减慢很多事情),那么当我们运行检索大量数据的报告时,我们会收到连接错误 . 我的猜测是我需要将超时设置为更大的值,我做了一些研究,似乎我需要设置commandtimeout(或者可能是查询超时?) .

下面是我们用于将SQL Server表/视图从SQL Server后端连接到Access前端的VBA代码 . 我是对的,我可能需要指定命令时间吗?在这里我们会添加commandtimeout(或其他超时)值?

Public Sub CreateSQLLinkedTable(strSourceTableName As String, strNewTableName As String)

'************************************************************************************
'* Create a linked table in the current database from a table in a different SQL Server file.
'* In:                                                                              *
'*   strNewTableName - name of linked table to create                               *
'*   strSourceTableName - name of table in database                             *
'************************************************************************************

Setup:

    Dim tdf As TableDef
    Dim strConnect As String, strMsg As String
    Dim myDB As Database

    ' set database vars
    Set myDB = CurrentDb
    Set tdf = myDB.CreateTableDef(strNewTableName)

MakeConnections:

    On Error GoTo OnError

    ' turn system warnings off
    DoCmd.SetWarnings False

    ' define connect string and source table
    ' We do not need to specify the username (Uid) and password (Pwd) in this connection
    ' string, because that information is already cached from the connection to the SQL
    ' Projects database that we created in CheckSQLConnection() that was run to check connection
    ' to the database.  So here we can have a connection string without the Uid and Pwd.
    With tdf
        .Connect = "ODBC;Driver={SQL Server};" & _
                  "server=" & myServer & ";" & _
                  "database=" & mySQLDB & ";"

        .SourceTableName = strSourceTableName
    End With

    ' execute appending the table
    myDB.TableDefs.Append tdf

    ' turn system warnings back on
    DoCmd.SetWarnings True

ExitProgram:
    ' this block of code will run if there are no errors

    Exit Sub

OnError:
    ' this block of code runs if there is an error, per On Error assignment above

    ' display error message with details
    MsgBox "There was an error connecting to the SQL Server data source Projects. Error = " & err & ", Description: " & err.Description

    'exit Projects
    Call CloseFormsAndQuit

End Sub

1 回答

  • 2

    有一个ODBC超时属性 . 在设计视图中打开查询,然后转到属性以查看它 . 当前数据库属性页面上还有一个(ODBC)查询超时 . 您也可以通过编程方式设置它:

    Dim objDB As DAO.Database
    Set objDB = CurrentDb()
    objDB.QueryTimeout = 120
    

    http://www.geeksengine.com/article/how-to-change-timeout-value-for-access-sql.html
    另请检查服务器配置 . 服务器端有一个查询超时 .

相关问题