Create linked tables (SQL Server) --- table's don't work, cannot delete data in tables


I have some code which created linked table based on sql server (see attachd) however when I use access database and fire simple delete like this one
CurrentDb.Execute ("Delete * From " & tablename & " Where UserId='" & selUser & "'")
then it fails with the following error: "Could not delete from specified tables" Run-time error '3076'

When I link this particular table manually I get a pop up to create 'unique key' and after adding to the code dbSeeChanges + dbFailOnError that works fine.

So my question are:
1) how can I specify 'unique key' which usually will be multiple fields
2) Is there another way to make it work? I would prefer automation to avoid humna errors etc.

Many thanks in advance
Public Sub CreateLinkedTables()

   Dim CAT As ADOX.Catalog
   Dim TBL As ADOX.Table
   Dim xDbPath As String
   Dim sConnString As String
   sConnString = "ODBC;" & _
        "Driver={SQL Server};" & _
        "Server={server name};" & _
        "Database=database;" & _
        "Uid=user;" & _
   Set CAT = New ADOX.Catalog
   ' Open the catalog.
   CAT.ActiveConnection = CurrentProject.Connection
  ' modification ... start
   DoCmd.SetWarnings (warningsOff)
   Dim strSQl As String
   strSQl = "SELECT * FROM dbo_tbl_TablesForLinking "
   Dim rst As New ADODB.Recordset
   rst.ActiveConnection = CurrentProject.Connection
   Dim numrec As Integer
   Dim xTblName As String
   rst.Open strSQl
   numrec = 0
   Do Until rst.EOF
    Set TBL = New ADOX.Table
    Set TBL.ParentCatalog = CAT
      '  Here is where I would like to check if link already exists.
   '  If it exists I would like to delete the link and add the new link to the new data source (xDbPath)
   '  The follow code does not work... I have changed it many ways without success
     ' Set the properties to create the link.

        xTblName = rst(0)  ' get each table from list
        TBL.Name = xTblName
       TBL.Properties("Jet OLEDB:Link Provider String") = sConnString
       TBL.Properties("Jet OLEDB:Remote Table Name") = xTblName
       TBL.Properties("Jet OLEDB:Create Link") = True
       If (TBL.Properties("Jet OLEDB:Link Datasource") <> "") Then
            TBL.Name = xTblName
           CAT.Tables.Delete TBL.Name
           CAT.Tables.Append TBL
           CAT.Tables.Append TBL
       End If
       numrec = numrec + 1
       Set TBL = Nothing
   Set rst = Nothing
   Set CAT = Nothing
   Set TBL = Nothing
End Sub

Open in new window

LVL 10
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
DAO is the recommended method to do this. Doug Steele has a nice article about this:

The article shows how to create the _uniqueindex to make sure things work.

Is a Primary Key defined on your SQL table? Generally Access will pick that up correctly and use it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
In addition to Scott's information, you'll also find that you must have a default value in any Boolean type fields in the tables that you're linking from SQL. Access doesn't play with null boolean fields in SQL.

Personally, I use DSN-Less connections and have no issues at all linking to SQL.


itcoupleAuthor Commented:

The Access db is a complete mess without primary keys in all tables and everything is done using VBA instead of SQL!! and because I have very tight budget and must move to sql to do report models in this particular case I just linked the tables manually as I don't want to make changes to tables even if I should due to the budget :(

But the new way I used the code but got some errors and thought it's not worth it....I will just do that manually :) at least I know there isn't any magic way to do it faster and easier :p

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.