Sub test()
Dim Cn As String, Sql As String
Const Fichier = "C:\Myrep\1 exemple ecriture dans fichier fermé\1 exemple ecriture dans fichier fermé\exemple ADO ajouter une ligne dans un range ou un tableau structuré\destination.xlsx", AvecTitre = True
Cn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=" & Array("No", "YES")(Abs(AvecTitre)) & ";"""
Sql = "Insert Into [Feuil1$] ([nom], [prenom],[Tel]) values(?,?,?);
Set a = ExecuteRequete(Sql, Cn, "[nom]", "TOTO", "[prenom]", "1234", "[Tel]", "060606")
Sql = "select * from [Feuil1$] where [nom]=?"
Set a = ExecuteRequete(Sql, Cn, "[nom]", "TOTO")
End Sub
Function ExecuteRequete(Sql As String, Cn As String, ParamArray Param() As Variant) As Object
Dim I As Integer
With CreateObject("ADODB.Command")
.ActiveConnection = Cn
.CommandType = 1
.CommandTimeout = 500
.CommandText = Sql
For I = LBound(Param) To UBound(Param) Step 2
Set prm = CreateObject("ADODB.Parameter")
prm.Name = Param(I): prm.Value = Param(I + 1): prm.Type = 12
.Parameters.Append prm
Next
Set ExecuteRequete = .Execute
End With
End Function