Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CTime and CRecordset

90 views
Skip to first unread message

Tim Morgan

unread,
Feb 11, 2000, 3:00:00 AM2/11/00
to
Hi all.

I am writing a program using VC++ 6.0 that interfaces with SQL 7.0
tables. I am using CRecordSet to do the database access. I can query
the database OK, but have run into a problem when adding new records
into the database.

To add a record, I do the following:
- Open the database
- Call the "CRecordset::AddNew()" method.
- Set the member variables in my recordset to the desired values.
- Call the 'Update()' method of the recordset class.

When I call "Update()" I get the error "Unhandled exception. Access
violation".

After tracing down through the code, I found that I am getting the error
when the field exchange is taking place in my recordset class. The
error occurs on a CTime object in the function "RFX_Date" in the MFC
module "dbrfx.cpp". I have verified that the value passed into this
function is a valid date value. However, in this function, the value
for my CTime object is actually being modified to a non-valid (large
negative value) number. When the GetYear() method is called on the
CTime object, the exception occurs since the number is negative (see
code below).

Am I doing something wrong, or is this a bug? The weird part is, the
new record gets written to the database OK, so this error is occuring
AFTER the database write has occurred. The MFC code is shown below,
with comments where the value of my CTime variable is being modified to
a invalid value.

Thanks for any assistance.

-- Tim
tmo...@envoyglobal.com

/// Code from "mfc\src\dbrfx.cpp in the function RFX_Date()
void AFXAPI RFX_Date(CFieldExchange* pFX, LPCTSTR szName, CTime& value)
{
.
.
.
.
case CFieldExchange::LoadField:
{
// Get the field data
CFieldInfo* pInfo = &pFX->m_prs->m_rgFieldInfos[nField - 1];

// Restore the status
pFX->m_prs->SetFieldStatus(nField - 1, pInfo->m_bStatus);

// If not NULL, restore the value, length and proxy
if (!pFX->m_prs->IsFieldStatusNull(nField - 1))
{


// !!!!!!!!!!!!!!!!!!!!!!!!!! This is where the value of my CTime object
gets incorrectly set!!!!!!!!
AfxCopyValueByRef(pInfo->m_pvDataCache, &value,
plLength, pInfo->m_nDataType);


// Restore proxy for correct WHERE CURRENT OF operations
TIMESTAMP_STRUCT* pts =
(TIMESTAMP_STRUCT*)pFX->m_prs->m_pvFieldProxy[nField-1];

//!!!!!!!! Exception occurs on the next statement.
pts->year = (SWORD)value.GetYear();
pts->month = (UWORD)value.GetMonth();
pts->day = (UWORD)value.GetDay();
pts->hour = (UWORD)value.GetHour();
pts->minute = (UWORD)value.GetMinute();
pts->second = (UWORD)value.GetSecond();
pts->fraction = 0;
}
else
*plLength = SQL_NULL_DATA;

Upon enterth


Scott McPhillips

unread,
Feb 11, 2000, 3:00:00 AM2/11/00
to
Try initializing the CTime variable(s) to any valid date in the recordset
constructor. This fixes a known bug -- not sure if it's the problem you've got.

--
Scott McPhillips [VC++ MVP]

Tim Morgan

unread,
Feb 14, 2000, 3:00:00 AM2/14/00
to Scott McPhillips
That was it! I had come across this article in the Knowledge Base but didn't fully
comprehend it at the time. I made sure the value of the CTime variable was valid
before I called "update", but I hadn't set the value in the constructor. I didn't
realize it had to be valid when 'add' was called. Thanks for pointing this out.

-- Tim

0 new messages