0

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

5
  • What is the connection string and what code are you using to create the table?
    – HelloW
    Feb 25, 2014 at 17:44
  • I am using the Upsizing Wizard of MS Access
    – Flok18
    Feb 25, 2014 at 17:47
  • Is this a recurring process or a one time scenario?
    – HelloW
    Feb 25, 2014 at 17:54
  • I don't have time right now to answer this but I will get back to it if it still needs attention later today. You will most likely want to create your sql server tables via an executed sql statement that you generate by looking at your local table and then create a link to that table then use an insert statement to put the data in.
    – HelloW
    Feb 25, 2014 at 18:10
  • One more question, can you give the reason why you want to do this? It seems outside of normal practice. This isn't to say it isn't a good idea.
    – HelloW
    Feb 25, 2014 at 19:24

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.