Results 1 to 8 of 8

Thread: VB6 - Create Relationship in Access Using DAO/Access Object Model

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    VB6 - Create Relationship in Access Using DAO/Access Object Model

    I didnt see anything on creating an Access Relationship when
    I searched, so here is my code to create a relationship.

    It also creates a new blank Access database, Add 2 new tables,
    creates new fields, set primary keys, and then its creates a
    relationship between the two tables based on the primary keys.

    Works on Access versions 2000 +.
    I don't have Access 97 anymore so un-tested on 97.

    VB Code:
    1. Option Explicit
    2. 'Add reference to MS Access xx.0 Object Library
    3. 'Add reference to DAO 3.x Object Library
    4. Private oApp As Access.Application
    5.  
    6. Private Sub Form_Load()
    7.    
    8.     Dim oRel As DAO.Relation
    9.     Dim oDB As DAO.Database
    10.     Dim oTable1 As DAO.TableDef
    11.     Dim oTable2 As DAO.TableDef
    12.     Dim oIndex As DAO.Index
    13.    
    14.     'Create new blank access database
    15.     Set oApp = New Access.Application
    16.     oApp.NewCurrentDatabase App.Path & "\MyDB2.mdb"
    17.     Set oDB = oApp.CurrentDb
    18.     oApp.Visible = True
    19.    
    20.     'Create first table (Table1)
    21.     Set oTable1 = oDB.CreateTableDef("Table1")
    22.     With oTable1
    23.         .Fields.Append .CreateField("Field1", dbInteger)
    24.         .Fields.Append .CreateField("Field2", dbText)
    25.         .Fields.Append .CreateField("Field3", dbText)
    26.         .Fields.Append .CreateField("Field4", dbText)
    27.     End With
    28.     oDB.TableDefs.Append oTable1
    29.    
    30.     'Create an index on Table1
    31.     Set oIndex = oTable1.CreateIndex
    32.     With oIndex
    33.         .Name = "Field1Index"
    34.         .Fields.Append .CreateField("Field1")
    35.         .Primary = True
    36.     End With
    37.     oTable1.Indexes.Append oIndex
    38.    
    39.     'Create second table (Table2)
    40.     Set oTable2 = oDB.CreateTableDef("Table2")
    41.     With oTable2
    42.         .Fields.Append .CreateField("Field1", dbInteger)
    43.         .Fields.Append .CreateField("Field2", dbText)
    44.         .Fields.Append .CreateField("Field3", dbText)
    45.         .Fields.Append .CreateField("Field4", dbText)
    46.     End With
    47.     oDB.TableDefs.Append oTable2
    48.    
    49.     'Create an index on Table2
    50.     Set oIndex = Nothing
    51.     Set oIndex = oTable2.CreateIndex
    52.     With oIndex
    53.         .Name = "Field1Index"
    54.         .Fields.Append .CreateField("Field1")
    55.         .Primary = True
    56.     End With
    57.     oTable2.Indexes.Append oIndex
    58.    
    59.     'Create relationship between table1 and table2
    60.     Set oRel = oApp.CurrentDb.CreateRelation("MyRelationship", "Table1", "Table2", dbRelationLeft Or dbRelationUpdateCascade Or dbRelationDeleteCascade)
    61.     oRel.Fields.Append oRel.CreateField("Field1")
    62.     oRel.Fields("Field1").ForeignName = "Field1"
    63.     oApp.CurrentDb.Relations.Append oRel
    64.  
    65.     MsgBox "Done!"
    66.     Unload Me
    67.    
    68. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    Junior Member
    Join Date
    Mar 2005
    Posts
    19

    Post Re: VB6 - Create Relationship in Access Using DAO/Access Object Model

    hello rob,


    hello ro b it was nice joining u. can u help me that how can i get refrence to ms access xx.o lib I am using vb 6.0 and I an reading ur code and also side by side creating for my rememberance with the help of ur code. i wanted to know the dll required for the ms access lib . i will be obliged if u helpp me than u very much in advance for ur support.



    yours new mate
    hemal

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 - Create Relationship in Access Using DAO/Access Object Model

    You can add the references (if you have Access installed on your system) by going to
    Project > References... > and selecting "MS Access xx.0 Object Library" and "MS DAO 3.x Object Library"

    They should show in the references list automatically. If you need to browse for them then the files are:
    ?:\Program Files\Microsoft Office\Officexx\MSACC.OLB
    ?:\Program Files\Common Files\Microsoft Shared\DAO\daoxx0.dll
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow Re: VB6 - Create Relationship in Access Using DAO/Access Object Model

    Many thanks for u code. I tried to run it in access 2000 but when i click on the button i get this error:

    run-tim eror'424':

    object reuired

    -------------------------------------------------------
    And when i debug it it points to this line :

    oApp.NewCurrentDatabase App.Path & "\MyDB2.mdb"


    I be happy if u help me how to fix this error. I have a button on my form that i click on it it should run this code.Furthermore, should i make any table to store the reletionships ? Thank u and looking forward to your reply.
    --------------------------------------------------------

    This is the code :

    Option Compare Database


    'Add reference to MS Access xx.0 Object Library
    'Add reference to DAO 3.x Object Library
    Private oApp As Access.Application



    Private Sub Command0_Click()
    Dim oRel As DAO.Relation
    Dim oDB As DAO.Database
    Dim oTable1 As DAO.TableDef
    Dim oTable2 As DAO.TableDef
    Dim oIndex As DAO.Index

    'Create new blank access database
    Set oApp = New Access.Application
    oApp.NewCurrentDatabase App.Path & "\MyDB2.mdb"
    Set oDB = oApp.CurrentDb
    oApp.Visible = True

    'Create first table (Table1)
    Set oTable1 = oDB.CreateTableDef("Table1")
    With oTable1
    .Fields.Append .CreateField("Field1", dbInteger)
    .Fields.Append .CreateField("Field2", dbText)
    .Fields.Append .CreateField("Field3", dbText)
    .Fields.Append .CreateField("Field4", dbText)
    End With
    oDB.TableDefs.Append oTable1

    'Create an index on Table1
    Set oIndex = oTable1.CreateIndex
    With oIndex
    .Name = "Field1Index"
    .Fields.Append .CreateField("Field1")
    .Primary = True
    End With
    oTable1.Indexes.Append oIndex

    'Create second table (Table2)
    Set oTable2 = oDB.CreateTableDef("Table2")
    With oTable2
    .Fields.Append .CreateField("Field1", dbInteger)
    .Fields.Append .CreateField("Field2", dbText)
    .Fields.Append .CreateField("Field3", dbText)
    .Fields.Append .CreateField("Field4", dbText)
    End With
    oDB.TableDefs.Append oTable2

    'Create an index on Table2
    Set oIndex = Nothing
    Set oIndex = oTable2.CreateIndex
    With oIndex
    .Name = "Field1Index"
    .Fields.Append .CreateField("Field1")
    .Primary = True
    End With
    oTable2.Indexes.Append oIndex

    'Create relationship between table1 and table2
    Set oRel = oApp.CurrentDb.CreateRelation("MyRelationship", "Table1", "Table2", dbRelationLeft Or dbRelationUpdateCascade Or dbRelationDeleteCascade)
    oRel.Fields.Append oRel.CreateField("Field1")
    oRel.Fields("Field1").ForeignName = "Field1"
    oApp.CurrentDb.Relations.Append oRel

    MsgBox "Done!"
    Unload Me

    End Sub

  5. #5

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 - Create Relationship in Access Using DAO/Access Object Model

    I replied in your thread in the VBA forum
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    New Member
    Join Date
    Oct 2007
    Posts
    2

    Re: VB6 - Create Relationship in Access Using DAO/Access Object Model

    Hi There

    I know this is quite an old thread but it does pretty much exactly what I want to do.
    I would like to create new tables but to load an existing database and create the relationship to an existing table.
    The way I have tried this is to "OpenDatabase" but it doesn't seem to like that.
    It would be great if you had any suggestions.

    thanks

  7. #7

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 - Create Relationship in Access Using DAO/Access Object Model

    There is a CreateDatabase method too. Its a child method of the DBEngine object.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    New Member
    Join Date
    Oct 2007
    Posts
    2

    Re: VB6 - Create Relationship in Access Using DAO/Access Object Model

    Thanks for that, I will give it a try

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