Excel VBA Adding records below existing values -
i've been developing little tool query's our database , returns references.
i'm having problem adding newly query'd values below existing values in excel sheet1.
option explicit public ref string const dwconnectstring = "provider=sqloledb... " public property rref() string rref = me.textbox1.value ref = trim(rref) end property private sub textbox1_change() dim rref string rref = me.textbox1.value end sub private sub zoekref_click() dim cn object dim rs object dim cm object dim ref string dim strsource string dim startrow integer ref = rref set cn = createobject("adodb.connection") cn.open dwconnectstring set rs = createobject("adodb.recordset") 'rs = new adodb.recordset strsource = "select consignment.consignment, consignment.document_remark_2, invoice_hist.net_amount, invoice_hist.vat_amount, invoice_hist.inv_currency " strsource = strsource & "from consignment left outer join invoice_hist on consignment.consignment=invoice_hist.consignment " strsource = strsource & "where document_remark_2 '%" strsource = strsource & ref & "%'" rs.open strsource, cn if rs.eof msgbox "geen resultaten" exit sub else dim fieldnames, j rs.movefirst redim fieldnames(rs.fields.count - 1) j = 0 rs.fields.count - 1 fieldnames(j) = rs.fields(j).name next sheet1.range(sheet1.cells(1, 1), sheet1.cells(1, rs.fields.count)).value = fieldnames j = 1 rs.fields.count sheet1.columns(j).autofit next sheet1.cells.copyfromrecordset rs 'fldcount2 = sheets("sheet1").usedrange.rows.count sheet1.rows(1).insert sheet1.range(sheet1.cells(1, 1), sheet1.cells(1, rs.fields.count)).value = fieldnames startrow = 3 until rs.eof rs.movenext startrow = startrow + 1 loop end if rs.close set rs = nothing cn.close set cn = nothing end sub i thought using line:
do until trim(cells(startrow,1).value) = "" startrow = startrow + 1 loop before rs.movenext lines, seems test recordset, not actual excel file.
can test current sheet1's values before adding new recordset comes below what's existing?
thanks help.
expand scope of loop.
rs.movefirst until rs.eof 'do work here 'then increment counter , recordset rs.movenext startrow = startrow + 1 loop
Comments
Post a Comment