我有一个旧的Access dB,目前是2000 - 2003 .MDB格式。转换为.ACCDB后,以下代码无法附加SQL Server表。通过代码重新附加SQL表时,我收到以下错误:对象无效或不再设置。它在 cat.Tables.Append tbl 行上失败。 它可以在Access 2013中正常运行.MDB格式dB,但在转换为2007格式后,它会失败。我知道我可以将其保留为MDB文件,但我想转换为避免将来出现问题。我没有帮助尝试过不同的连接字符串设置。
缩写代码:
Dim tbl As New ADOX.Table, cat As New ADOX.Catalog
Dim cnn As New ADODB.Connection
Dim ProviderString As String
Set cat.ActiveConnection = CurrentProject.Connection
ProviderString = "ODBC;" & _
"Provider=SQLOLEDB;" & _
"Driver=SQL Server;" & _
"Server=" & sServerName & ";" & _
"database=" & sDatabaseName & ";" & _
"UID=" & gbldbLogin & ";" & _
"PWD=" & gbldbPassword
TableName = "MyTableName"
DoCmd.DeleteObject acTable, TableName
tbl.Name = TableName
Set tbl.ParentCatalog = cat
tbl.Properties("Jet OLEDB:Link Provider String") = ProviderString
tbl.Properties("Jet OLEDB:Remote Table Name") = TableName
tbl.Properties("Jet OLEDB:Create Link") = True
tbl.Properties("Jet OLEDB:Cache Link Name/Password") = True
cat.Tables.Append tbl
Set tbl = Nothing