Données excel vers une table access

Résolu
eric_epinay Messages postés 3 Date d'inscription samedi 18 août 2007 Statut Membre Dernière intervention 27 novembre 2008 - 27 nov. 2008 à 17:10
cs_eren Messages postés 38 Date d'inscription vendredi 3 janvier 2003 Statut Membre Dernière intervention 27 novembre 2008 - 27 nov. 2008 à 17:41
Bonjour?
je cherche une solution , pour créer depuis excel, une macro, qui me permette d'ajouter des valeurs placées dans des cellules excel, vers une table access
est ce possible? quelqu'un peut il m'aider

Y a t'il une procédure pour
Depuis excel,
Ouvrir un fichier access, ouvrir une table et y placer des données ?

Merci par avance

ebl

1 réponse

cs_eren Messages postés 38 Date d'inscription vendredi 3 janvier 2003 Statut Membre Dernière intervention 27 novembre 2008
27 nov. 2008 à 17:41
Bonjour,
'Export data from Excel to Access (ADO)
'If you want to export data to an Access table from an Excel worksheet,
'the macro example below shows how this can be done:


Sub ADOFromExcelToAccess()
' Ajouter le complément Microsoft ActiveX Data Objects 2.x Library
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
    ' connect to the Access database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\FolderName\DataBaseName.mdb;"
    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable  ' all records in a table
    r = 2 ' the start row in the worksheet
    Do While Len(Range("A" & r).Formula) > 0 ' repeat until first empty cell in column A
        With rs
            .AddNew ' create a new record
            ' add values to each field in the record
            .Fields("FieldName1") = Range("A" & r).Value
            .Fields("FieldName2") = Range("B" & r).Value
            .Fields("FieldName3") = Range("C" & r).Value
            ' add more fields if necessary...
            .Update ' stores the new record
        End With
        r = r + 1 ' next row
    Loop
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub
3
Rejoignez-nous