Microsoft 365 Lister les noms des champs d’une table

José77

XLDnaute Occasionnel
Bonjour,
Pour communiquer entre Excel et une base accessible j’utilise ADO.
Je voudrais récupérer les noms des champs d’une table dans un combobox, mais là je n’y arrive pas. Si quelqu’un pouvait m’aider ce serait super.
Merci par avance
José
 

dysorthographie

XLDnaute Accro
Bonjour,
ici je fais une connexion Excel ,mais ça fonctionne en ADO sur n'importe quelle base de données!
{ListeChampTable}

VB:
Sub test()
Dim Connexion As Object: Set Connexion = CreateObject("Adodb.connection")
With Connexion
    .Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES;""" 'à la place de ThisWorkbook.FullName tu écris le chemin du fichier.xlsx dan Access!
    t = ListeChampTable(Connexion, "Feuil1$")
    Debug.Print ChampExist(Connexion, "Feuil1$", "A")
    .Close
End With
End Sub
Function ListeChampTable(Connexion As Object, table As String)
Dim t() As String, i As Integer
With Connexion
    With .OpenSchema(4, Array(Empty, Empty, table))
        While Not .EOF
            ReDim Preserve t(i)
            t(i) = !COLUMN_NAME
            i = i + 1
            .MoveNext
        Wend
        ListeChampTable = t
    End With
End With
End Function
Public Function ChampExist(Connexion As Object, table As String, Champ As String) As Boolean
With Connexion
    With .OpenSchema(4, Array(Empty, Empty, table))
       If Not .EOF Then
           .Filter = "COLUMN_NAME ='" & Champ & "'"
           ChampExist = Not .EOF
       End If
    End With
End With
End Function
 

José77

XLDnaute Occasionnel
Bonsoir
Merci pour votre réponse mais malheureusement je n'arrive pas à la mettre en oeuvre.
Peut-être aurai je dû expliquer un peu mieux
Ma base : clients.accdb
Les tables : TClient et Tvoiture
j'aurais aimé récupérer les noms des champs de TClient (à savoir IdClient , Nom et prenom).

cordialement
José
 

José77

XLDnaute Occasionnel
'@@@@@@@@@@@@@@@
Public Function connexion() As Boolean
'@@@@@@@@@@@@@@@

'ouverture de la base Clients.accdb
strfolder = ThisWorkbook.Path & "\"
Chemin = strfolder & "clients.accdb"

Set adoconnection = New ADODB.Connection
ConnectString = "Provider=Microsoft.ace.oledb.12.0;Data source=" & Chemin & ";persist security info = false"
adoconnection.Open ConnectString
Set adorecordset = New ADODB.Recordset

End Function


voila le code que j'utilise pour me connecter à la base
 

dysorthographie

XLDnaute Accro
Code:
Public ADOConnection As ADODB.Connection
Public ListeChamps() As String 'Représente le tableau des nom de champs
'..................................................................................................................................
Sub test()
If connexion Then
    ListeChamps = ListeChampTable(ADOConnection, "TClient")
    ComboBox.List = ListeChampTable(ADOConnection, "TClient")
End If
End Sub
'..................................................................................................................................

'@@@@@@@@@@@@@@@
Public Function connexion() As Boolean
'@@@@@@@@@@@@@@@

'ouverture de la base Clients.accdb
strfolder = ThisWorkbook.Path & "\"
Chemin = strfolder & "clients.accdb"

Set ADOConnection = New ADODB.Connection
ConnectString = "Provider=Microsoft.ace.oledb.12.0;Data source=" & Chemin & ";persist security info = false"
ADOConnection.Open ConnectString
Set adorecordset = New ADODB.Recordset

End Function
'***********************************************************
'Permet de récupérer les noms de tous les champs d'une table!
Function ListeChampTable(connexion As Object, table As String)
Dim t() As String, i As Integer
With connexion
    With .OpenSchema(4, Array(Empty, Empty, table)) 'ouvre le RcordSet des champs de la table!
        While Not .EOF
            ReDim Preserve t(i)
            t(i) = !COLUMN_NAME
            i = i + 1
            .MoveNext
        Wend
        ListeChampTable = t
    End With
End With
End Function
'*************************************************************
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 042
Messages
2 084 829
Membres
102 685
dernier inscrit
med_remi021