The Generic recordset

The GenericRecordset is a simple wraper for DAO recordset that
simplifies the use of the CDaoRecordset::GetFieldValue()
function. It allows you to create a recordset based on a query,
and recrieve the resulting set using the following functions:

  • GetCString()
  • GetShort()
  • GetLong()
  • GetDouble()
  • GetFloat()
  • GetDate() (this is a COleDate)
  • GetColumn() (this is a VARIANT)
  • GetOleColumn() (COleVariant) (new)

We have added the GenericRectodset to our small utility dll we
use everywhere. This has simplifies many of the things we do. For
example if you need a quick SUM of a column you would do this:

GenericRecordset SumSet(pSomeCDaoDatabase);

SumSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT SUM(amnt_owed) FROM billing");
TotalOwed = SumSet.GetFloat(0);
SumSet.Close();

All the "get" functions
allow you to use either the column number, or the column name.
This sample could have just as easily been:

float nTotalOwed;
GenericRecordset SumSet(pSomeCDaoDatabase);

SumSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT SUM(amnt_owed) as total_amnt FROM billing");
TotalOwed = SumSet.GetFloat("total_amnt");
SumSet.Close();

We not only use the GenericRecordset for simple queries, but
we have just about eliminated the use of the "wizard"
based recordsets in our applications. It is simpler for us and
requires much less coding. We have recently added some more
functionallity to the GenericRecordset. These new functions are:

SetKeys(CString) Tells the set which columns are the keys. The

format is a coma delimited CString

"key_field_name,key_field_name……" This is used

internally by the operator == function.

CString KeyValue = "user_id,user_department";

MySet.SetKeys(KeyValue);

operator += Appends a record into the set. You can either send
this function a comma delimited CString. (there is no need to put
simgle quotes arounds the strings) or you can send it a similar
GenericRecordset.

CString NewRecord = "1,5,Bob,Jones,San Diego";
MySet += NewRecord;

Or

MySet1 += MySet2; (where MySet1 and MySet2 are exactly the same
columns)


operator = updates a record in the set. You can either send
this function a comma delimited CString. (there is no need to put
simgle quotes arouns the strings) or you can send it a similar
GenericRecordset.

CString UpdateRecord = "1,5,Bob,Jones,San Diego";
MySet = UpdateRecord;

Or

MySet1 = MySet2; (where MySet1 and MySet2 are exactly the same
columns)


operator == checks for the existance of a record in the set.
This compairs 2 recordsets or a CString that contains the key
values.

CString CheckRecord = "12435";
if(MySet == NewRecord)
AfxMessageBox("RecordExists");

Or
if(MySet1 == MySet2)
AfxMessageBox("Record Exists");

The GenericRecordset is simple. There is nothing special about
it,but it performs a neat little job that is otherwise a pain in
the fanny. There is an obvious lack of exception handling in the
code. We handle all exceptions from our code. I would advice you
to do the same. We are currently working on several control
binding functions that will allow you to easily populate a
listbox or edit control based on a query. You will see these down
the road, or please feel free to make that modification and tell
me about it.

If you find any bugs please let me know. Thanks and enjoy.
Also please feel free to write me about anything about the
GenericRecordset.



Download source with example- 2 KB

Date updated: 27 Juli

More by Author

Get the Free Newsletter!

Subscribe to Data Insider for top news, trends & analysis

Must Read