+ Reply to Thread
Results 1 to 3 of 3

How do I retrieve the column names in a ADODB recordset from MS SQ

  1. #1
    MChrist
    Guest

    How do I retrieve the column names in a ADODB recordset from MS SQ

    The following is a code chunk that I am using to retrieve a recordset from an
    MS SQL Server using a function that I created on the server. The function
    returns the records fine, but it doesn't return the column headers.

    I would like to use the information to create/update a pivot table directly,
    without having to paste the data to the spreadsheet via the copyfromrecordset
    method.

    Any ideas of what I'm missing.

    Thanks in advance.

    Mark

    Set Cnxn = New ADODB.Connection
    Cnxn.ConnectionTimeout = 0
    Cnxn.Open strCnxn

    'SQL to call from db
    Set rs = New ADODB.Recordset
    strSQL = "SELECT ClientID, Client, [Desc], Tool, Amount FROM
    dbo.fnMyFunction('" _
    & Format(DateValue(dtStart), "mm/dd/yyyy") & "','" _
    & Format(DateValue(dtEnd), "mm/dd/yyyy") & "')"


  2. #2
    DM Unseen
    Guest

    Re: How do I retrieve the column names in a ADODB recordset from MS SQ

    Mark,

    you can create a pivottable in code directly on top of your SQL, no
    need to get the ADO recordset first.

    You can either work through code in one go using a pivottable object,
    but I suggest you first create a manual pivottable report, and then use
    some code to set the parameters in the sql string, since pivottable
    reports do not allow parameters.

    The easiest way is as follows:

    create a stored proc to wrap all SQL stuff up, XL uses ODBC and can be
    picky on some SQL (like table functions!), but stored procs always
    work. Start Macro recorder for later VBA code reference and create a
    PVtable from external data, and in MS Query use File->execute SQL. Now
    select your sp and fill in parameters directly. Run the PV it and stop
    the recorder. Look at the SQL property and change that with VBA
    according your parameters before each run.

    DM Unseen


  3. #3
    K Dales
    Guest

    RE: How do I retrieve the column names in a ADODB recordset from MS SQ

    The column headers for an ADO recordset are in the field names, e.g.
    rs.Fields(1).Name.

    If you want to use this query to populate a pivottable, though, a more
    direct way is to use the SourceData property of the pivottable object. You
    can use this to specify your SQL connection string and query. From the help
    file:

    SourceData Property

    Returns the data source for the PivotTable report, as shown in the following
    table. Read-only Variant...

    External data source: An array. Each row consists of an SQL connection
    string with the remaining elements as the query string, broken down into
    255-character segments.

    "MChrist" wrote:

    > The following is a code chunk that I am using to retrieve a recordset from an
    > MS SQL Server using a function that I created on the server. The function
    > returns the records fine, but it doesn't return the column headers.
    >
    > I would like to use the information to create/update a pivot table directly,
    > without having to paste the data to the spreadsheet via the copyfromrecordset
    > method.
    >
    > Any ideas of what I'm missing.
    >
    > Thanks in advance.
    >
    > Mark
    >
    > Set Cnxn = New ADODB.Connection
    > Cnxn.ConnectionTimeout = 0
    > Cnxn.Open strCnxn
    >
    > 'SQL to call from db
    > Set rs = New ADODB.Recordset
    > strSQL = "SELECT ClientID, Client, [Desc], Tool, Amount FROM
    > dbo.fnMyFunction('" _
    > & Format(DateValue(dtStart), "mm/dd/yyyy") & "','" _
    > & Format(DateValue(dtEnd), "mm/dd/yyyy") & "')"
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1