vba - Pulling Column Names into Excel from SQL query -
i'm using excel pull data sql db. used code question , works fine. want pull in column names table in addition actual table. figured out names using each fld loop. there's still issue of populating them horizontally in row in excel number of columns might change - i'm thinking need each loop or similar.
sub getdatafromado() 'declare variables' set objmyconn = new adodb.connection set objmycmd = new adodb.command set objmyrecordset = new adodb.recordset 'open connection' objmyconn.connectionstring = "provider=sqloledb;data source=localhost;user id=abc;password=abc;" objmyconn.open 'set , excecute sql command' set objmycmd.activeconnection = objmyconn objmycmd.commandtext = "select * mytable" objmycmd.commandtype = adcmdtext objmycmd.execute 'loop names' ' here????' 'open recordset' set objmyrecordset.activeconnection = objmyconn objmyrecordset.open objmycmd 'copy data excel' activesheet.range("a1").copyfromrecordset (objmyrecordset) end sub
my usual code similar:
for intcolindex = 0 objmyrecordset.fields.count - 1 range("a4").offset(0, intcolindex).value = objmyrecordset.fields(intcolindex).name next
Comments
Post a Comment