Sql server 为什么在currentdb.tabledefs.append之后需要压缩和修复数据库
我试图在Access中使用无DSN连接和VBA创建一个链接表。我在网上找到了非常有用的链接。以下是链接中的代码:Sql server 为什么在currentdb.tabledefs.append之后需要压缩和修复数据库,sql-server,ms-access,vba,Sql Server,Ms Access,Vba,我试图在Access中使用无DSN连接和VBA创建一个链接表。我在网上找到了非常有用的链接。以下是链接中的代码: '//Name : AttachDSNLessTable '//Purpose : Create a linked table to SQL Server without using a DSN '//Parameters '// stLocalTableName: Name of the table that you are creating in the
'//Name : AttachDSNLessTable
'//Purpose : Create a linked table to SQL Server without using a DSN
'//Parameters
'// stLocalTableName: Name of the table that you are creating in the current database
'// stRemoteTableName: Name of the table that you are linking to on the SQL Server database
'// stServer: Name of the SQL Server that you are linking to
'// stDatabase: Name of the SQL Server database that you are linking to
'// stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
'// stPassword: SQL Server user password
Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
On Error GoTo AttachDSNLessTable_Err
Dim td As TableDef
Dim stConnect As String
For Each td In CurrentDb.TableDefs
If td.Name = stLocalTableName Then
CurrentDb.TableDefs.Delete stLocalTableName
End If
Next
If Len(stUsername) = 0 Then
'//Use trusted authentication if stUsername is not supplied.
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
Else
'//WARNING: This will save the username and the password with the linked table information.
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
End If
Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
CurrentDb.TableDefs.Append td
AttachDSNLessTable = True
Exit Function
AttachDSNLessTable_Err:
AttachDSNLessTable = False
MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description
End Function
正如您所看到的,每个tabledef都是使用
CurrentDb.TableDefs.Append td
代码为我传递,没有任何错误。但是这些表没有出现在我的Access中。只有当我压缩并修复数据库时,表才会出现。我不知道为什么会这样 将对象附加到集合后,必须刷新集合:
CurrentDb.TableDefs.refresh
要在数据库窗口中查看chenges:Application.RefreshDatabaseWindow
RefreshDatabaseWindow方法更新数据库窗口(数据库)
窗口:在Access 2003及更早版本中,当
数据库或项目已打开。它显示创建新数据库或项目的快捷方式
数据库对象和打开现有对象。在更高版本中
替换为导航窗格。)在数据库对象(数据库)之后
对象:Access数据库包含表、查询、,
窗体、报表、页面、宏和模块。Access项目包含
对象(如窗体、报表、页面、宏和模块)已被删除
创建、删除或重命名
当然对。Microsoft的参考代码不包括这一点,因为如果要附加多个表,通常只需运行这两条语句一次(在附加所有表之后)。这很有意义。我刚刚添加了Application.RefreshDatabaseWindow,它成功了。不必使用CurrentDb.TableDefs.Refresh@namko
CurrentDb.TableDefs.Refresh
只刷新一个集合TableDefs
,而不刷新数据库窗口。而刷新数据库窗口
刷新七个集合并重新绘制数据库窗口。