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 [VC++ MVP]
-- Tim