MS Access - 链接表 - 数据库所有者/系统管理员

时间:2014-02-25 17:09:25

标签: sql-server ms-access

我们遇到了访问链接表向导的问题。我们想将访问表传输到SQL Server,然后链接表。我们希望有多个用户可以选择这样做。目前我们发现,如果我们使用具有sysadmin权限的sql帐户或者sql用户是目标数据库的所有者,则只能成功链接表。

有没有办法让用户在没有sysadmin权限且成为目标数据库所有者的情况下创建链接表?我认为如果我使用具有为目标数据库分配的db_owner角色的sql用户,则可以创建链接表,但这不起作用。

请帮帮我。

提前谢谢。

亲切的问候

弗洛里安

1 个答案:

答案 0 :(得分:0)

这个答案很快就会到来。我修改了here中的代码并将其更改为从本地表复制到SQL Server中的表。

Public Sub CopySchemaAndData_ADOX(ByVal sourceTableName As String, ByVal destinationTableName As String)
On Error GoTo Err_Handler

Dim cn As ADODB.Connection
Dim cat As ADOX.Catalog
Dim cnSQL As ADODB.Connection
Dim catSQL As ADOX.Catalog
Dim sourceTable As ADOX.Table
Dim destinationTable As ADOX.Table

Set cnSQL = New ADODB.Connection
'Set cnSQL = CurrentProject.Connection
cnSQL.Provider = "MSDASQL"
cnSQL.ConnectionString = gstrOLEDBConnection 'put your connection string here. the user needs to be able to create tables on the database
cnSQL.Open
Set catSQL = New ADOX.Catalog
Set catSQL.ActiveConnection = cnSQL

Set destinationTable = New ADOX.Table
destinationTable.Name = destinationTableName

Set cn = CurrentProject.Connection
Set cat = New ADOX.Catalog
Set cat.ActiveConnection = cn
Set sourceTable = cat.Tables(sourceTableName)

Dim col As ADOX.Column
For Each col In sourceTable.Columns
   Dim newCol As ADOX.Column
   Set newCol = New ADOX.Column

   With newCol
      .Name = col.Name
      .Attributes = col.Attributes
      .DefinedSize = col.DefinedSize
      .NumericScale = col.NumericScale
      .Precision = col.Precision
      .Type = col.Type
   End With

   destinationTable.Columns.Append newCol
Next col

Dim key As ADOX.key
Dim newKey As ADOX.key

Dim KeyCol As ADOX.Column
Dim newKeyCol As ADOX.Column
For Each key In sourceTable.keys
   Set newKey = New ADOX.key
   newKey.Name = key.Name
   For Each KeyCol In key.Columns
      Set newKeyCol = destinationTable.Columns(KeyCol.Name)
      newKey.Columns.Append (newKeyCol)
   Next KeyCol

   destinationTable.keys.Append newKey
Next key

catSQL.Tables.Append destinationTable

'To do...
'Link the new sql table
'Finally, copy data from source to destination table
'Dim sql As String
'sql = "INSERT INTO " & destinationTableName & " SELECT * FROM " & sourceTableName
'CurrentDb.Execute sql

Err_Handler:
   Set cat = Nothing
   Set catSQL = Nothing
   Set key = Nothing
   Set col = Nothing
   Set sourceTable = Nothing
   Set destinationTable = Nothing
   Set cnSQL = Nothing
   Set cn = Nothing

   If Err.Number <> 0 Then
      msgBox Err.Number & ": " & Err.Description, vbCritical, Err.Source
   End If
End Sub