ADO Recordset Delete problem 
Author Message
 ADO Recordset Delete problem

I'm trying to use ADO to delete a record.  It blows up in here everytime,
with an access violation, unless I use adAffectCurrent, which doesn't delete
anything.  Please let me know what I'm doing wrong

thanks
Ken

// here is where it blows  up
inline HRESULT _Recordset15::Delete ( enum AffectEnum AffectRecords ) {
    HRESULT _hr = raw_Delete(AffectRecords);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _hr;

Quote:
}

// start of my code

BOOL afuntion
{
....

        AttComboBox.GetLBText(AttComboBox.GetCurSel(), acString);
// I've tried adOpenStatic, and adOpenDynamic
        mystruct = theApp.DB->OpenRecordSetTemp("G28", acString, adOpenDynamic);

        if (mystruct)
        {
                mystruct->adoRecordset->Delete(adAffectAll);  <-----  blows up here
                theApp.DB->CloseUpdateRecordSetTemp(mystruct);
..........

Quote:
}

RecordStruct * CAdoDB::OpenRecordSetTemp(LPCTSTR tablename, LPCTSTR casenum,
CursorTypeEnum cursorType)
{

        HRESULT hr;
        CString table;
        table = tablename;

        // if not then open and add to RecordSetArray
        // RecordStruct stores table name and the record set
        RecordStruct *mystruct = new RecordStruct;
        ASSERT (mystruct);

        // get the recordset and if successful save in RecordSetArray
        try
        {
                hr = mystruct->adoRecordset.CreateInstance( __uuidof(Recordset) );
        }
        catch( HRESULT hr )
        {
                ReportHRError(hr, "Error initializing ADO recordset object");
                delete mystruct;
                return FALSE;
        }

        TRACE( _T("Successfully created ADO recordset object.\n") );
        if (HasWorkID(tablename))
                mystruct->sql = "SELECT * FROM " + table + " WHERE workid = " + "'" +
casenum + "'";
        else
                mystruct->sql = "SELECT * FROM " + table + " WHERE case_num = " + "'" +
casenum + "'";

        _bstr_t bstrSQL( _T(mystruct->sql) );

        // make recordset a disconnected recordset
        mystruct->adoRecordset->CursorLocation = adUseClient;

        // Hook connection object to recordset and open recordset.
        mystruct->adoRecordset->PutRefActiveConnection( adoConnection );

        hr = mystruct->adoRecordset->Open( _variant_t( bstrSQL ),  vtMissing,
                                                                cursorType, adLockBatchOptimistic, adCmdText );

        mystruct->adoRecordset->PutRefActiveConnection( NULL );

        if ( FAILED( hr ) )
        {
                ReportHRError(hr, "Error opening ADO recordset");
                delete mystruct;
                return FALSE;
        }

        //TRACE("Successfully opened ADO recordset. Table = %s\n", table);
        if (VARIANT_TRUE == mystruct->adoRecordset->ADO_EOF)
        {
                /*
                mystruct->adoRecordset->AddNew();
                _bstr_t bstr( _T(pcasenum) );
                mystruct->adoRecordset->Fields->Item[_variant_t(_T("case_num"))]->Value =
_variant_t( bstr );
                // mark as a modified record
                mystruct->Modified = TRUE;

                mystruct->adoRecordset->PutRefActiveConnection( adoConnection );
                hr = mystruct->adoRecordset->UpdateBatch(adAffectAll);
                if ( FAILED( hr ) )
                {
                        ReportHRError(hr, "Error updating ADO recordset");
                }
                hr = mystruct->adoRecordset->Requery(_variant_t(long(0)));
                if ( FAILED( hr ) )
                {
                        ReportHRError(hr, "Error on requery of ADO recordset");
                }
                mystruct->adoRecordset->PutRefActiveConnection( NULL );
                */
                PclError(MError, "Record not found");
                delete mystruct;

                return NULL;
        }
        // track it
        mystruct->Modified = FALSE;
        mystruct->tablename = tablename;
        return (mystruct);

Quote:
}

BOOL CAdoDB::CloseUpdateRecordSetTemp(RecordStruct *mystruct)
{
        HRESULT hr;
        if (mystruct)
        {
                if (mystruct->Modified) // this number can be greater than 1
                {
                        mystruct->adoRecordset->PutRefActiveConnection( adoConnection );
                        // do a batchupdate here
                        hr = mystruct->adoRecordset->UpdateBatch(adAffectAll);
                        // check for errors
                        if( FAILED( hr ) )

                                TRACE( "*** HRESULT ***" );
                                TRACE( LogCrackHR( hr )  );
                                LogAdoErrorImport(adoConnection);
                        }
                        mystruct->adoRecordset->PutRefActiveConnection( NULL );
                }
                mystruct->adoRecordset->Close();
                delete mystruct;
                return TRUE;
        }
        else
        {
                PclError(MError, "UpdateAndCloseRecordset: Invalid Recordstruct");
                return FALSE;
        }
        return TRUE;

Quote:
}



Fri, 13 Jul 2001 03:00:00 GMT  
 ADO Recordset Delete problem
Why don't you just use SQL to delete all records, if this is what you want:
Like "Delete from TableX"
Stefan Zschocke
Quote:

>I'm trying to use ADO to delete a record.  It blows up in here everytime,
>with an access violation, unless I use adAffectCurrent, which doesn't
delete
>anything.  Please let me know what I'm doing wrong

>thanks
>Ken

>// here is where it blows  up
>inline HRESULT _Recordset15::Delete ( enum AffectEnum AffectRecords ) {
>    HRESULT _hr = raw_Delete(AffectRecords);
>    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
>    return _hr;
>}

>// start of my code

>BOOL afuntion
>{
>....

> AttComboBox.GetLBText(AttComboBox.GetCurSel(), acString);
>// I've tried adOpenStatic, and adOpenDynamic
> mystruct = theApp.DB->OpenRecordSetTemp("G28", acString, adOpenDynamic);

> if (mystruct)
> {
> mystruct->adoRecordset->Delete(adAffectAll);  <-----  blows up here
> theApp.DB->CloseUpdateRecordSetTemp(mystruct);
>..........

>}

>RecordStruct * CAdoDB::OpenRecordSetTemp(LPCTSTR tablename, LPCTSTR
casenum,
>CursorTypeEnum cursorType)
>{

> HRESULT hr;
> CString table;
> table = tablename;

> // if not then open and add to RecordSetArray
> // RecordStruct stores table name and the record set
> RecordStruct *mystruct = new RecordStruct;
> ASSERT (mystruct);

> // get the recordset and if successful save in RecordSetArray
> try
> {
> hr = mystruct->adoRecordset.CreateInstance( __uuidof(Recordset) );
> }
> catch( HRESULT hr )
> {
> ReportHRError(hr, "Error initializing ADO recordset object");
> delete mystruct;
> return FALSE;
> }

> TRACE( _T("Successfully created ADO recordset object.\n") );
> if (HasWorkID(tablename))
> mystruct->sql = "SELECT * FROM " + table + " WHERE workid = " + "'" +
>casenum + "'";
> else
> mystruct->sql = "SELECT * FROM " + table + " WHERE case_num = " + "'" +
>casenum + "'";

> _bstr_t bstrSQL( _T(mystruct->sql) );

> // make recordset a disconnected recordset
> mystruct->adoRecordset->CursorLocation = adUseClient;

> // Hook connection object to recordset and open recordset.
> mystruct->adoRecordset->PutRefActiveConnection( adoConnection );

> hr = mystruct->adoRecordset->Open( _variant_t( bstrSQL ),  vtMissing,
> cursorType, adLockBatchOptimistic, adCmdText );

> mystruct->adoRecordset->PutRefActiveConnection( NULL );

> if ( FAILED( hr ) )
> {
> ReportHRError(hr, "Error opening ADO recordset");
> delete mystruct;
> return FALSE;
> }

> //TRACE("Successfully opened ADO recordset. Table = %s\n", table);
> if (VARIANT_TRUE == mystruct->adoRecordset->ADO_EOF)
> {
> /*
> mystruct->adoRecordset->AddNew();
> _bstr_t bstr( _T(pcasenum) );
> mystruct->adoRecordset->Fields->Item[_variant_t(_T("case_num"))]->Value =
>_variant_t( bstr );
> // mark as a modified record
> mystruct->Modified = TRUE;

> mystruct->adoRecordset->PutRefActiveConnection( adoConnection );
> hr = mystruct->adoRecordset->UpdateBatch(adAffectAll);
> if ( FAILED( hr ) )
> {
> ReportHRError(hr, "Error updating ADO recordset");
> }
> hr = mystruct->adoRecordset->Requery(_variant_t(long(0)));
> if ( FAILED( hr ) )
> {
> ReportHRError(hr, "Error on requery of ADO recordset");
> }
> mystruct->adoRecordset->PutRefActiveConnection( NULL );
> */
> PclError(MError, "Record not found");
> delete mystruct;

> return NULL;
> }
> // track it
> mystruct->Modified = FALSE;
> mystruct->tablename = tablename;
> return (mystruct);

>}

>BOOL CAdoDB::CloseUpdateRecordSetTemp(RecordStruct *mystruct)
>{
> HRESULT hr;
> if (mystruct)
> {
> if (mystruct->Modified) // this number can be greater than 1
> {
> mystruct->adoRecordset->PutRefActiveConnection( adoConnection );
> // do a batchupdate here
> hr = mystruct->adoRecordset->UpdateBatch(adAffectAll);
> // check for errors
> if( FAILED( hr ) )

> TRACE( "*** HRESULT ***" );
> TRACE( LogCrackHR( hr )  );
> LogAdoErrorImport(adoConnection);
> }
> mystruct->adoRecordset->PutRefActiveConnection( NULL );
> }
> mystruct->adoRecordset->Close();
> delete mystruct;
> return TRUE;
> }
> else
> {
> PclError(MError, "UpdateAndCloseRecordset: Invalid Recordstruct");
> return FALSE;
> }
> return TRUE;
>}



Sat, 14 Jul 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Problem with ADO recordsets of recordsets under MTS

2. ADO+Recordset+Delete

3. deleting records in an ado recordset

4. Strange delete problem with DAO recordset

5. Speed of ADO Recordset vs ADO Command in VC

6. Strange delete problem with DAO recordset

7. ADO.Stream and ADO.RecordSet

8. Problems getting values from ADO Recordset

9. Problem with IDL for COM server returning ADO disconnected Recordset

10. Returning ADO Recordset Problem

11. Problem in UpdateBatch of ADO Recordset

12. Problem with ADO Recordset

 

 
Powered by phpBB® Forum Software