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
| Sub AjouterDesEnregistrementsAUneTable()
Dim test As Byte
Dim rs As Recordset
Dim MyDB As Database, MyTable As Recordset, Sh As Worksheet
test = 0
Set MyDB = OpenDatabase("S:\Qualité\BDD Qualité\BDD Qualité.mdb")
Set MyTable = MyDB.OpenRecordset("produits")
Set Sh = Worksheets("Feuil1")
Set rs = "Select distinct sap from produits"
For Each r In .Range("A5:C300").Rows
Do While (rs.EOF = False And test = 0)
'Si la clé de ta ligne à ajouter est deja utilisée alors on stop de comparer
If (rs!sap = Sh.Cells(r.Row, 1)) Then test = 1
Loop
'si la clé est non prise alors on ajoute
If (test = 0) Then
With MyTable
.AddNew
!sap = Sh.Cells(r.Row, 1)
!nom = Sh.Cells(r.Row, 2)
!prenom = Sh.Cells(r.Row, 3)
.Update
End With
End If
test = 0
Next
End Sub |
Partager