IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

VBA: Transfert de données d'Excel vers Access


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Homme Profil pro
    Manager
    Inscrit en
    Juin 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Manager

    Informations forums :
    Inscription : Juin 2012
    Messages : 4
    Par défaut VBA: Transfert de données d'Excel vers Access
    Bonjour le forum, le fil,

    J'ai lu cette discussion.

    Je souhaite remplir une table Access à partir d'une plage, le tout en Late Biding.

    Lorsque j'exécute la procédure, j'ai un message d'erreur sur la ligne "Add.New"
    "3251 Le jeu d'enregistrements suivant ne prend pas en charge la mise à jour. Il s'agit peut-être d'une limitation du fournisseur ou du type de verrou sélectionné."

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    Sub PasteData_LateBiding()
    Dim Cat As Object
    'Dim Cat As New ADOX.Catalog
    Dim Cnx As Object
    'Dim Cnx As New ADODB.Connection
    Dim rsT As Object
    'Dim rsT As New ADODB.Recordset
    Dim ndf As String
    Dim stSQL As String
    'Dim wbPath As String
    Dim Derlig As Integer
    Dim SDerCol As String
    Dim Let_Col As String
    Dim oTables As String
    Dim L As Long, C As Byte
    Dim TabPlage As Variant
    Dim DerCol As Byte
    Dim Tbl As Object
    Dim Requete As String
    Dim champ As Object
    'Set Cat = CreateObject("ADOX.Catalog")
    'Cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf
     
    On Error GoTo ErrHandl
     
    ndf = ThisWorkbook.Path & "\" & BDD
    'wbPath = ThisWorkbook.FullName
     
    Set Ws = ThisWorkbook.Sheets("Donnees")
    'Plage Collaborateurs
    With Ws
        DerCol = .Range("A1").End(xlToRight).Column
        Derlig = .Range("A65536").End(xlUp).Row
        SDerCol = .Range("A1").End(xlToRight).Address: Let_Col = Mid(SDerCol, 2, InStr(2, SDerCol, "$") - 2)
        TabPlage = .Range("A1:" & Let_Col & Derlig).Value
    End With
    oTables = "Collaborateurs"
     
    'Set Cat = CreateObject("ADOX.Catalog")
     
    Set Cnx = CreateObject("ADODB.Connection")
    With Cnx
        '.Provider = "Microsoft.JET.OLEDB.4.0"
        .Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf & ";Persist Security Info=False;" '"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & ndf
        '.Open ndf
    End With
     
    'With Cat
    '    .ActiveConnection = Cnx
    ''On Error Resume Next
    ''    .Tables.Delete COLLABORATEURS
    ''On Error GoTo 0
    'End With
     
    'Set Tbl = CreateObject("ADOX.Table")
    'Tbl.Name = oTables
    Set rsT = CreateObject("ADODB.Recordset")
    'Cnx.Execute stSQL
     
    'Cat.Tables.Append Tbl
     
    With rsT
        .ActiveConnection = Cnx
        '.Open oTables, adOpenKeyset, adLockOptimistic, adCmdTableDirect ', LockType:=adLockOptimistic
        '.Open "Collaborateurs", Cnx, adOpenKeyset ', LockType:=adLockOptimistic
        .Open "Collaborateurs", Cnx ', adOpenKeyset, adLockOptimistic, adCmdTable
    End With
     
    'supprime l'enregistrement si le champ 'Ville' est vide
    Requete = "DELETE * FROM " & "Collaborateurs" '& " WHERE [Ville]IS NULL "
    Cnx.Execute Requete
     
    'Boucle sur les lignes et colonnes de la plage pour remplir table
    'j = 0
    For L = 2 To UBound(TabPlage)
    '' Parcourir chaque champ de l'enregistrement courant
    '   For Each champ In rsT.Fields
    '        champ = Ws.Cells(L, 1 + j)
    '        j = j + 1
    '   Next champ
        With rsT
            .AddNew
            For C = 1 To DerCol
                .Fields(C - 1).Value = TabPlage(L, C)
            Next
        .Update
        End With
    Next L
    '*******************************************************************
     
    Set Cat = Nothing
    Set Cnx = Nothing
    Set rsT = Nothing
    Set Ws = Nothing
    Exit Sub
     
    ErrHandl:
     
    MsgBox Err.Number & vbCr & Err.Description, vbCritical
    rep = Err.Number & vbCr & Err.Description
    Ws.Range("J11") = rep
    'If Not rsT Is Nothing Then
    '    If rsT.State = adStateOpen Then rsT.Close
    'End If
    Set rsT = Nothing
     
    If Not Cnx Is Nothing Then
        If Cnx.State = adStateOpen Then Cnx.Close
    End If
    Set Cnx = Nothing
     
    Set Cat = Nothing
    'Set Cnx = Nothing
    'Set rsT = Nothing
    Set Ws = Nothing
     
     
    End Sub
    Merci d'avance pour votre aide !

    Bougla
    Fichiers attachés Fichiers attachés

  2. #2
    Invité
    Invité(e)
    Par défaut
    Bonjour,
    désolé mai je n'est pas vu le problème!

    Nom : Sans titre.png
Affichages : 220
Taille : 36,1 Ko
    Fichiers attachés Fichiers attachés

  3. #3
    Membre à l'essai
    Homme Profil pro
    Manager
    Inscrit en
    Juin 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Manager

    Informations forums :
    Inscription : Juin 2012
    Messages : 4
    Par défaut
    Re,

    J'ai coché les références et pourtant cela me met toujours de le message...

    Nom : Sans titre.png
Affichages : 225
Taille : 27,5 Ko

    EDIT: en cochant les références cela fonctionne correctement, mais par cette méthode l'utilisateur doit avoir Access d'installé, sommes nous en Late Biding ?

    Bougla

  4. #4
    Membre à l'essai
    Homme Profil pro
    Manager
    Inscrit en
    Juin 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Manager

    Informations forums :
    Inscription : Juin 2012
    Messages : 4
    Par défaut
    Re rudurupt,

    Cela fonctionne du tonnerre même si l'utilisateur n'a pas Access d'installé...

    Merci beaucoup !!!

  5. #5
    Invité
    Invité(e)
    Par défaut
    vola!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    Public Const BDD = "Reclamation_Incident.accdb"
     
     
    Sub PasteData()
    'Dim Cat As Object
    Dim Cat As Object
    Set Cat = CreateObject("ADOX.Catalog")
    'Dim Cnx As Object
    Dim Cnx As Object
    Set Cnx = CreateObject("ADODB.Connection")
    'Dim rsT As Object
    Dim rsT As Object
    Set rsT = CreateObject("ADODB.Recordset")
    Dim ndf As String
    Dim stSQL As String
    'Dim wbPath As String
    Dim Derlig As Integer
    Dim SDerCol As String
    Dim Let_Col As String
    Dim oTables As String
    Dim L As Long, C As Byte
    Dim TabPlage As Variant
    Dim DerCol As Byte
    Dim Tbl As Object
    Dim Requete As String
     
    'Set Cat = CreateObject("ADOX.Catalog")
    'Cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf
     
    On Error GoTo ErrHandl
     
    ndf = ThisWorkbook.Path & "\" & BDD
    'wbPath = ThisWorkbook.FullName
     
    Set Ws = ThisWorkbook.Sheets("Donnees")
    'Plage Collaborateurs
    With Ws
        DerCol = .Range("A1").End(xlToRight).Column
        Derlig = .Range("A65536").End(xlUp).Row
        SDerCol = .Range("A1").End(xlToRight).Address: Let_Col = Mid(SDerCol, 2, InStr(2, SDerCol, "$") - 2)
        TabPlage = .Range("A1:" & Let_Col & Derlig).Value
    End With
    oTables = "Collaborateurs"
    'oTables =replace(oTables,""","") 'Esc("Collaborateurs")
     
    '[Master Grid_Export$A1:M7]
    'stSQL = "INSERT INTO oTables * FROM" & "[" & Esc(Ws.Name) & "$A1:" & Esc(Let_Col) & Derlig & "]" & "IN '" & _
    Esc(wbPath) & "' 'Excel 12.0;HDR=No;'"
     
    'stSQL = "INSERT INTO Collaborateurs ([Id], [Site], [Equipe], [Nom], [User], [Statut], [Email], [Civilite])" & _
    "SELECT * FROM [Excel 12.0;HDR=No;DATABASE=" & wbPath & "]." & "[" & Ws.Name & "$]"
     
    'Set Cat = CreateObject("ADOX.Catalog")
     
    'Set Cnx = CreateObject("ADODB.Connection")
    With Cnx
        .Provider = "Microsoft.JET.OLEDB.4.0"
        '.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf '"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & ndf
        .Open ndf
    End With
     
    With Cat
        .ActiveConnection = Cnx
    'On Error Resume Next
    '    .Tables.Delete COLLABORATEURS
    'On Error GoTo 0
    End With
     
    Set Tbl = CreateObject("ADOX.Table")
    Tbl.Name = oTables
    'Set rsT = CreateObject("ADODB.Recordset")
    'Cnx.Execute stSQL
     
    'Cat.Tables.Append Tbl
     Const adLockOptimistic = 3
    With rsT
        .ActiveConnection = Cnx
        '.Open oTables, adOpenKeyset, adLockOptimistic, adCmdTableDirect ', LockType:=adLockOptimistic
        .Open oTables, LockType:=adLockOptimistic
    End With
     
    'supprime l'enregistrement si le champ 'Ville' est vide
    Requete = "DELETE * FROM " & oTables '& " WHERE [Ville]IS NULL "
    Cnx.Execute Requete
     
    'Boucle sur les lignes et colonnes de la plage pour remplir table
    For L = 2 To UBound(TabPlage)
        With rsT
            .AddNew
            For C = 1 To DerCol
                .Fields(C - 1).Value = TabPlage(L, C)
            Next
        .Update
        End With
    Next L
    '*******************************************************************
     
    Set Cat = Nothing
    Set Cnx = Nothing
    Set rsT = Nothing
    Set Ws = Nothing
    Exit Sub
     
    ErrHandl:
     
    MsgBox Err.Number & vbCr & Err.Description, vbCritical
    rep = Err.Number & vbCr & Err.Description
    Ws.Range("J11") = rep
    'If Not rsT Is Nothing Then
    '    If rsT.State = adStateOpen Then rsT.Close
    'End If
    Set rsT = Nothing
     
    If Not Cnx Is Nothing Then
        If Cnx.State = adStateOpen Then Cnx.Close
    End If
    Set Cnx = Nothing
     
    Set Cat = Nothing
    'Set Cnx = Nothing
    'Set rsT = Nothing
    Set Ws = Nothing
     
     
    End Sub
     
     
    Sub PasteData_LateBiding()
    Dim Cat As Object
    'Dim Cat As New ADOX.Catalog
    Dim Cnx As Object
    'Dim Cnx As New ADODB.Connection
    Dim rsT As Object
    'Dim rsT As New ADODB.Recordset
    Dim ndf As String
    Dim stSQL As String
    'Dim wbPath As String
    Dim Derlig As Integer
    Dim SDerCol As String
    Dim Let_Col As String
    Dim oTables As String
    Dim L As Long, C As Byte
    Dim TabPlage As Variant
    Dim DerCol As Byte
    Dim Tbl As Object
    Dim Requete As String
    Dim champ As Object
    'Set Cat = CreateObject("ADOX.Catalog")
    'Cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf
     
    On Error GoTo ErrHandl
     
    ndf = ThisWorkbook.Path & "\" & BDD
    'wbPath = ThisWorkbook.FullName
     
    Set Ws = ThisWorkbook.Sheets("Donnees")
    'Plage Collaborateurs
    With Ws
        DerCol = .Range("A1").End(xlToRight).Column
        Derlig = .Range("A65536").End(xlUp).Row
        SDerCol = .Range("A1").End(xlToRight).Address: Let_Col = Mid(SDerCol, 2, InStr(2, SDerCol, "$") - 2)
        TabPlage = .Range("A1:" & Let_Col & Derlig).Value
    End With
    oTables = "Collaborateurs"
     
    'Set Cat = CreateObject("ADOX.Catalog")
     
    Set Cnx = CreateObject("ADODB.Connection")
    With Cnx
        '.Provider = "Microsoft.JET.OLEDB.4.0"
        .Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf & ";Persist Security Info=False;" '"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & ndf
        '.Open ndf
    End With
     
    'With Cat
    '    .ActiveConnection = Cnx
    ''On Error Resume Next
    ''    .Tables.Delete COLLABORATEURS
    ''On Error GoTo 0
    'End With
     
    'Set Tbl = CreateObject("ADOX.Table")
    'Tbl.Name = oTables
    Set rsT = CreateObject("ADODB.Recordset")
    'Cnx.Execute stSQL
     
    'Cat.Tables.Append Tbl
     
    With rsT
        .ActiveConnection = Cnx
        '.Open oTables, adOpenKeyset, adLockOptimistic, adCmdTableDirect ', LockType:=adLockOptimistic
        '.Open "Collaborateurs", Cnx, adOpenKeyset ', LockType:=adLockOptimistic
        .Open "Collaborateurs", Cnx ', adOpenKeyset, adLockOptimistic, adCmdTable
    End With
     
    'supprime l'enregistrement si le champ 'Ville' est vide
    Requete = "DELETE * FROM " & "Collaborateurs" '& " WHERE [Ville]IS NULL "
    Cnx.Execute Requete
     
    'Boucle sur les lignes et colonnes de la plage pour remplir table
    'j = 0
    For L = 2 To UBound(TabPlage)
    '' Parcourir chaque champ de l'enregistrement courant
    '   For Each champ In rsT.Fields
    '        champ = Ws.Cells(L, 1 + j)
    '        j = j + 1
    '   Next champ
        With rsT
            '.AddNew
            For C = 1 To DerCol
                .Fields(C - 1).Value = TabPlage(L, C)
            Next
            .AddNew
        .Update
        End With
    Next L
    '*******************************************************************
     
    Set Cat = Nothing
    Set Cnx = Nothing
    Set rsT = Nothing
    Set Ws = Nothing
    Exit Sub
     
    ErrHandl:
     
    MsgBox Err.Number & vbCr & Err.Description, vbCritical
    rep = Err.Number & vbCr & Err.Description
    Ws.Range("J11") = rep
    'If Not rsT Is Nothing Then
    '    If rsT.State = adStateOpen Then rsT.Close
    'End If
    Set rsT = Nothing
     
    If Not Cnx Is Nothing Then
        If Cnx.State = adStateOpen Then Cnx.Close
    End If
    Set Cnx = Nothing
     
    Set Cat = Nothing
    'Set Cnx = Nothing
    'Set rsT = Nothing
    Set Ws = Nothing
     
     
    End Sub

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2007] VBA: Transfert de données d'Excel vers Access
    Par kimou75 dans le forum Macros et VBA Excel
    Réponses: 59
    Dernier message: 23/03/2016, 17h51
  2. Transfert de données d'excel vers access
    Par mataw dans le forum VBA Access
    Réponses: 1
    Dernier message: 04/04/2012, 09h04
  3. Importation de données d'Excel vers Access
    Par jolemoine dans le forum Access
    Réponses: 9
    Dernier message: 13/07/2007, 14h56
  4. transfert de donnés de excel vers Mysql
    Par mkachekh dans le forum Outils
    Réponses: 4
    Dernier message: 20/10/2006, 12h08
  5. [VBA-E]Exporter des données d'Excel vers Access
    Par Mut dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 03/06/2006, 15h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo