OVH Cloud OVH Cloud

Détecter si un champ existe dans une table

1 réponse
Avatar
Alain MENARD
Re bonjour

Avant de supprimer un champ, je voudrais savoir s'il existe.

Merci par avance

--

Amicalement

Alain MENARD

Pour me joindre : amenard@vision-tech.fr

1 réponse

Avatar
François Picalausa
"Alain MENARD" a écrit dans le message de
news:3fb65092$0$27031$
Re bonjour

Avant de supprimer un champ, je voudrais savoir s'il existe.

Merci par avance



Bonjour/soir,

voicci deux solutions qui semblent fonctionner:
Option Explicit

Private Sub Form_Load()

Dim cat As ADOX.Catalog, myCol As ADOX.Column

Set cat = New ADOX.Catalog

' Connexion à la base de donnée
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=F:Documents and SettingsFrançois PicalausaMes documentsMy
websBrinksterarticles.mdb"

' Suppression du champ
On Error Resume Next
Set myCol = cat.Tables("news").Columns("Toto")
Select Case Err.Number
Case 0
MsgBox "Le champ Toto existe!"
Case 3265
MsgBox "Le champ Toto n'existe pas!"
Case Else
MsgBox "Une erreur innatendue s'est produite !"
End Select
Err.Clear
On Error GoTo 0

For Each myCol In cat.Tables("news").Columns
If myCol.Name = "PubDate" Then
MsgBox "Le champ PubDate existe!"
Exit For
End If
Next myCol

Set myCol = Nothing
Set cat = Nothing
End Sub

Toutefois, n'étant absolument pas expert dans le domaine, il est possible
que ces codes soient erronnés. J'espère que ça pourra aider en attendant
mieux ;-)

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com