Results 1 to 7 of 7

Thread: How to add ODBC linked table in an Access database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    How to add ODBC linked table in an Access database

    MS Access, VB6, ODBC

    Is it possible, under program control, to dynamically add a ODBC Linked table into access?

    ie I want to create a tempory table in Access which is looking at another database using the ODBC interface
    chilling

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to add ODBC linked table in an Access database

    Try this

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: How to add ODBC linked table in an Access database

    I am trying to use ADOX to dynamically create a "LINKED" ODBC table in an Access database back to DSN table but am not sure what the ODBC syntax is for the connections string. This is the code ....
    Code:
    cnnToLocalAccessMDB = = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\.....\LocalMDB.mdb"
    strLocalTableName = "tblStock"
    strRemotePathFileMDB = "Provider=MSDASQL.1;Password=tx;Persist Security Info=True;User ID=txdemo;Extended Properties='DSN=SageLine50 Demo;UID=txdemo;PWD=tx;'"
    strRemoteTableName = "STOCK"
    
    Dim catLocal As ADOX.Catalog
    Set catLocal = New ADOX.Catalog
    catLocal.ActiveConnection = cnnToLocalAccessMDB
    
    Set tblRemote = New ADOX.Table
    With tblRemote
    Set .ParentCatalog = catLocal
    .Name = strLocalTableName
    .Properties.Item("Jet OLEDB:Create Link").Value = True
    .Properties.Item("Jet OLEDB:Link Datasource").Value = strRemotePathFileMDB
    .Properties.Item("Jet OLEDB:Remote Table Name").Value = strRemoteTableName
    End With
    
    catLocal.Tables.Append tblRemote


    This is the Error that occures on the line catLocal.Tables.Append tblRemote :
    Code:
    Could not find file
    'C:\...\Provider=MSDASQL.1;Password=tx;Persist Security Info=True;User ID=txdemo;Extended Properties='DSN=SageLine50 Demo;UID=txdemo;PWD=tx;'
    It appears as though it is not finding the correct Sage Database from the DSN although if I test the DSN it all appears to be working correctly.

    Anyone with any thoughts on this one?
    chilling

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: How to add ODBC linked table in an Access database

    For those who might need this functionality .... my two errors were
    • I should have used Jet OLEDB:Link Provider String rather than Jet OLEDB:Link Datasource
    • The connection string only needs to "ODBC;DSN=SageLine50 Demo;UID=txdemo;PWD=tx;Persist Security Info=True;"
    ... but I still have not managed to stop the ODBC link binging up its own UI requesting a username and password even though I am passing one ops: ... if anyone has any ideas on that one ...?
    chilling

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: How to add ODBC linked table in an Access database

    I have now managed (with lots of help from you guys )to get all that working with a new LINKED table being created in Access but I now need to flag one of the fields as the Primary Key. This is the code (it creates the table but not the index) ...
    VB Code:
    1. Function CreateACCLinkTableInQW(ByVal lcnnQW As String, ByVal lstrQWTable As String, ByVal lcnnACC As String, ByVal lstrACCTable As String) As Boolean
    2.  
    3. Dim catLocal As ADOX.Catalog
    4. Dim tblLinked As ADOX.Table
    5. Dim inxLinked As New ADOX.Index
    6. Dim lintTable As Integer
    7.  
    8. ' Create a connection to QuoteWerks
    9. Set catLocal = New ADOX.Catalog
    10. catLocal.ActiveConnection = lcnnQW
    11.  
    12.  
    13. For lintTable = 0 To catLocal.Tables.Count - 1
    14.     If CStr(catLocal.Tables.Item(lintTable).Name) = lstrQWTable Then
    15.         lblnTable = True
    16.         CreateACCLinkTableInQW = True
    17.         Exit For
    18.     End If
    19. Next ' Table
    20. 'If the table does not exist create it
    21. If Not lblnTable Then
    22.    
    23.     Set tblLinked = New ADOX.Table
    24.     With tblLinked
    25.         Set .ParentCatalog = catLocal
    26.         .Name = lstrQWTable
    27.         .Properties.Item("Jet OLEDB:Create Link").Value = True
    28.         .Properties.Item("Jet OLEDB:Link Provider String").Value = lcnnACC
    29.         .Properties.Item("Jet OLEDB:Remote Table Name").Value = lstrACCTable
    30.     End With
    31.    
    32.     ' Add the primary key index
    33.     With inxLinked
    34.         .Name = "inxStock_Code"
    35.         .Columns.Append "STOCK_CODE"
    36.         .Columns("STOCK_CODE").SortOrder = adSortAscending
    37.         .PrimaryKey = True
    38.         .Unique = True
    39.     End With
    40.     tblLinked.Indexes.Append inxLinked
    41.     Set inxLinked = Nothing
    42.  
    43.     catLocal.Tables.Append tblLinked
    44.     Set tblLinked = Nothing
    45.     CreateACCLinkTableInQW = True
    46. End If
    47.  
    48. End Function
    The following error appears on the line catLocal.Tables.Append tblLinked:
    Invalid arguement
    any ideas ?
    chilling

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: How to add ODBC linked table in an Access database

    Also there must be a simplier way to replace this bit of ugly code :
    VB Code:
    1. For lintTable = 0 To catLocal.Tables.Count - 1
    2.     If CStr(catLocal.Tables.Item(lintTable).Name) = lstrQWTable Then
    3.         lblnTable = True
    4.         CreateACCLinkTableInQW = True
    5.         Exit For
    6.     End If
    7. Next ' Table
    ie test for the existance of a table without having to loop arround all the tables.
    chilling

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: How to add ODBC linked table in an Access database

    I am confused about Keys and Indexes in MS Access and especially LINKED tables.

    I understand to use a LINKED table in Access in JOIN to update another table the table has to have a Primary Key (KP). Does that mean that I have to use the tblLinked.Indexes.Append inxLinked or the tblLinked.Keys.Append keyLinked command?

    In either case I am confused with the syntax...
    VB Code:
    1. Dim keyRemote As New ADOX.Key
    2.     With keyLinked
    3.         .Name = "keySTOCK_CODE"
    4.         .Type = adKeyPrimary
    5.         .Columns.Append "STOCK_CODE"
    6.     End With
    7.     tblLinked.Keys.Append keyLinked
    8.     tblLinked.Keys.Refresh
    9.     catLocal.Tables("SageStock").Keys.Append keyLinked
    10.     Set keyRemote = Nothing
    What I am confused about is should I be creating the table first and then adding the Keys and/or Indexes or should the Keys and Indexes be added to the tblLinked object before the table is Appended to the Catalog. It does not appear to matter with a normal Access table but I can not get it to work either way for a LINKED table.

    I orginally made the assumption that it had to be before add ing the tblLinked object to the Catalog, as this appears to be the case within Access. (ie once you have linked a table you can not add the Primary Key you have to do that at the time of adding the table).

    Anyone know or have any view on any of the above?
    chilling

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width