Results 1 to 2 of 2

Thread: Questions & Answers to Relationships with ADO / ADOX and MDB

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2001
    Location
    Bangkok
    Posts
    969

    Questions & Answers to Relationships with ADO / ADOX and MDB

    Hi everybody,

    after a lot of questions to everybody without any satisfieing answer about ADO and Relationships, i contacted a friend from Microsoft who gave me this answer:

    If you want to create a Relationship in ADO, it is the best way to have an Index on your table and the index is active. Then just use a SQL-command: SELECT .... WHERE

    I asked him, what happens then with ADOX, and he answerred that this was only included for Marketingreasons. ADOX Relations will be anyway translated to an SQL-command, so it is better to do it direct. He said that ADOX is infact only interesting to read informations about the current INDEXes and the structure of the table out. They don't use ADOX for anythign else.

    If I am wrong or he is wrong, please reply. I want to clear this Question forever.

    But I am still curious about ADOX.
    My questions are still the same:

    1) I guess that a Key is a Index, so why in all samples (ADOX) they create always a new key/index when they are anyway already created before in a MDB?

    2) In all the samples they show always only that they create a new field, with the ADOX.Table If the fields are already there why? I used before other Database-Compilers and when the fields was already there, for example CustNumber in two tables, and the to relate Table had the index correct then it was easy, just say: Set Relation for CustNumber in TableOrders

    I hope to get many responses for this to clear this problem forever. Please don't just paste MSDN-code here. This Codes don't bring the solution

    Thanks Frank

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    St. Albans, Herts, UK
    Posts
    259
    Here is the DAO code to copy relations from an existing database
    VB Code:
    1. Dim dbSrc As Database
    2.     Dim dbNew As Database
    3.     Dim rlSrc As Relation
    4.     Dim rlNew As Relation
    5.     Dim fdSrc As Field
    6.  
    7.     Set dbSrc = OpenDatabase("PathAndFileSource)
    8.     Set dbNew = OpenDatabase("PathAndFileDestination)
    9.  
    10.     For Each rlSrc In dbSrc.Relations
    11.         Set rlNew = dbNew.CreateRelation(rlSrc.Name, rlSrc.Table, rlSrc.ForeignTable, rlSrc.Attributes)
    12.         For Each fdSrc In rlSrc.Fields
    13.             rlNew.Fields.Append rlNew.CreateField(fdSrc.Name)
    14.             rlNew.Fields(fdSrc.Name).ForeignName = rlSrc.Fields(fdSrc.Name).ForeignName
    15.         Next fdSrc
    16.         dbNew.Relations.Append rlNew
    17.     Next rlSrc
    Here is the equivalent with ADOX
    VB Code:
    1. Dim cnnSrc As New ADODB.Connection
    2.     Dim cnnNew As New ADODB.Connection    
    3.     Dim catSrc As New ADOX.Catalog
    4.     Dim catNew As New ADOX.Catalog
    5.     Dim tblSrc As New ADOX.Table
    6.     Dim tblNew As New ADOX.Table
    7.     Dim keySrc As New ADOX.Key
    8.     Dim keyNew As New ADOX.Key
    9.  
    10.     cnnSrc.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=PathAndFileSource"
    11.     Set catSrc.ActiveConnection = cnnSrc
    12.  
    13.     cnnNew.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=PathAndFileDestination"
    14.     Set catNew.ActiveConnection = cnnNew
    15.  
    16.     'copy relations (keys in ADO)
    17.     For Each tblSrc In catSrc.Tables
    18.         If InStr(1, tblSrc.Name, "MSys", vbTextCompare) = 0 And tblSrc.Type = "TABLE" Then
    19.             For Each keySrc In tblSrc.Keys
    20.                 If keySrc.RelatedTable <> "" Then
    21.                     keyNew.Name = keySrc.Name
    22.                     keyNew.Type = adKeyForeign
    23.                     keyNew.RelatedTable = keySrc.RelatedTable
    24.                     keyNew.Columns.Append keySrc.Columns(0).Name
    25.                     keyNew.Columns(keySrc.Columns(0).Name).RelatedColumn = keySrc.Columns(0).RelatedColumn
    26.                     tblNew.Keys.Append keyNew
    27.                     Set keyNew = Nothing
    28.                 End If
    29.             Next keySrc
    30.         End If
    31.     Next tblSrc
    As Microsoft themselves imply on their site:http://msdn.microsoft.com/library/de...cesstables.asp
    You must establish a reference to the Microsoft DAO 3.6 object library and use DAO code to programmatically set these remaining table properties: Description, Filter, OrderBy, LinkChildFields, LinkMasterFields, SubdatasheetExpanded, SubdatasheetName, and SubdatasheetHeight. For information about working with these properties, search Microsoft Access Help.
    You are best to use DAO for manipulating database structure!
    Last edited by gab2001uk; Dec 30th, 2001 at 05:01 AM.
    Graham, www.gab2001uk.com VBExplorer Forum Moderator VBExplorer
    www.gab2001uk.com For comparing and contrasting DAO with ADO
    Code for Creating, Copying, Compacting, Replicating, Synchronising Access 97/2000 databases plus showing Schemas and using .Seek

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