vba - Update Access database through Excel sheet -
i trying update table in access through code shows error message "user define type not define". how solve problem? enter code here
sub updateclick() dim conn adodb.connection dim myrecordset adodb.recordset dim strconn string dim s string set s = "c:\users\pc2\documents\database2.accdb" strconn = "provider=microsoft.jet.oledb.4.0 data source=s" set strconn = new adodb.connection set myrecordset = new adodb.recordset myrecordset .open "select * personinformation", strconn, adopenkeyset, adlockoptimistic .fields("id").value = worksheets("sheet1").range("a2").value .fields("fname").value = worksheets("sheet1").range("b2").value .fields("lname").value = worksheets("sheet1").range("c2").value .fields("address").value = worksheets("sheet1").range("d2").value .fields("age").value = worksheets("sheet1").range("e2").value .update .close end set myrecordset = nothing set conn = nothing end sub
after comment noticed had more wrong connection.
adapt code example below - sites such https://www.connectionstrings.com/access/ show string use version of access.
sub fillarrayaccess() dim oconn object dim myrecordset object dim sconn string dim s string s = "c:\users\pc2\documents\database2.accdb" 'your connection string change depending on access version. sconn = "provider=microsoft.jet.oledb.4.0;data source=" & s 'sconn = "provider=microsoft.ace.oledb.12.0;data source=" & s 'access 2010 set oconn = createobject("adodb.connection") oconn.open sconn set myrecordset = createobject("adodb.recordset") myrecordset.open "personinformation", oconn, 1, 3, &h2 myrecordset .addnew .fields("id").value = worksheets("sheet1").range("a2").value .fields("fname").value = worksheets("sheet1").range("b2").value .fields("lname").value = worksheets("sheet1").range("c2").value .fields("address").value = worksheets("sheet1").range("d2").value .fields("age").value = worksheets("sheet1").range("e2").value .update .close end set myrecordset = nothing set oconn = nothing end sub
edit: have updated code work original posters data.
Comments
Post a Comment