Microsoft 365 Type de données

José77

XLDnaute Occasionnel
Bonjour,
Avec Excel je me connecte à plusieurs bases de données et je voudrais savoir s’il est possible, à l’aide d’un code vba, de connaître le type de données des différents champs de la table (numérotation auto, texte, date, numérique etc …)
Si quelqu’un a une idée ça m’intéresse.
Merci
José
 

dysorthographie

XLDnaute Accro
Bonjour,
Je suis sur mon téléphone portable,je regarde dès que je peux, en attendant interesses toi à
adodb.connection.OpenSchema

Dès que je peux je modifie le code que je t'avais donné en 2020

Dans la fonction qui suit je retourne le nom des champs mais pas leur type !

Il existe quand même dans le recordset que retourne openshema de la fonction même si dedans je ne le gère pas !
Code:
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

 
Dernière édition:

dysorthographie

XLDnaute Accro
Voila c'est fait!
VB:
Option Explicit
Type TypeFeild
    Name As String
    Value As Integer
End Type
Type Champ
    AutoNumber As Boolean
    DEFAULT As String
    Description As String
    Name As String
    Position As Integer
    Size As String
    Type As TypeFeild
End Type
Sub Test()
Dim Cn As Object, Tchamps() As Champ 'Liste des champs et leurs valeur!
Set Cn = CreateObject("Adodb.Connection") 'Access
With Cn
    .Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Myrep\MyBDD.accdb;"
    Tchamps() = ListeChampTable(Cn, "MyTable")
    Range("A1").CopyFromRecordset .Execute("[MyTable]")
    .Close
End With
End Sub
Function ListeChampTable(Connexion As Object, table As String) As Champ()
Dim t() As Champ, i As Integer, G As Object, Tp As Collection
Set Tp = ConsType
With Connexion
    With .OpenSchema(4, Array(Empty, Empty, table))
        While Not .EOF
            ReDim Preserve t(i)
            t(i).Name = !COLUMN_NAME
            t(i).Position = !ORDINAL_POSITION
            t(i).Type.Name = Tp("k" & !DATA_TYPE)
            t(i).Type.Value = !DATA_TYPE
            t(i).Size = IIf("" & !CHARACTER_MAXIMUM_LENGTH = "", "NULL", !CHARACTER_MAXIMUM_LENGTH)
            t(i).DEFAULT = IIf("" & !COLUMN_DEFAULT = "", "NULL", !COLUMN_DEFAULT)
            t(i).Description = IIf("" & !Description = "", "NULL", !Description)
            Debug.Print !COLUMN_FLAGS
            t(i).AutoNumber = !COLUMN_FLAGS.Value = 90 And t(i).Type.Value = 3
            i = i + 1
            .MoveNext
        Wend
        ListeChampTable = t
    End With
End With
End Function
Function ConsType() As Collection
Set ConsType = New Collection
ConsType.Add "adSmallInt", "k" & 2
ConsType.Add "adInteger", "k" & 3
ConsType.Add "adSingle", "k" & 4
ConsType.Add "adDouble", "k" & 5
ConsType.Add "adCurrency", "k" & 6
ConsType.Add "adDate", "k" & 7
ConsType.Add "adIDispatch", "k" & 9
ConsType.Add "adBoolean", "k" & 11
ConsType.Add "adVariant", "k" & 12
ConsType.Add "adDecimal", "k" & 14
ConsType.Add "adUnsignedTinyInt", "k" & 17
ConsType.Add "dBigInt", "k" & 20
ConsType.Add "adGUID", "k" & 72
ConsType.Add "adBinary", "k" & 128
ConsType.Add "adChar", "k" & 129
ConsType.Add "adWChar", "k" & 130
ConsType.Add "adNumeric", "k" & 131
ConsType.Add "adDBTimeStamp", "k" & 135
ConsType.Add "adVarChar", "k" & 200
ConsType.Add "adLongVarChar", "k" & 201
ConsType.Add "adVarWChar", "k" & 202
ConsType.Add "adLongVarWChar", "k" & 203
ConsType.Add "adVarBinary", "k" & 204
ConsType.Add "adLongVarBinary", "k" & 205
End Function
 

Discussions similaires

Statistiques des forums

Discussions
312 215
Messages
2 086 328
Membres
103 180
dernier inscrit
Vcr