Probleme ajout enregistrement dans une table access via ado
Bonjour à tous,
je charge une table access nomée Fam_BD sur Excel via OLEDB
Cette table sert à alimenter une listview, sur lequel j'ai un bouton add new et via lequel je souhaite ajouter un enregistrement à ma table sur access.
ma table comporte 3 champs.
les nouvelles informations du champs1 sont inscrit dans une textbox2
celles du champs2 dans une textbox1
celles du champs3 dans une textbox3
une fois avoir vérifier que c'est informations ne sont pas en doublons dans la table sur excel, je souhaite les ajouter à la table access .
Pour cela j'utilise ado et la macro ci-dessous.
Dim Cn As ADODB.Connection
Dim Fichier As String, TexteSQL As String
Dim Chemin As String
Dim NomBD As String
Dim NomTable As String
Chemin = Workbooks(ThisWorkbook.Name).Path & "\BD\Input\"
NomBD = "BD PMT.accdb"
NomTable = "Fam_BD"
Set Cn = New ADODB.Connection
Cn.Open "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & Chemin & NomBD
'Insertion des données dans la Table1 qui contient 3 champs:
'1 champ date (doit être encadrée par le symbole dièse #)
'1 champ nombre
'1 champ texte (doit être encadré par des apostrophes ')
TexteSQL = "INSERT INTO [Fam_BD] VALUES ('" & TextBox2.Value & "','" & TextBox1.Value & "','" & TextBox3.Value & "')"
Cn.Execute TexteSQL
Cn.Close
Set Cn = Nothing
J'ai alors une erreur sur la ligne
Cn.Execute TexteSQL
quelqu'un serait'il comment y remédier?
Cordialement
Bon ça m'a pris 4 heures mais j'ai enfin trouvé une solution. J'ai laissé tombé la macro qui bugait et j'ai utilisé celle-ci. J'espère qu'elle pourra servir à d'autres.
Maintenant encore 4heures pour faire pareil avec modifier
'source :http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=173:import-export-data-from-access-to-excel-using-ado&catid=79&Itemid=475
'Using ADO to Export data from Excel worksheet (your host application) to an Access Database Table.
'refer Image 10a to view the existing SalesManager Table in MS Access file "SalesReport.accdb"
'refer Image 10b for data in Excel worksheet which is exported to Access Database Table.
'refer Image 10c to view the SalesManager Table in Access file "SalesReport.accdb", after data is exported.
'To use ADO in your VBA project, you must add a reference to the ADO Object Library in Excel (your host application) by clicking Tools-References in VBE, and then choose an appropriate version of Microsoft ActiveX Data Objects x.x Library from the list.
'--------------
'DIM STATEMENTS
Dim strMyPath As String, strDBName As String, strDB As String, strSQL As String
Dim i As Long, n As Long, lastRow As Long, lFieldCount As Long
'instantiate an ADO object using Dim with the New keyword:
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
'--------------
'THE CONNECTION OBJECT
strDBName = "BD PMT.accdb"
strMyPath = Workbooks(ThisWorkbook.Name).Path & "\BD\Input\"
strDB = strMyPath & strDBName
'Connect to a data source:
'For pre - MS Access 2007, .mdb files (viz. MS Access 97 up to MS Access 2003), use the Jet provider: "Microsoft.Jet.OLEDB.4.0". For Access 2007 (.accdb database) use the ACE Provider: "Microsoft.ACE.OLEDB.12.0". The ACE Provider can be used for both the Access .mdb & .accdb files.
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB
'--------------
'OPEN RECORDSET, ACCESS RECORDS AND FIELDS
Dim ws As Worksheet
'set the worksheet:
Set ws = ActiveWorkbook.Sheets("Fam_BD")
'Set the ADO Recordset object:
Set adoRecSet = New ADODB.Recordset
'Opening the table named SalesManager:
strTable = "Fam_BD"
adoRecSet.Open Source:=strTable, ActiveConnection:=connDB, CursorType:=adOpenStatic, LockType:=adLockOptimistic
'--------------
'COPY RECORDS FROM THE EXCEL USERFORM:
adoRecSet.AddNew
adoRecSet.Fields(0).Value = UCase(Me.TextBox2.Value)
adoRecSet.Fields(1).Value = UCase(Me.TextBox1.Value)
adoRecSet.Fields(2).Value = UCase(Me.TextBox3.Value)
adoRecSet.Update
'--------------
'close the objects
adoRecSet.Close
connDB.Close
'destroy the variables
Set adoRecSet = Nothing
Set connDB = Nothing
Unload Me