I am having some serious trouble trying to append data to an already existing Access table from Excel using Excel VBA.
I have data in a worksheet in Excel(active worksheet) and I would like to transfer the contents of the row A4-BL4 and append it to a Table in Access called Table1 each time I trigger this macro from excel.
I am trying to do this with ADO and a recordset, but dont really know what I am doing 🙂
I do not want to delete any of the contents in the access table, but basically just insert the new row into it without overriding or deleting anything.
My code is the following, it currently gives me an error “Arguements of the wrong type, are out of acceptable range,or are in conflict with one another”
on the line :
rs.Open qry, cn, adOpenKeyset
I am almost certain I am going the wrong way about doing this, I appreciate any help thank you.
I am very new to working with Access so my code will probably provide a good laugh 🙂
It is absolutely crucial this must be triggered from excel and not access
Sub EXPORT()
Application.ScreenUpdating = False
Dim cn As ADODB.Connection
Dim rs As New ADODB.Recordset
Set cn = New ADODB.Connection
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Users\yilmadu001\Desktop\Database.accdb;"
cn.Open (strcon)
qry = "SELECT * FROM Table1"
rs.Open qry, cn, adOpenKeyset
rs.AddNew
rs.Fields("Date") = Range("A4").Value
rs.Fields("Order Number") = Range("B4").Value
rs.Fields("Product Code") = Range("C4").Value
rs.Fields("E-Number") = Range("D4").Value
rs.Fields("size") = Range("E4").Value
rs.Fields("type") = Range("F4").Value
rs.Fields("type2") = Range("G4").Value
rs.Fields("Voltage") = Range("H4").Value
rs.Fields("Compound") = Range("I4").Value
rs.Fields("Number") = Range("J4").Value
rs.Fields("Tag Number") = Range("K4").Value
rs.Fields("Quantity") = Range("L4").Value
rs.Fields("zero") = Range("M4").Value
rs.Fields("net") = Range("N4").Value
rs.Fields("Actual net") = Range("O4").Value
rs.Fields("min") = Range("P4").Value
rs.Fields("Actual min") = Range("Q4").Value
rs.Fields("max1") = Range("R4").Value
rs.Fields("max2") = Range("S4").Value
rs.Fields("Top") = Range("T4").Value
rs.Fields("2") = Range("U4").Value
rs.Fields("side") = Range("V4").Value
rs.Fields("5") = Range("W4").Value
rs.Fields("Bottom") = Range("X4").Value
rs.Fields("8") = Range("Y4").Value
rs.Fields("side2") = Range("Z4").Value
rs.Fields("10") = Range("AA4").Value
rs.Fields("set1") = Range("AB4").Value
rs.Fields("set2") = Range("AC4").Value
rs.Fields("set3") = Range("AD4").Value
rs.Fields("Usage1") = Range("AE4").Value
rs.Fields("Usage2") = Range("AF4").Value
rs.Fields("Main (%)") = Range("AG4").Value
rs.Fields("A (%)") = Range("AH4").Value
rs.Fields("ratio (%)") = Range("AI4").Value
rs.Fields("set3") = Range("AJ4").Value
rs.Fields("set4") = Range("AK4").Value
rs.Fields("Core ") = Range("AL4").Value
rs.Fields("Tip1") = Range("AM4").Value
rs.Fields("Tip2") = Range("AN4").Value
rs.Fields("Tip3") = Range("AO4").Value
rs.Fields("set5") = Range("AP4").Value
rs.Fields("speed") = Range("AQ4").Value
rs.Fields("speed2") = Range("AR4").Value
rs.Fields("speed3") = Range("AS4").Value
rs.Fields("speed4") = Range("AT4").Value
rs.Fields("speed5") = Range("AU4").Value
rs.Fields("speed6") = Range("AV4").Value
rs.Fields("speed7") = Range("AW4").Value
rs.Fields("setting") = Range("AX4").Value
rs.Fields("Selected") = Range("AY4").Value
rs.Fields("S/U1") = Range("AZ4").Value
rs.Fields("S/U2") = Range("BA4").Value
rs.Fields("S/U3") = Range("BB4").Value
rs.Fields("ON/OFF") = Range("BC4").Value
rs.Fields("YES/NO") = Range("BD4").Value
rs.Fields("NOTES") = Range("BE4").Value
rs.Fields("Size1") = Range("BF4").Value
rs.Fields("Size2") = Range("BG4").Value
rs.Fields("Size3") = Range("BH4").Value
rs.Fields("Temp1") = Range("BI4").Value
rs.Fields("Temp2") = Range("BJ4").Value
rs.Fields("Temp3") = Range("BK4").Value
rs.Fields("Temp4") = Range("BL4").Value
rs.Update
rs.Close
db.Close
Application.ScreenUpdating = True
End Sub
You need to specify your recordset to be update-able:
rs.Open qry, cn, adOpenKeyset, adLockOptimistic
since your query is simply getting all of the records you can use adCmdTable
option, but you have to pass the name of the table only:
rs.Open "Table1", cn, adOpenKeyset, adLockOptimistic, adCmdTable
Tags: excelexcel, vba