1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   | BOOL CDataDlg::OnInitDialog() 
{
	HRESULT hr = NOERROR;
 
	CDialog::OnInitDialog();
 
	try
	{
		m_pRs.CreateInstance(__uuidof(Recordset));
 
		// La requête est du genre "SELECT Champ1, Champ2 FROM Table"
		m_pRs->Open((LPCTSTR)m_strCmdText, (LPCTSTR)m_strConnection, adOpenDynamic, adLockOptimistic, adCmdText);
 
		if (FAILED(hr = m_pRs->QueryInterface(__uuidof(IADORecordBinding), (LPVOID *)&m_piAdoRecordBinding)))
			_com_issue_error(hr);
		if (FAILED(hr = m_piAdoRecordBinding->BindToRecordset(this)))
			_com_issue_error(hr);
 
		// Test si le jeu d'enregistrement est vide, s'il est vide on créer un nouvel enregistrement vierge
		if (m_pRs->BOF == -1 && m_pRs->EndOfFile == -1)
		{
			AddNew ();
		}
 
		else
			RefreshBoundData();
	}
 
	catch (_com_error &e)
	{
		GenerateError(e.Error(), e.Description());
	}
 
	return TRUE;	
}
 
void CDataDlg::AddNew ()
{
	try
	{
		m_piAdoRecordBinding -> Update (this);
 
		if (m_pRs->Supports(adAddNew))
		{
			NewBlanckRecord ();
			m_piAdoRecordBinding -> AddNew (this);
			m_pRs->MoveLast ();
		}
	}
 
	catch (_com_error &e)
	{
		GenerateError(e.Error(), e.Description());
	}
} | 
Partager