In following code I can export 1 line of data from Excel to an Acces DataBase. But how do I have to change my code such that more lines are exported, e.g. all the lines where value in A column is not empty? (And maye there is an easier code?)
Sub TestAccess()
Dim DONNEE_1 As String
Dim DONNEE_2 As String
Dim DONNEE_3 As String
Dim DONNEE_4 As String
Dim WBK1 As Workbook
Dim WST1 As Worksheet
Set WBK1 = ActiveWorkbook
Set WST1 = WBK1.Worksheets("Pays")
The first row of data I export
DONNEE_1 = CStr(WST1.Range("A2"))
DONNEE_2 = CStr(WST1.Range("B2"))
DONNEE_3 = CStr(WST1.Range("C2"))
DONNEE_4 = CDate(Now())
Set cn = CreateObject("ADODB.Connection")
name of my destination Access database:
Fichier = "V:\20-STAT-statistiques\WorkDJ\Access test\Test.accdb"
Make a connection with my Excel file:
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
Fichier & ";Persist Security Info=False"
cn.Open
The columns in my Acces database where I want to add data
Set oCm = New ADODB.Command
oCm.ActiveConnection = cn
oCm.CommandText = "INSERT INTO Voyages (Ville,Pays, Continent, Ajout)" &
_"VALUES ('" & DONNEE_1 & "','" & DONNEE_2 & "','" & DONNEE_3 & "', #" &
DONNEE_4 & "#)"
oCm.Execute
cn.Close
End Sub