• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

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

Hi

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
Emil
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;" & _
        "Pwd=password;"
   
   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
       Else
           CAT.Tables.Append TBL
       End If
       numrec = numrec + 1
       rst.MoveNext
       Set TBL = Nothing
   Loop
   rst.Close
   Set rst = Nothing
   Set CAT = Nothing
   Set TBL = Nothing
End Sub

Open in new window

0
itcouple
Asked:
itcouple
2 Solutions
 
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:

http://www.accessmvp.com/djsteele/DSNLessLinks.html

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.
0
 
jefftwilleyCommented:
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.

J

0
 
itcoupleAuthor Commented:
Hi

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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now