CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2001
    Posts
    6

    creating linked-tables in Access by code

    I need to create a linked table from a table in SQL-server database.
    I know it can be done by way of creating a DSN first and then linking using the code

    tabledef1.name = "A"
    tableDef1.Soucertablename="SQLA"
    tabledef1.Connect="ODBC;Driver=SQL Server;......"

    But I need to do all this DSN-less..Is it possible.

    Please comment on this.I need this info urgent.
    Thanks in advance

    Anand Sagar
    [email protected]



  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: creating linked-tables in Access by code

    Try this. It will link table to Access db

    'ref ADO and ADOX

    Dim cat As New ADOX.Catalog
    Dim tbl As New ADOX.Table
    Dim cnAccess As New ADODB.Connection

    Private Sub cmdDeleteLinkedTable_Click()

    cat.Tables.Delete "lnkAuthors"
    MsgBox "Link Deleted"

    End Sub

    Private Sub Form_Load()

    cmdDeleteLinkedTable.Caption = "Delete Linked Table"
    cmdLinkTable.Caption = "Link Table"
    optNoRefresh.Caption = "Don't Refresh Link"
    optRefresh.Caption = "Refresh Link"

    cnAccess.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\nwind.mdb;"
    cat.ActiveConnection = cnAccess

    End Sub

    Private Sub Form_Unload(Cancel As Integer)

    'Clean up code
    Set cat = Nothing

    cnAccess.Close
    Set cnAccess = Nothing

    End Sub

    Private Sub cmdLinkTable_Click()

    Dim rs As New ADODB.Recordset

    tbl.Name = "lnkAuthors"
    Set tbl.ParentCatalog = cat

    tbl.Properties("Jet OLEDB:Link Provider String") = "ODBC;Driver=SQL Server;Server=<servername>;database=pubs;uid=sa;pwd=;"
    tbl.Properties("Jet OLEDB:Remote Table Name") = "Authors"
    tbl.Properties("Jet OLEDB:Create Link") = True

    cat.Tables.Append tbl

    If optNoRefresh.Value = False Then
    'Executing the following line will refresh the link and the table will be Updateable.
    tbl.Properties("Jet OLEDB:Link Provider String") = "ODBC;Driver=SQL Server;Server=<servername>;database=pubs;uid=sa;pwd=;"
    End If

    rs.CursorLocation = adUseClient
    rs.Open "Select * from lnkAuthors", cnAccess, adOpenStatic, adLockOptimistic

    rs!State = "WA"
    rs.Update 'The error occurs here if the link is not refreshed.

    MsgBox "Recordset updated"

    rs.Close
    Set rs = Nothing

    End Sub

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

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

Featured