【发布时间】:2018-05-14 15:47:15
【问题描述】:
我们已将一些后端数据表从网络驱动器(mbd 文件)移至 SQL Server 数据库中。大部分工作都很好,但如果员工通过 VPN 访问内容(这会大大减慢速度),那么当我们运行检索大量数据的报告时会出现连接错误。我的猜测是我需要将超时设置为更大的值,并且我做了一些研究,似乎我需要设置命令超时(或者可能是查询超时?)。
下面是我们用来将 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
【问题讨论】:
-
Access 对所有链接表使用一个超时值。要更改它,请查看this answer。不过,您可以为每个表设置超时。
-
从长远来看(或不那么长),您应该考虑将部分或全部数据处理移动到服务器 - 使用更精细的视图或存储过程 - 以便减少数据传递给客户端。
-
让我们的查询通过(以及其他方式优化)肯定是待办事项列表中的下一个。
标签: sql-server ms-access vba odbc