Results 1 to 5 of 5

Thread: Help needed in retriveing db reletionships using vba ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow Help needed in retriveing db reletionships using vba ?

    Hi experts. I need a help in retriveing primary key,alternatekey,foreign key and lenght of each field
    using vba and displaying it in massage box. I be happy if some one show me how i can retrive these information.
    Thanks

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

    Re: Help needed in retriveing db reletionships using vba ?

    What db are you using? Access? There are no real foreign keys in access. I have some CodeBank code on creating
    a relationship using DAO that should help you. Search by my username in vb6 code bank.
    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Talking Re: Help needed in retriveing db reletionships using vba ?

    Quote Originally Posted by RobDog888
    What db are you using? Access? There are no real foreign keys in access. I have some CodeBank code on creating
    a relationship using DAO that should help you. Search by my username in vb6 code bank.
    Many thanks for u reply. I am using access 2000 and want to use vba to retrive database reletionship. I tried to run your code but i got this error :

    run-tim eror'424':

    object reuired

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

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


    ----------------------------------------------
    i be happy if u help me fix this error. Thanks

    Here is the code :

    VB Code:
    1. Option Compare Database
    2.  
    3.  
    4. 'Add reference to MS Access xx.0 Object Library
    5. 'Add reference to DAO 3.x Object Library
    6. Private oApp As Access.Application
    7.  
    8.  
    9.  
    10. Private Sub Command0_Click()
    11.      MsgBox "ok"
    12.     Dim oRel As DAO.Relation
    13.     Dim oDB As DAO.Database
    14.     Dim oTable1 As DAO.TableDef
    15.     Dim oTable2 As DAO.TableDef
    16.     Dim oIndex As DAO.Index
    17.    
    18.     'Create new blank access database
    19.     Set oApp = New Access.Application
    20.     oApp.NewCurrentDatabase App.Path & "\MyDB2.mdb"
    21.     Set oDB = oApp.CurrentDb
    22.     oApp.Visible = True
    23.    
    24.     'Create first table (Table1)
    25.     Set oTable1 = oDB.CreateTableDef("Table1")
    26.     With oTable1
    27.         .Fields.Append .CreateField("Field1", dbInteger)
    28.         .Fields.Append .CreateField("Field2", dbText)
    29.         .Fields.Append .CreateField("Field3", dbText)
    30.         .Fields.Append .CreateField("Field4", dbText)
    31.     End With
    32.     oDB.TableDefs.Append oTable1
    33.    
    34.     'Create an index on Table1
    35.     Set oIndex = oTable1.CreateIndex
    36.     With oIndex
    37.         .Name = "Field1Index"
    38.         .Fields.Append .CreateField("Field1")
    39.         .Primary = True
    40.     End With
    41.     oTable1.Indexes.Append oIndex
    42.    
    43.     'Create second table (Table2)
    44.     Set oTable2 = oDB.CreateTableDef("Table2")
    45.     With oTable2
    46.         .Fields.Append .CreateField("Field1", dbInteger)
    47.         .Fields.Append .CreateField("Field2", dbText)
    48.         .Fields.Append .CreateField("Field3", dbText)
    49.         .Fields.Append .CreateField("Field4", dbText)
    50.     End With
    51.     oDB.TableDefs.Append oTable2
    52.    
    53.     'Create an index on Table2
    54.     Set oIndex = Nothing
    55.     Set oIndex = oTable2.CreateIndex
    56.     With oIndex
    57.         .Name = "Field1Index"
    58.         .Fields.Append .CreateField("Field1")
    59.         .Primary = True
    60.     End With
    61.     oTable2.Indexes.Append oIndex
    62.    
    63.     'Create relationship between table1 and table2
    64.     Set oRel = oApp.CurrentDb.CreateRelation("MyRelationship", "Table1", "Table2", dbRelationLeft Or dbRelationUpdateCascade Or dbRelationDeleteCascade)
    65.     oRel.Fields.Append oRel.CreateField("Field1")
    66.     oRel.Fields("Field1").ForeignName = "Field1"
    67.     oApp.CurrentDb.Relations.Append oRel
    68.  
    69.     MsgBox "Done!"
    70.     Unload Me
    71.    
    72. End Sub
    Last edited by RobDog888; May 2nd, 2005 at 10:34 AM. Reason: Added vbcode tags

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

    Re: Help needed in retriveing db reletionships using vba ?

    Thats because youur doing it "in" Access. My code example was from VB6 but we can make it work.
    Change oApp to Application and remove these lines...

    VB Code:
    1. Set oApp = New Access.Application
    2. Private oApp As Access.Application
    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

  5. #5
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Help needed in retriveing db reletionships using vba ?

    another post about the same thing by Tony007:

    http://www.vbforums.com/showthread.php?t=336554

    Tony007, perhaps you should stick to a single post about a single topic, makes it easier for other members to help you.
    if you fail to plan, you plan to fail

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