简体   繁体   中英

MS Access - Linked Tables - DB Owner / Sysadmin

we have a problem with the access linked table wizard. We would like to transfer access tables to an sql server and then link the tables. We would like to have the option that multiple users can do this. Currently we discover that it is only possible to link tables successfully if we use an sql account with sysadmin rights or if the sql user is the owner of the destination database.

Is there a way to enable users creating linked tables without having sysadmin rights and being the owner of the destination database? I thought it would be possible to create linked tables if I use an sql user with the db_owner role assigned for the destination db, but this does not work.

Please help me.

Thank you in advance.

Kind regards

Florian

This answer was long in coming. I modified the code from here and changed it to copy from a local table to a table in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM