|
-
May 1st, 2005, 03:09 PM
#1
Thread Starter
Frenzied Member
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
-
May 1st, 2005, 08:15 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 2nd, 2005, 03:53 AM
#3
Thread Starter
Frenzied Member
Re: Help needed in retriveing db reletionships using vba ?
 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:
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()
MsgBox "ok"
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
Last edited by RobDog888; May 2nd, 2005 at 10:34 AM.
Reason: Added vbcode tags
-
May 2nd, 2005, 10:33 AM
#4
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:
Set oApp = New Access.Application
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 2nd, 2005, 12:13 PM
#5
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|