Delete - Worked Example
key : purple - original code / blue - new code
Step 1 :
Link the Delete menu item to an OnDelete() function within the View. Add the following code. The name Mr William Wordworth will be deleted from the database.
void
CInterView::OnDelete()
{
_RecordsetPtr pRecordSet;
CInterDoc * pDoc;
pDoc = GetDocument();
HRESULT hr;
_bstr_t bstrQuery(
"SELECT * FROM Names WHERE ID = 76");
_variant_t vNull;
vNull.vt = VT_ERROR;
vNull.scode = DISP_E_PARAMNOTFOUND;
try
{hr = pRecordSet.CreateInstance(_uuidof(Recordset));
if (SUCCEEDED(hr))
{pRecordSet->PutRefActiveConnection(pDoc->m_pConnection);
hr = pRecordSet->Open(_variant_t(bstrQuery), vNull,
adOpenForwardOnly, adLockOptimistic, adCmdText);
if (!pRecordSet->GetadoEOF())
{pRecordSet->Delete(adAffectCurrent);
pRecordSet->Close();}
}
}
catch( _com_error &e )
{TRACE( "Error:%08lx.\n", e.Error());
TRACE( "ErrorMessage:%s.\n", e.ErrorMessage());
TRACE( "Source:%s.\n", (LPCTSTR) _bstr_t(e.Source()));
TRACE( "Description:%s.\n", (LPCTSTR) _bstr_t(e.Description()));}
catch(...)
{TRACE( "\n*** Unhandled Exception ***\n" );
}
}
Click here for the basic exercise
Click here for the source code
Click here to return to menu