[英]Changing SQL connection information for DSN-less Access frontend
I've got a mission-critical Access 2003 database that changed from a local MDB, to an MDB frontend with the backend on MS SQL Server 2005, using the Microsoft SQL Server Database Migration Assistant (SSMA) software. 我有一个任务关键型Access 2003数据库,它使用Microsoft SQL Server数据库迁移助手(SSMA)软件从本地MDB更改为使用MS SQL Server 2005后端的MDB前端。
Now, I need to permanently change the server that the tables are linked to from an IP address (which is changing soon) to a hostname pointing to the same server. 现在,我需要永久地将表所链接的服务器从IP地址 (即将更改)更改为指向同一服务器的主机名 。 The server itself is not changing, just the connection string.
服务器本身不会改变,只是连接字符串。
It's a DSN-less connection, so the ODBC info is contained within the Access MDB file. 它是无DSN连接,因此ODBC信息包含在Access MDB文件中。 If I try to refresh the table links within Access, it prompts me for a DSN (which I don't want to use).
如果我尝试刷新Access中的表链接,它会提示我输入DSN(我不想使用它)。
I've done some Googling, and I've found several scraps of code to have it update itself each time the program launches. 我做了一些谷歌搜索,我发现了几个代码片段,每次程序启动时都会自动更新。 But, I'm worried that that could potentially introduce problems or delays for the users.
但是,我担心这可能会给用户带来问题或延迟。 Is that my best option, or is there some trick to permanently change the connection string stored within the MDB?
这是我最好的选择,还是有一些技巧可以永久更改存储在MDB中的连接字符串?
You can use VBA to alter the .Connect
properties for your linked TableDef
s. 您可以使用VBA更改链接的
TableDef
的.Connect
属性。
See this sample from the Immediate window. 从立即窗口中查看此示例。 (I used
Replace()
simply to split up that long line.) (我使用
Replace()
只是为了分割那条长线。)
? Replace(CurrentDb.TableDefs("remote_table").Connect, ";", ";" & vbCrLf)
ODBC;
DRIVER=SQL Server Native Client 10.0;
SERVER=HP64\SQLEXPRESS;
Trusted_Connection=Yes;
APP=Microsoft Office 2003;
WSID=WIN732B;
DATABASE=testbed;
So I could build a new string with a different SERVER, and assign the new string to the TableDef .Connect
property. 所以我可以使用不同的SERVER构建一个新字符串,并将新字符串分配给
TableDef .Connect
属性。
If this is intended to be a permanent change you should only need to do it one time, not every time you open the database. 如果这是一个永久性更改,您只需要执行一次,而不是每次打开数据库时都这样做。
When I've done similar connection changes, it has been between different servers. 当我完成类似的连接更改时,它位于不同的服务器之间。 So I deleted the TableDef and re-created it anew, to make sure Access didn't keep any cached meta information about that connection which would now be out of date.
所以我删除了TableDef并重新创建它,以确保Access没有保留任何有关该连接的缓存元信息,这些信息现在已经过时了。 However, in your case, you're dealing with the same physical server, just referencing it by name instead of IP.
但是,在您的情况下,您正在处理相同的物理服务器,只是通过名称而不是IP来引用它。 I doubt the cached information would be a concern for you.
我怀疑缓存的信息对你来说是一个问题。
The following code has served me well for years: 以下代码多年来一直很好用:
Function LinkTable(DbName As String, SrcTblName As String, _
Optional TblName As String = "", _
Optional ServerName As String = DEFAULT_SERVER_NAME, _
Optional DbFormat As String = "ODBC") As Boolean
Dim db As dao.Database
Dim TName As String, td As TableDef
On Error GoTo Err_LinkTable
If Len(TblName) = 0 Then
TName = SrcTblName
Else
TName = TblName
End If
'Do not overwrite local tables.'
If DCount("*", "msysObjects", "Type=1 AND Name=" & Qt(TName)) > 0 Then
MsgBox "There is already a local table named " & TName
Exit Function
End If
Set db = CurrentDb
'Drop any linked tables with this name'
If DCount("*", "msysObjects", "Type In (4,6,8) AND Name=" & Qt(TName)) > 0 Then
db.TableDefs.Delete TName
End If
With db
Set td = .CreateTableDef(TName)
td.Connect = BuildConnectString(DbFormat, ServerName, DbName)
td.SourceTableName = SrcTblName
.TableDefs.Append td
.TableDefs.Refresh
LinkTable = True
End With
Exit_LinkTable:
Exit Function
Err_LinkTable:
'Replace following line with call to error logging function'
MsgBox Err.Description
Resume Exit_LinkTable
End Function
Private Function BuildConnectString(DbFormat As String, _
ServerName As String, _
DbName As String, _
Optional SQLServerLogin As String = "", _
Optional SQLServerPassword As String = "") As String
Select Case DbFormat
Case "NativeClient10"
BuildConnectString = "ODBC;" & _
"Driver={SQL Server Native Client 10.0};" & _
"Server=" & ServerName & ";" & _
"Database=" & DbName & ";"
If Len(SQLServerLogin) > 0 Then
BuildConnectString = BuildConnectString & _
"Uid=" & SQLServerLogin & ";" & _
"Pwd=" & SQLServerPassword & ";"
Else
BuildConnectString = BuildConnectString & _
"Trusted_Connection=Yes;"
End If
Case "ADO"
If Len(ServerName) = 0 Then
BuildConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & DbName & ";"
Else
BuildConnectString = "Provider=sqloledb;" & _
"Server=" & ServerName & ";" & _
"Database=" & DbName & ";"
If Len(SQLServerLogin) > 0 Then
BuildConnectString = BuildConnectString & _
"UserID=" & SQLServerLogin & ";" & _
"Password=" & SQLServerPassword & ";"
Else
BuildConnectString = BuildConnectString & _
"Integrated Security=SSPI;"
End If
End If
Case "ODBC"
BuildConnectString = "ODBC;" & _
"Driver={SQL Server};" & _
"Server=" & ServerName & ";" & _
"Database=" & DbName & ";"
If Len(SQLServerLogin) > 0 Then
BuildConnectString = BuildConnectString & _
"Uid=" & SQLServerLogin & ";" & _
"Pwd=" & SQLServerPassword & ";"
Else
BuildConnectString = BuildConnectString & _
"Trusted_Connection=Yes;"
End If
Case "MDB"
BuildConnectString = ";Database=" & DbName
End Select
End Function
Function Qt(Text As Variant) As String
Const QtMark As String = """"
If IsNull(Text) Or IsEmpty(Text) Then
Qt = "Null"
Else
Qt = QtMark & Replace(Text, QtMark, """""") & QtMark
End If
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.