4

我正在尝试使用 SQL Server 后端在 Access中创建链接(或导入)表。基本上,业务用户定期需要一个表[SQL Rulesnew]的副本(是的,有空格,叹气)所以我们想给他们一个小的 Access 2003 工具,它可以按需完成这项工作。

我确实尝试过使用Docmd.TransferDataBase acTable但没有运气

这是我正在使用的代码:

Sub getData()

Dim sConnStr As String
Dim oTable As TableDef
Dim sDestinationTable As String
Dim dbs As Database
Dim tbl As DAO.TableDef
Dim tblLinked As DAO.TableDef       

    sDestinationTable = "SQL Rulesnew"
    Set dbs = CurrentDb

    ' source table name has a SPACE (rolleyes!)
    CurrentDb.CreateTableDef sDestinationTable  

    ' got the below from a Data Link File (UDL)
    sConnStr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MYDBNAME;Data Source=MYSERVERNAME"

    ' the below also failed!
    'DoCmd.TransferDatabase acLink, "ODBC Database", "ODBC;Driver={SQL Server};Server=Fos;Database=Hermes_Rep;Trusted_Connection=Yes", acTable, "[Report SQLRulesnew]", "SQLRules" & VBA.Format(Now, "ddmmyyyy")

    'If DCount("*", "MSysObjects", "[Name]='[SQL Rulesnew]' AND [Type] In (1, 4, 6)") > 0 Then
    If IsTable(sDestinationTable) Then
       DoCmd.DeleteObject acTable, sDestinationTable
    End If

    Set tblLinked = dbs.CreateTableDef(sDestinationTable)
    Debug.Print "Linking the " & sDestinationTable
    tblLinked.Connect = sConnStr
    tblLinked.SourceTableName = sDestinationTable
    dbs.TableDefs.Append tblLinked
    tblLinked.RefreshLink

End Sub

Function IsTable(sTblName As String) As Boolean
    'does table exists and work ?
    'note: finding the name in the TableDefs collection is not enough,
    '      since the backend might be invalid or missing
Dim x
    On Error GoTo Coventry
    x = DCount("*", sTblName)
    IsTable = True
    Exit Function

Coventry:
    Debug.Print Now, sTblName, Err.Number, Err.Description
    IsTable = False
End Function

不幸的是我得到一个错误找不到可安装的ISAM就行了dbs.TableDefs.Append tblLinked

我该怎么办?

谢谢菲利普

4

1 回答 1

4

我通过反复试验找到了答案...

基本上我在 Access 中的表名可以在不使用[方括号]的情况下有一个空格,因此以下命令可以正常工作(在删除任何现有对象之后):

DoCmd.TransferDatabase _
  acImport, _
  "ODBC Database", _
  "ODBC;Driver={SQL Server};Server=Fos;Database=Hermes;Trusted_Connection=Yes", _
  acTable, _
  "sourceTable", _
  "targetTable"
于 2013-04-03T09:44:04.253 回答