excel vba - SQL query to extract employee details from access table -
i new excel vba. have user form in trying populate names of employees amo. have database called ofc
. under have table employeedetails
. primary key peoplesoftid
.
here structure , contents of employee
table:
peoplesoftid nameofemployee racifid employeeid designation 43243309 maddala v43309 99651823 amo 43243310 abhishek a43301 99651824 amo 43243311 atanu a43311 99651825 mo 43243312 rajiv r43312 99651826 cse
this code i've written far:
dim cnn adodb.connection 'dim ado collection class dim rs adodb.recordset 'dim ado recordset class dim dbpath string dim sql string dim integer dim var 'add error handling on error goto errhandler: 'disable screen flickering. application.screenupdating = false dbpath = "e:\office_hsbc\ofc.accdb" var = "amo" set cnn = new adodb.connection ' initialise collection class variable cnn.connectionstring = "provider=microsoft.ace.oledb.12.0;data source=" & dbpath cnn.open sql = "select nameofemployee employeedetails designation= '" & var & "'" set rs = new adodb.recordset 'assign memory recordset rs.open sql, cnn if rs.eof , rs.bof rs.close cnn.close 'clear memory set rs = nothing set cnn = nothing 'enable screen. application.screenupdating = true msgbox "there no records in recordset!", vbcritical, "no records" exit sub end if = 0 rs.fields.count - 1 comboamo.additem rs.fields(i).value, next rs.close cnn.close set rs = nothing set cnn = nothing msgbox "congratulation data has been imported", vbinformation, "import successful" 'error handler on error goto 0 exit sub errhandler: 'clear memory set rs = nothing set cnn = nothing msgbox "error " & err.number & " (" & err.description & ") in procedure import_data"
you need move through each record in recordset. trying read of fields single record query returns 1 field. try instead:
msgbox "there no records in recordset!", vbcritical, "no records" exit sub end if = 0 until rs.eof comboamo.additem rs.fields("nameofemployee").value, rs.movenext = + 1 loop rs.close
Comments
Post a Comment