Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

Bring in column title with query in VBA like Excel's query wizard

WHen using Excel's quiery wizard, it brings in the column titles, but is there a way to set a VBA SQL statement to do the same?  That is, rather than having to name the feilds with code in VBA, can the SQL statement be set to bring the column headers in as well?
With Workbooks(strWbkName).Worksheets("DataSource") 'Current month's Green Package destination workbook
    strSQL = "SELECT [ClientCode] , [Fund/Sector],[%NotionalScaling], [%NotionalBench], " & _
          "[SpreadDurationContributionPort], [SpreadDurationContributionBench] " & _
          "FROM " & strRangeName & " "
    Set rst = New ADODB.Recordset
     Call rst.Open(strSQL, strCnn, CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly, CommandTypeEnum.adCmdText)
            strLastRow = .Range(strDestination & "65536").End(xlUp).Address
            intRowNum = (.Range(strLastRow).Row) + 1
     Call .Range(strDestination & intRowNum).CopyFromRecordset(rst)  'Need to dynamcially determin last cell
End With

Open in new window

Avatar of dlmille
dlmille
Flag of United States of America image

Select Column_Name from table should return all column names.

Dave
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sandra Smith

ASKER

Thanks Dave.

Sandra