excel - How does the CopyFromRecordset rs work -
hi want know how part of code pasting data? why not pasting data 1 row below other
set targetrange = targetrange.cells(a, 1) set cn1 = new adodb.connection cn1.open "provider=microsoft.ace.oledb.12.0; data source=" & dbfullname & ";" set rs = new adodb.recordset rs .open sqlstr, cn1, , adcmdtext if = 1 intcolindex = 0 rs.fields.count - 1 ' field names targetrange.offset(0, intcolindex).value = "'" & rs.fields(intcolindex).name next intcolindex targetrange.offset(1, 0).copyfromrecordset rs ' recordset data elseif <> 1 targetrange.offset(1, 0).copyfromrecordset rs end if end rs.close set rs = nothing cn1.close set cn1 = nothing
the context provided incomplete
- is there other code before posted?
- is part of function or entire function?
- where variables declared , initialized?
- what issue - not pasting anything, or part?
the code not organized, if entire sub() in first line:
set targetrange = targetrange.cells(a, 1)
"a" initialized 0 , nothing work
.
i'm guessing code should this:
.
option explicit public sub getdbdata(byref rng range, byval sqlstr string, byval dbname string) dim targetrng range dim targetrow long dim targetcol long dim cn adodb.connection dim rs adodb.recordset set cn = new adodb.connection set rs = new adodb.recordset cn.open "provider=microsoft.ace.oledb.12.0; data source=" & dbname & ";" rs.open sqlstr, cn, , adcmdtext set targetrng = rng.cells(1, 1) targetrng targetcol = 0 rs.fields.count - 1 'field names .offset(0, targetcol).value2 = "'" & rs.fields(targetcol).name next .offset(1, 0).copyfromrecordset rs 'db data end rs.close cn.close set rs = nothing set cn = nothing end sub
(this not tested)
Comments
Post a Comment