Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / SQL

Excel user defined functions unlimited

Rate me:
Please Sign up or sign in to vote.
4.81/5 (15 votes)
30 Nov 200619 min read 222.8K   5.7K   77  
Describes how to return tables of values as the result of user defined functions (UDF) in Excel.
//---------------------------------------------------------------------------
//  Excel Multivalue Formula Add-In
//  Copyright (C) <2005> <Herbert Danler>
//  Contact: danler@users.sourceforge.net
//  Project Home Page: http://excelmvf.sourceforge.net/
//
//  This program is free software; you can redistribute it and/or modify it under
//  the terms of the GNU General Public License as published by the Free Software
//  Foundation; either version 2 of the License, or (at your option) any
//  later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
//  or FITNESS FOR A PARTICULAR PURPOSE.
//  See the GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along with
//  this program; if not, write to the Free Software Foundation, Inc.,
//  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//---------------------------------------------------------------------------

/***********************************************************************
 * Module:  TOrderItemDataset.cpp
 * Author:  Herbert Danler
 * Modified: Montag, 8. November 2004 15:13:19
 * Purpose: Implementation of the class TOrderItemDataset
 * Comment: A TOrderItemDataset instance contains all orderItems of a certain period of time. Data is returned as a VARIANT containing a two dimensional array of VARIANTs.
 *    The class also provides sorting 
 ***********************************************************************/
#include "stdafx.h"
#include "TOrderItemRecord.h"
#include "TSgADOQuery.h"
#include "TOrderItemDataset.h"


std::string TOrderItemDataset::SQL="Select * from orderitems where orderdate between ? and ?";

////////////////////////////////////////////////////////////////////////
// Name:       TOrderItemDataset::retrieve()
// Purpose:    Implementation of TOrderItemDataset::retrieve()
// Return:     int
////////////////////////////////////////////////////////////////////////

int TOrderItemDataset::retrieve(void)
{
   AdoNS::_ParameterPtr param1("ADODB.Parameter");
   AdoNS::_ParameterPtr param2("ADODB.Parameter");
   AdoNS::_RecordsetPtr recordset("ADODB.Recordset");
   AdoNS::_CommandPtr query = TSgADOQuery::getInstance();
   
   std::string result  = verifySortingCriterias(sortCriterias);
   std::string orderclause;
   if (result!=""){
	   orderclause.append(" order by ");
	   orderclause.append(result);
   }
   query->CommandText=std::string(SQL).append(orderclause).c_str();
   //query->CommandType=AdoNS::adCmdText;
   
   //add parameters
   param1=query->CreateParameter("startdate",AdoNS::adDate,AdoNS::adParamInput,sizeof(DATE),startDate);
   param2=query->CreateParameter("enddate",AdoNS::adDate,AdoNS::adParamInput,sizeof(DATE),endDate);
   query->Parameters->Append(param1);
   query->Parameters->Append(param2);
   
   
   recordset =query->Execute(NULL,NULL,AdoNS::adCmdText);

   

   recordContainer.clear();
   int recordcounter=0;
   setTimestamp(); //unvalid values are stored in the container too, so timestamp has to be set independent from success of fetch
   while (!recordset->EndOfFile){
      TOrderItemRecord orderItemRecord;
      orderItemRecord.orderID =               recordset->Fields->Item["orderID"]->Value;
      orderItemRecord.customername =          recordset->Fields->Item["customer"]->Value;
      orderItemRecord.orderdate =             recordset->Fields->Item["orderdate"]->Value;
      orderItemRecord.status =                recordset->Fields->Item["status"]->Value;
      orderItemRecord.deliverydate =          recordset->Fields->Item["deliverydate"]->Value;
      orderItemRecord.description =           recordset->Fields->Item["description"]->Value;
      orderItemRecord.price =                 recordset->Fields->Item["price"]->Value;
      orderItemRecord.amount =                recordset->Fields->Item["amount"]->Value;
      orderItemRecord.totalprice =            recordset->Fields->Item["totalprice"]->Value;
      recordContainer.push_back(orderItemRecord);
      recordcounter++;
      recordset->MoveNext();
   }
   
   recordset->Close();
   setTimestamp();
   setvalid();
   return recordcounter;
}



////////////////////////////////////////////////////////////////////////
// Name:       TOrderItemDataset::TOrderItemDataset(VARIANT pstartdate, VARIANT penddate, std::string psortcriterias)
// Purpose:    Implementation of TOrderItemDataset::TOrderItemDataset()
// Parameters:
// - pstartdate
// - penddate
// - psortcriterias
// Return:     
////////////////////////////////////////////////////////////////////////

TOrderItemDataset::TOrderItemDataset(VARIANT pstartdate, VARIANT penddate, std::string psortcriterias)
{
   startDate = pstartdate;
   endDate =   penddate;
   sortCriterias = psortcriterias;
   
}

////////////////////////////////////////////////////////////////////////
// Name:       TOrderItemDataset::~TOrderItemDataset()
// Purpose:    Implementation of TOrderItemDataset::~TOrderItemDataset()
// Return:     
////////////////////////////////////////////////////////////////////////

TOrderItemDataset::~TOrderItemDataset()
{
   // TODO : implement
}



////////////////////////////////////////////////////////////////////////
// Name:       TOrderItemDataset::setStartDate(VARIANT newStartDate)
// Purpose:    Implementation of TOrderItemDataset::setStartDate()
// Parameters:
// - newStartDate
// Return:     void
////////////////////////////////////////////////////////////////////////

void TOrderItemDataset::setStartDate(VARIANT newStartDate)
{
   startDate = newStartDate;
}

////////////////////////////////////////////////////////////////////////
// Name:       TOrderItemDataset::setEndDate(VARIANT newEndDate)
// Purpose:    Implementation of TOrderItemDataset::setEndDate()
// Parameters:
// - newEndDate
// Return:     void
////////////////////////////////////////////////////////////////////////

void TOrderItemDataset::setEndDate(VARIANT newEndDate)
{
   endDate = newEndDate;
}

////////////////////////////////////////////////////////////////////////
// Name:       TOrderItemDataset::operator<(const excelmvf::TDataset& pmvrecordset)
// Purpose:    Implementation of TOrderItemDataset::operator<()
// Parameters:
// - pmvrecordset
// Return:     bool
////////////////////////////////////////////////////////////////////////

bool TOrderItemDataset::operator<(const excelmvf::TDataset<TOrderItemRecord>& pmvrecordset) const
{
   const TOrderItemDataset* tempObject;
   if (tempObject=dynamic_cast<const TOrderItemDataset*> (&pmvrecordset)){
        if (startDate.date < tempObject->startDate.date)
            return true;
        else if  (startDate.date == tempObject->startDate.date){
            if (endDate.date < tempObject->endDate.date)
               return true;
            else if  (endDate.date == tempObject->endDate.date){
                 if (sortCriterias < tempObject->sortCriterias)
                    return true;
            }
        }           
   }
   return false;
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions