PC Review


Reply
Thread Tools Rate Thread

Delete a constraint from code?

 
 
jacobh
Guest
Posts: n/a
 
      24th Jan 2006
Hi.

I've created a relation (referential integrity) between to tables manually
in Access.

Now I need to delete the relation/constraint (because I need to delete a
field), but I don't know the name of the constraint.

I need to delete the field "MyField" - by executing SQL queries like

ALTER TABEL MyTable DROP CONSTRAINT MyXXX
ALTER TABEL MyTable DROP MyField

How can I find in Access, where I can see the name of MyXXX? Or is there a
way through the ADO interface to learn the names of relations associated
with a certain field?

I created the field and the relation myself, but now copies of the database
is in use many places, so I need a way to do this by code.

Thanks for any help!

Jacob




 
Reply With Quote
 
 
 
 
Brendan Reynolds
Guest
Posts: n/a
 
      24th Jan 2006

The following code requires a reference to the ADOX (Microsoft ADO Ext. 2.x
for DDL and Security) object library. Alternatively, if your app already has
a reference to the DAO object library, you may find it easier to use DAO,
there's an example of that below too. Alternatively again, you could try
adapting the code at the following KB article which uses the ADODB
OpenSchema method. Replace 'adSchemaIndexes' in the sample code with
'adSchemaForeignKeys'.

http://support.microsoft.com/default...b;en-us;185979

Public Sub ListAllRelations()

Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim ky As ADOX.Key
Dim col As ADOX.Column

Set cat = New ADOX.Catalog
Set cat.ActiveConnection = CurrentProject.Connection
For Each tbl In cat.Tables
For Each ky In tbl.Keys
If ky.Type = adKeyForeign Then
Debug.Print "Table: " & tbl.Name
Debug.Print "Key: " & ky.Name
Debug.Print "Related Table: " & ky.RelatedTable
For Each col In ky.Columns
Debug.Print "Column: " & col.Name
Debug.Print "Related Column: " & col.RelatedColumn
Next col
Debug.Print
End If
Next ky
Next tbl

End Sub

Public Sub ListRelationByColumn(ByVal ColumnName As String)

Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim ky As ADOX.Key
Dim col As ADOX.Column
Dim boolFound As Boolean

Set cat = New ADOX.Catalog
Set cat.ActiveConnection = CurrentProject.Connection
For Each tbl In cat.Tables
For Each ky In tbl.Keys
If ky.Type = adKeyForeign Then
boolFound = False
For Each col In ky.Columns
If col.Name = ColumnName Then
boolFound = True
Exit For
End If
Next col
If boolFound Then
Debug.Print "Table: " & tbl.Name
Debug.Print "Key: " & ky.Name
Debug.Print "Related Table: " & ky.RelatedTable
For Each col In ky.Columns
Debug.Print "Column: " & col.Name
Debug.Print "Related Column: " & col.RelatedColumn
Next col
Debug.Print
End If
End If
Next ky
Next tbl

End Sub

Public Sub ListRelationByColumn2(ByVal ColumnName As String)

Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field
Dim boolFound As Boolean

Set db = CurrentDb
For Each rel In db.Relations
boolFound = False
For Each fld In rel.Fields
If fld.Name = ColumnName Then
boolFound = True
Exit For
End If
Next fld
If boolFound Then
Debug.Print "Table: " & rel.Table
Debug.Print "Relation: " & rel.Name
Debug.Print "Related Table: " & rel.ForeignTable
For Each fld In rel.Fields
Debug.Print "Column: " & fld.Name
Debug.Print "Related Column: " & fld.ForeignName
Next fld
Debug.Print
End If
Next rel

End Sub

--
Brendan Reynolds
Access MVP

"jacobh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi.
>
> I've created a relation (referential integrity) between to tables manually
> in Access.
>
> Now I need to delete the relation/constraint (because I need to delete a
> field), but I don't know the name of the constraint.
>
> I need to delete the field "MyField" - by executing SQL queries like
>
> ALTER TABEL MyTable DROP CONSTRAINT MyXXX
> ALTER TABEL MyTable DROP MyField
>
> How can I find in Access, where I can see the name of MyXXX? Or is there a
> way through the ADO interface to learn the names of relations associated
> with a certain field?
>
> I created the field and the relation myself, but now copies of the
> database is in use many places, so I need a way to do this by code.
>
> Thanks for any help!
>
> Jacob
>
>
>
>
>



 
Reply With Quote
 
Jacob Havkrog
Guest
Posts: n/a
 
      25th Jan 2006
Thanks Brendan.

That was very helpful.

If the latest version of ADO in installed ona machine, is ADOX then also
installed? Can I be sure it's there for me to use?

You don't happen to have some code that demonstrates how to add a contraint
using ADOX, re my latest posting.

Best regards
Jacob


"Brendan Reynolds" <(E-Mail Removed)> skrev i en
meddelelse news:(E-Mail Removed)...
>
> The following code requires a reference to the ADOX (Microsoft ADO Ext.
> 2.x for DDL and Security) object library. Alternatively, if your app
> already has a reference to the DAO object library, you may find it easier
> to use DAO, there's an example of that below too. Alternatively again, you
> could try adapting the code at the following KB article which uses the
> ADODB OpenSchema method. Replace 'adSchemaIndexes' in the sample code with
> 'adSchemaForeignKeys'.
>
> http://support.microsoft.com/default...b;en-us;185979
>
> Public Sub ListAllRelations()
>
> Dim cat As ADOX.Catalog
> Dim tbl As ADOX.Table
> Dim ky As ADOX.Key
> Dim col As ADOX.Column
>
> Set cat = New ADOX.Catalog
> Set cat.ActiveConnection = CurrentProject.Connection
> For Each tbl In cat.Tables
> For Each ky In tbl.Keys
> If ky.Type = adKeyForeign Then
> Debug.Print "Table: " & tbl.Name
> Debug.Print "Key: " & ky.Name
> Debug.Print "Related Table: " & ky.RelatedTable
> For Each col In ky.Columns
> Debug.Print "Column: " & col.Name
> Debug.Print "Related Column: " & col.RelatedColumn
> Next col
> Debug.Print
> End If
> Next ky
> Next tbl
>
> End Sub
>
> Public Sub ListRelationByColumn(ByVal ColumnName As String)
>
> Dim cat As ADOX.Catalog
> Dim tbl As ADOX.Table
> Dim ky As ADOX.Key
> Dim col As ADOX.Column
> Dim boolFound As Boolean
>
> Set cat = New ADOX.Catalog
> Set cat.ActiveConnection = CurrentProject.Connection
> For Each tbl In cat.Tables
> For Each ky In tbl.Keys
> If ky.Type = adKeyForeign Then
> boolFound = False
> For Each col In ky.Columns
> If col.Name = ColumnName Then
> boolFound = True
> Exit For
> End If
> Next col
> If boolFound Then
> Debug.Print "Table: " & tbl.Name
> Debug.Print "Key: " & ky.Name
> Debug.Print "Related Table: " & ky.RelatedTable
> For Each col In ky.Columns
> Debug.Print "Column: " & col.Name
> Debug.Print "Related Column: " & col.RelatedColumn
> Next col
> Debug.Print
> End If
> End If
> Next ky
> Next tbl
>
> End Sub
>
> Public Sub ListRelationByColumn2(ByVal ColumnName As String)
>
> Dim db As DAO.Database
> Dim rel As DAO.Relation
> Dim fld As DAO.Field
> Dim boolFound As Boolean
>
> Set db = CurrentDb
> For Each rel In db.Relations
> boolFound = False
> For Each fld In rel.Fields
> If fld.Name = ColumnName Then
> boolFound = True
> Exit For
> End If
> Next fld
> If boolFound Then
> Debug.Print "Table: " & rel.Table
> Debug.Print "Relation: " & rel.Name
> Debug.Print "Related Table: " & rel.ForeignTable
> For Each fld In rel.Fields
> Debug.Print "Column: " & fld.Name
> Debug.Print "Related Column: " & fld.ForeignName
> Next fld
> Debug.Print
> End If
> Next rel
>
> End Sub
>
> --
> Brendan Reynolds
> Access MVP
>
> "jacobh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi.
>>
>> I've created a relation (referential integrity) between to tables
>> manually in Access.
>>
>> Now I need to delete the relation/constraint (because I need to delete a
>> field), but I don't know the name of the constraint.
>>
>> I need to delete the field "MyField" - by executing SQL queries like
>>
>> ALTER TABEL MyTable DROP CONSTRAINT MyXXX
>> ALTER TABEL MyTable DROP MyField
>>
>> How can I find in Access, where I can see the name of MyXXX? Or is there
>> a way through the ADO interface to learn the names of relations
>> associated with a certain field?
>>
>> I created the field and the relation myself, but now copies of the
>> database is in use many places, so I need a way to do this by code.
>>
>> Thanks for any help!
>>
>> Jacob
>>
>>
>>
>>
>>

>
>



 
Reply With Quote
 
Brendan Reynolds
Guest
Posts: n/a
 
      25th Jan 2006

The only supported way to install any of the ADO components is to install
the MDAC redistributable, so in theory if one component is installed they
should all be installed. But I mostly use DAO, I don't think I've got any
widely-deployed apps using ADOX, so I don't have personal experience in that
area.

I don't think I've seen the latest post to which you refer. I'll try to keep
an eye out for it.

--
Brendan Reynolds
Access MVP

"Jacob Havkrog" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks Brendan.
>
> That was very helpful.
>
> If the latest version of ADO in installed ona machine, is ADOX then also
> installed? Can I be sure it's there for me to use?
>
> You don't happen to have some code that demonstrates how to add a
> contraint using ADOX, re my latest posting.
>
> Best regards
> Jacob
>
>
> "Brendan Reynolds" <(E-Mail Removed)> skrev i en
> meddelelse news:(E-Mail Removed)...
>>
>> The following code requires a reference to the ADOX (Microsoft ADO Ext.
>> 2.x for DDL and Security) object library. Alternatively, if your app
>> already has a reference to the DAO object library, you may find it easier
>> to use DAO, there's an example of that below too. Alternatively again,
>> you could try adapting the code at the following KB article which uses
>> the ADODB OpenSchema method. Replace 'adSchemaIndexes' in the sample code
>> with 'adSchemaForeignKeys'.
>>
>> http://support.microsoft.com/default...b;en-us;185979
>>
>> Public Sub ListAllRelations()
>>
>> Dim cat As ADOX.Catalog
>> Dim tbl As ADOX.Table
>> Dim ky As ADOX.Key
>> Dim col As ADOX.Column
>>
>> Set cat = New ADOX.Catalog
>> Set cat.ActiveConnection = CurrentProject.Connection
>> For Each tbl In cat.Tables
>> For Each ky In tbl.Keys
>> If ky.Type = adKeyForeign Then
>> Debug.Print "Table: " & tbl.Name
>> Debug.Print "Key: " & ky.Name
>> Debug.Print "Related Table: " & ky.RelatedTable
>> For Each col In ky.Columns
>> Debug.Print "Column: " & col.Name
>> Debug.Print "Related Column: " & col.RelatedColumn
>> Next col
>> Debug.Print
>> End If
>> Next ky
>> Next tbl
>>
>> End Sub
>>
>> Public Sub ListRelationByColumn(ByVal ColumnName As String)
>>
>> Dim cat As ADOX.Catalog
>> Dim tbl As ADOX.Table
>> Dim ky As ADOX.Key
>> Dim col As ADOX.Column
>> Dim boolFound As Boolean
>>
>> Set cat = New ADOX.Catalog
>> Set cat.ActiveConnection = CurrentProject.Connection
>> For Each tbl In cat.Tables
>> For Each ky In tbl.Keys
>> If ky.Type = adKeyForeign Then
>> boolFound = False
>> For Each col In ky.Columns
>> If col.Name = ColumnName Then
>> boolFound = True
>> Exit For
>> End If
>> Next col
>> If boolFound Then
>> Debug.Print "Table: " & tbl.Name
>> Debug.Print "Key: " & ky.Name
>> Debug.Print "Related Table: " & ky.RelatedTable
>> For Each col In ky.Columns
>> Debug.Print "Column: " & col.Name
>> Debug.Print "Related Column: " & col.RelatedColumn
>> Next col
>> Debug.Print
>> End If
>> End If
>> Next ky
>> Next tbl
>>
>> End Sub
>>
>> Public Sub ListRelationByColumn2(ByVal ColumnName As String)
>>
>> Dim db As DAO.Database
>> Dim rel As DAO.Relation
>> Dim fld As DAO.Field
>> Dim boolFound As Boolean
>>
>> Set db = CurrentDb
>> For Each rel In db.Relations
>> boolFound = False
>> For Each fld In rel.Fields
>> If fld.Name = ColumnName Then
>> boolFound = True
>> Exit For
>> End If
>> Next fld
>> If boolFound Then
>> Debug.Print "Table: " & rel.Table
>> Debug.Print "Relation: " & rel.Name
>> Debug.Print "Related Table: " & rel.ForeignTable
>> For Each fld In rel.Fields
>> Debug.Print "Column: " & fld.Name
>> Debug.Print "Related Column: " & fld.ForeignName
>> Next fld
>> Debug.Print
>> End If
>> Next rel
>>
>> End Sub
>>
>> --
>> Brendan Reynolds
>> Access MVP
>>
>> "jacobh" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Hi.
>>>
>>> I've created a relation (referential integrity) between to tables
>>> manually in Access.
>>>
>>> Now I need to delete the relation/constraint (because I need to delete
>>> a
>>> field), but I don't know the name of the constraint.
>>>
>>> I need to delete the field "MyField" - by executing SQL queries like
>>>
>>> ALTER TABEL MyTable DROP CONSTRAINT MyXXX
>>> ALTER TABEL MyTable DROP MyField
>>>
>>> How can I find in Access, where I can see the name of MyXXX? Or is there
>>> a way through the ADO interface to learn the names of relations
>>> associated with a certain field?
>>>
>>> I created the field and the relation myself, but now copies of the
>>> database is in use many places, so I need a way to do this by code.
>>>
>>> Thanks for any help!
>>>
>>> Jacob
>>>
>>>
>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constraint matches constraint named Constraint1 already in collect =?Utf-8?B?VGFz?= Microsoft C# .NET 3 26th Apr 2010 04:22 PM
Silent acceptance of constraint-violating code Norman Diamond Microsoft VC .NET 5 4th Aug 2006 05:07 AM
Delete data in a linked Excel sheet using Access code or seql delete Rocky Microsoft Access External Data 9 26th Jun 2005 12:42 AM
code to delete a user/ code to move users into and out of groups =?Utf-8?B?dHc=?= Microsoft Access Security 11 30th May 2005 06:59 AM
Find out which row/constraint triggers the constraint exception =?Utf-8?B?UmFtb24gZGUgS2xlaW4=?= Microsoft ADO .NET 1 2nd Jul 2004 10:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:03 PM.