Sign in to follow this  

ADO : working with 2 tables

This topic is 4587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, I don't know if I do it right this way, but I have a database with 2 tabels : Persons & Cities On my main page of my application I want to show the person with the city where he lives and the postal code to achieve this I join the 2 tables like this :
::CoInitialize(NULL) ;

	m_strConnection = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ..//adressen.mdb") ;
	
	m_strCmdText = "SELECT Personen.IDPersoon, Personen.Voornaam, Personen.Achternaam, Personen.[Straat&Nummer], Gemeentes.Gemeente, Gemeentes.Postcode FROM Gemeentes INNER JOIN Personen ON Gemeentes.IDGemeente=Personen.IDGemeente" ;
       m_pRS = NULL ;
	m_piAdoRecordBinding = NULL ;
	
	m_pRS.CreateInstance(__uuidof(Recordset)) ;
	
	try
	{
		m_pRS->Open((LPCTSTR)m_strCmdText,
					(LPCTSTR)m_strConnection,
					adOpenKeyset,
					adLockOptimistic,
					adCmdUnknown) ;
	}
	catch(_com_error &e)
	{
		AfxMessageBox(e.Description()) ;
	}

	if (SUCCEEDED(m_pRS->QueryInterface(__uuidof(IADORecordBinding), (LPVOID*) &m_piAdoRecordBinding)))
	{
		m_piAdoRecordBinding->BindToRecordset(&m_rsADORecSetPersonen) ;
		return S_OK ;
	}
	else
	{
		MessageBox(NULL, "Fout bij het aanspreken van de database!", "Fout!", MB_OK) ;
		return S_FALSE ;
	}
}

and I have a class for the recordbinding : [code] class CAdoRsPersonen : public CADORecordBinding { BEGIN_ADO_BINDING(CAdoRsPersonen) ADO_FIXED_LENGTH_ENTRY ( 1, adInteger, m_lnIDPersoon, m_ulIDPersStatus, false) ADO_VARIABLE_LENGTH_ENTRY2 ( 2, adVarChar, m_szNaam, sizeof(m_szNaam), m_ulNaamStatus, true) ADO_VARIABLE_LENGTH_ENTRY2 ( 3, adVarChar, m_szVoornaam, sizeof(m_szVoornaam), m_ulVoornaamStatus, true) ADO_VARIABLE_LENGTH_ENTRY2 ( 4, adVarChar, m_szStraat, sizeof(m_szStraat), m_ulStraatStatus, true) ADO_VARIABLE_LENGTH_ENTRY2 ( 5, adVarChar, m_szGemeente, sizeof(m_szGemeente), m_ulGemeenteStatus, true) ADO_VARIABLE_LENGTH_ENTRY2 ( 6, adVarChar, m_szPostcode, sizeof(m_szPostcode), m_ulPostcodeStatus, true) END_ADO_BINDING() public: CAdoRsPersonen(); virtual ~CAdoRsPersonen(); long int m_lnIDPersoon ; ULONG m_ulIDPersStatus ; CHAR m_szNaam[51] ; ULONG m_ulNaamStatus ; CHAR m_szVoornaam[51] ; ULONG m_ulVoornaamStatus ; CHAR m_szStraat[51] ; ULONG m_ulStraatStatus ; long int m_lnIDGemeente ; ULONG m_ulIDGemStatus ; CHAR m_szGemeente[51] ; ULONG m_ulGemeenteStatus ; CHAR m_szPostcode[11] ; ULONG m_ulPostcodeStatus ; }; [/source] This works fine! But now I want to fill a combo box with all the available cities in my "Cities"-table so I thought, lets make a second class :
class CAdoRsGemeentes : public CADORecordBinding  
{
	BEGIN_ADO_BINDING(CAdoRsGemeentes)
		ADO_FIXED_LENGTH_ENTRY		( 1, adInteger, m_lnIDGemeente,							m_ulIDGemStatus,	false) 
		ADO_VARIABLE_LENGTH_ENTRY2	( 2, adVarChar, m_szGemeente,	sizeof(m_szGemeente),	m_ulGemeenteStatus,	true)
		ADO_VARIABLE_LENGTH_ENTRY2	( 3, adVarChar, m_szPostcode,	sizeof(m_szPostcode),	m_ulPostcodeStatus,	true)
	END_ADO_BINDING()

public:
	CAdoRsGemeentes();
	virtual ~CAdoRsGemeentes();
	
	long int			m_lnIDGemeente ;
	ULONG				m_ulIDGemStatus ;
	CHAR				m_szGemeente[51] ;
	ULONG				m_ulGemeenteStatus ;
	CHAR				m_szPostcode[11] ;
	ULONG				m_ulPostcodeStatus ;
};

and when I fill the combobox I use the same m_pRS, but I bind the recordset to the class of the "cities" table and load the postal codes and the cities, like this :
HRESULT CAdoDatabase::LaadRecordGemeentes()
{
	CString	strStatus ;
	
	SetQuery("Select * from Gemeentes") ;
	
	if (SUCCEEDED(m_pRS->QueryInterface(__uuidof(IADORecordBinding), (LPVOID*) &m_piAdoRecordBinding)))
	{
		m_piAdoRecordBinding->BindToRecordset(&m_rsADORecSetGemeentes) ;
		return S_OK ;
	}
	else
	{
		MessageBox(NULL, "Fout bij het aanspreken van de database!", "Fout!", MB_OK) ;
		return S_FALSE ;
	}

	if (adFldOK==m_rsADORecSetGemeentes.m_ulIDGemStatus)
		m_lnIDGemeente = m_rsADORecSetGemeentes.m_lnIDGemeente ;
	else
	{
		if (m_rsADORecSetGemeentes.m_ulIDGemStatus!=3)
		{
			strStatus.Format("%d", m_rsADORecSetGemeentes.m_ulIDGemStatus) ;
			MessageBox(NULL, "Problemen met het verkrijgen van het 'IDGemeente'-veld : " + strStatus, "Fout!", MB_OK) ;
			return S_FALSE ;
		}
	}

	if (adFldOK==m_rsADORecSetGemeentes.m_ulPostcodeStatus)
		m_strPostcode = m_rsADORecSetGemeentes.m_szPostcode ;
	else
	{
		if (m_rsADORecSetGemeentes.m_ulPostcodeStatus!=3)
		{
			strStatus.Format("%d", m_rsADORecSetGemeentes.m_ulPostcodeStatus) ;
			MessageBox(NULL, "Problemen met het verkrijgen van het 'Postcode'-veld : " + strStatus, "Fout!", MB_OK) ;
			return S_FALSE ;
		}
		else
			m_strPostcode = _T("") ;
	}

	if (adFldOK==m_rsADORecSetGemeentes.m_ulGemeenteStatus)
		m_strStad = m_rsADORecSetGemeentes.m_szGemeente ;
	else
	{
		if (m_rsADORecSetGemeentes.m_ulGemeenteStatus!=3)
		{
			strStatus.Format("%d", m_rsADORecSetGemeentes.m_ulGemeenteStatus) ;
			MessageBox(NULL, "Problemen met het verkrijgen van het 'Stad'-veld : " + strStatus, "Fout!", MB_OK) ;
			return S_FALSE ;
		}
		else
			m_strStad = _T("") ;
	}
	
	return S_OK ;
}

When I run my application I get no errors, but the combobox is filled with 3 same cities (3 times the city of record number 2), where in the "Cities" table, I have 4 cities. What am I doing wrong?

Share this post


Link to post
Share on other sites

This topic is 4587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this