-
MFC : searching a database for the second time
Hello,
Let me first explain what I'm trying to do :
I have a main dialogbox with a list of all the ppl in my database. When I click on 1 of them, their address appears in a textbox beneath it.
How am I trying to do it :
First I make sure that the ID of that person is in the List control.
When I click on that person I retrieve the ID from the Listcontrol.
Then I pass that ID onto a function that searches the database for that ID, like this :
Code:
CString strFilter = "SELECT * FROM Personen WHERE ID LIKE '" ;
strFilter += strID ;
strFilter += "'" ;
m_pDB->Close() ;
m_pDB->Open(AFX_DB_USE_DEFAULT_TYPE, strFilter) ;
after that I just retrieve the address from the record that I found.
Problem :
When I click the first time on a person, everything works. But then when I click on a different person, my textbox stays the same. (I think the record just stays the same). I already tried a :
Code:
CString strFilter = "SELECT * FROM Personen" ;
m_pDB->Close() ;
m_pDB->Open(AFX_DB_USE_DEFAULT_TYPE, strFilter) ;
What am I still missing here?
thanx in advance for any help!
-
Re: MFC : searching a database for the second time
Is m_pDB a databse?
I assume this is ODBC. You should be using CRecordsets, and their members m_strFilter and Requery. Opening and closing a database over and over is a very inefficent way of going about things.
-
Re: MFC : searching a database for the second time
refresh your recordset with Requery
or
could it be likely that u r passing the same strID all the time....
-
Re: MFC : searching a database for the second time
m_pDB is a pointer to a CRecordset yes
and it is ODBC
I already tried a Requery AND I'm sure that I don't pass the same strID too
-
Re: MFC : searching a database for the second time
You should be using m_strFilter not your own CString object called strFilter.
-
Re: MFC : searching a database for the second time
what exactly do you mean? Is there somewhere a good example on how to use a SQL query on the net?
-
Re: MFC : searching a database for the second time
Quote:
Originally Posted by da_cobra
what exactly do you mean? Is there somewhere a good example on how to use a SQL query on the net?
Read the CRecordset documentation in MSDN. It explains it pretty well.
-
Re: MFC : searching a database for the second time
I just read the MSDN and came up with this solution (but the same problem again :( )
Code:
HRESULT CPersonen::ZoekID(int nID)
{
CString strID ; // needed to convert the ID
m_pDB->m_strFilter = "" ; // reset filter
if (!m_pDB->CanRestart())
return S_FALSE ; // Unable to requery
else
if (!m_pDB->Requery())
return S_FALSE ; // requery failed
else
{
strID.Format("%d", nID) ; // convert ID to CString
CString strFilter = "ID = " ;
strFilter += strID ;
m_pDB->m_strFilter = strFilter ;
MessageBox(NULL, m_pDB->m_strFilter, "Filter", MB_OK) ;
if (!m_pDB->CanRestart())
return S_FALSE ; // Unable to requery
else
if (!m_pDB->Requery())
return S_FALSE ; // requery failed
else
return S_OK ;
}
}
this is the function that I use to search for an ID :
First I set the strFilter to "", so I get all my records.
Then I set the strFilter to "ID = number"
Now when I click the first time on a name I call this function and a message box appears with the strFilter which says "ID = 1" (everything correct!)
BUT when I check the current record, it's ID is -842150451!!!
Anyway, now when I press a button "more..." I get a dialog box with more info on that person, when I click OK on that dialog box I return to my main dialog box (with the textbox with the address in it)
Now when I select another person, the correct address is showed again, when I select another person, it stays the same address again (untill I open/close that 2nd dialogbox)
so to make a long story short :)
I pass on the right filter! But the same record is shown (or the very first time, a totally wrong record is shown (ID = -842150451))
I hope my explanation makes sense...
-
Re: MFC : searching a database for the second time
Why are you doing the initial Requery with m_strFilter = "" but not doing anything with the results? That call accomplishes nothing but to waste time.
What are you looking at to determine the current record's ID?
-
Re: MFC : searching a database for the second time
Correct me if I do not understand exactly what happens, but if I do a requery on a recordset with a filter then I only get those records with comply to that filter right?
so I first need to "get" all the records again (with m_strFilter="") before I can search for a given record, or do I have this all wrong?!?
I reply later on how I get the Id from the current record, because I found something which is not good for me :D
edit : I found the solution to the first problem of this thread! :)
I still have a question left (besides the one above)
if I want to show all names who start with an "a" for example, why doesn't this work :
m_strFilter = "name = 'a%'"
-
Re: MFC : searching a database for the second time
Quote:
Originally Posted by da_cobra
Correct me if I do not understand exactly what happens, but if I do a requery on a recordset with a filter then I only get those records with comply to that filter right?
so I first need to "get" all the records again (with m_strFilter="") before I can search for a given record, or do I have this all wrong?!?
I reply later on how I get the Id from the current record, because I found something which is not good for me :D
edit : I found the solution to the first problem of this thread! :D
You have it all wrong. There's no need to do the first query.
What was the problem?
-
Re: MFC : searching a database for the second time
It was a stupid mistake from my side.
I have to write an application with a new class for the database.
when I create that class for the first time I pass a pointer of the recordset to this class and I load all the fields into the member variables of that class.
But each time I searched for an ID, I didn't reload the fields in my class. A stupid mistake on which I searched a whole week for :(
Can you still answer that last question on how to search for names who begin with an "a" for example?
m_strFilter = "name = 'a%'" doesn't work :(
and what if I want to work with numbers and "OR" like this :
strFilter = "Naam = '1%' OR Naam = '2%' OR Naam = '3%' " ;
strFilter += "OR Naam = '4%' OR Naam = '5%' OR Naam = '6%' OR Naam = '7%' " ;
strFilter += "OR Naam = '8%' OR Naam = '9%' OR Naam = '0%'" ;
edit : oh, before I forget, thx for helping me, you put me on the right track by asking how I got the ID from the recordset (the first time I got it fromm the class, which wasn't updated yet)
-
Re: MFC : searching a database for the second time
Change = to LIKE
It still doesn't seem like you're using the CRecordset correctly. Do you have member variables set up and RFX data exchange mechanisms in place?
-
Re: MFC : searching a database for the second time
it works now!
thx alot
What do you mean by : "Do you have member variables set up and RFX data exchange mechanisms in place?"
In my self made class I have some member variables like : m_strName and I import the field Name through the passed on Recordset (m_pSet)
so :
1) I create my class : CPersons database ;
2) I pass on the recordset : database.GetRecordset(m_pSet)
3) everytime I change a record I reload the member variables with the fields from the recordset : m_strName = m_pSet->strName
-
Re: MFC : searching a database for the second time
What you're doing sounds very nonstandard. If you use class wizard to create a CRecordset, it'll give you member variables corresponding to the database elements. They're automatically updated for you through RFX (Recordset Field Exchange) functions. I would recommend using them rather than trying to reinvent the wheel and make more work for yourself.
What I'm asking is how do you transfer the data between the database and your variables. Are you doing the SQLBInd and SQLFetch statements yourself?
-
Re: MFC : searching a database for the second time
hey, my teacher told me to write my own database class, so I did :D
"What I'm asking is how do you transfer the data between the database and your variables. Are you doing the SQLBInd and SQLFetch statements yourself?"
What the **** are you talking about?!? :D
Sorry but I never heard of SQLBind and/or SQLFetch
-
Re: MFC : searching a database for the second time
You have to transfer data from the database to your program. You do that by using SQLBind to map a database column to your variable, then SQLFetch to actually transfer the data. You have to be doing that somewhere or you're not actually getting data from your database to your app. That's why I think you're missing quite a bit.
Can you post your recordset class header and implementation files?
-
Re: MFC : searching a database for the second time
sure I can post them, but their are mainly in dutch though and pretty big
header file
Code:
// Personen.h: interface for the CPersonen class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_PERSONEN_H__F1C3437E_9C5A_498A_8219_35BB0FB69B10__INCLUDED_)
#define AFX_PERSONEN_H__F1C3437E_9C5A_498A_8219_35BB0FB69B10__INCLUDED_
#include "EindwerkSet.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CPersonen
{
public:
CPersonen() ; // constructor
virtual ~CPersonen() ; // destructor
HRESULT FilterStartMet(char ch);
HRESULT SetRecordSet(CEindwerkSet *pDatabaseSet) ;
HRESULT Tonen(long int lnID) ;
HRESULT Annuleren() ;
HRESULT OpslaanRecord(bool bToevoegen) ;
HRESULT LaadRecord() ;
HRESULT Zoeken() ;
HRESULT Verwijderen(long int lnID) ;
HRESULT Wijzigen() ;
HRESULT Toevoegen() ;
HRESULT MaakEmail(int nIndex) ;
HRESULT ToonAdres(int nIndex) ;
HRESULT ToonWebsite(int nIndex) ;
HRESULT ZoekID(int nID) ;
HRESULT WisVelden() ;
void SetID(long int lnID) ;
void SetNaam(CString strNaam) ;
void SetVoornaam(CString strVoornaam) ;
void SetBijnaam(CString strBijnaam) ;
void SetTypeAdres1( int nTypeAdres1);
void SetTypeAdres2( int nTypeAdres2);
void SetNaamAdres1(CString strNaamAdres1) ;
void SetNaamAdres2(CString strNaamAdres2) ;
void SetStraat1(CString strStraat1) ;
void SetStraat2(CString strStraat2) ;
void SetNummer1(CString strNummer1) ;
void SetNummer2(CString strNummer2) ;
void SetPostcode1(int nPostcode1) ;
void SetStad1(CString strStad1) ;
void SetStad2(CString strStad2) ;
void SetLand1(CString strLand1) ;
void SetLand2(CString strLand2) ;
void SetPostcode2(int nPostcode2) ;
void SetTypeTel1(int nTypeTel1);
void SetTypeTel2(int nTypeTel2);
void SetTypeTel3(int nTypeTel3);
void SetTypeTel4(int nTypeTel4);
void SetTel1(CString strTel1) ;
void SetTel2(CString strTel2) ;
void SetTel3(CString strTel3) ;
void SetTel4(CString strTel4) ;
void SetTypeEmail1(int nTypeEmail1) ;
void SetTypeEmail2(int nTypeEmail2) ;
void SetTypeEmail3(int nTypeEmail3) ;
void SetTypeEmail4(int nTypeEmail4) ;
void SetEmail1(CString strEmail1) ;
void SetEmail2(CString strEmail2) ;
void SetEmail3(CString strEmail3) ;
void SetEmail4(CString strEmail4) ;
void SetTypeWebsite1(int nTypeWebsite1) ;
void SetTypeWebsite2(int nTypeWebsite2) ;
void SetWebsite1(CString strWebsite1) ;
void SetWebsite2(CString strWebsite2) ;
void SetTypeChat1(int nTypeChat1) ;
void SetTypeChat2(int nTypeChat2) ;
void SetChat1(CString strChat1) ;
void SetChat2(CString strChat2) ;
void SetNota(CString strNota) ;
long int GetID() ;
CString GetNaam() ;
CString GetVoornaam() ;
CString GetBijnaam() ;
int GetTypeAdres1() ;
int GetTypeAdres2() ;
CString GetNaamAdres1() ;
CString GetNaamAdres2() ;
CString GetStraat1() ;
CString GetStraat2() ;
CString GetNummer1() ;
CString GetNummer2() ;
CString GetStad1() ;
CString GetStad2() ;
int GetPostcode1() ;
int GetPostcode2() ;
CString GetLand1() ;
CString GetLand2() ;
int GetTypeTel1() ;
int GetTypeTel2() ;
int GetTypeTel3() ;
int GetTypeTel4() ;
CString GetTel1() ;
CString GetTel2() ;
CString GetTel3() ;
CString GetTel4() ;
int GetTypeEmail1() ;
int GetTypeEmail2() ;
int GetTypeEmail3() ;
int GetTypeEmail4() ;
CString GetEmail1() ;
CString GetEmail2() ;
CString GetEmail3() ;
CString GetEmail4() ;
int GetTypeWebsite1() ;
int GetTypeWebsite2() ;
CString GetWebsite1() ;
CString GetWebsite2() ;
int GetTypeChat1() ;
int GetTypeChat2() ;
CString GetChat1() ;
CString GetChat2() ;
CString GetNota() ;
CEindwerkSet *GetRecordSet() ;
private:
CEindwerkSet *m_pDB ;
long int m_lnID ;
CString m_strNaam ;
CString m_strVoornaam ;
CString m_strBijnaam ;
int m_nTypeAdres1 ; // 0 = geen, 1 = adres prive,
int m_nTypeAdres2 ; // 2 = adres werk, 3 = andere
CString m_strNaamAdres1 ;
CString m_strStraat1 ;
CString m_strNummer1 ;
CString m_strStad1 ;
int m_nPostcode1 ;
CString m_strLand1 ;
CString m_strNaamAdres2 ;
CString m_strStraat2 ;
CString m_strNummer2 ;
CString m_strStad2 ;
int m_nPostcode2 ;
CString m_strLand2 ;
int m_nTypeTel1 ; // 0 = geen, 1 = telefoon prive,
int m_nTypeTel2 ; // 2 = telefoon werk, 3 = GSM prive,
int m_nTypeTel3 ; // 4 = GSM werk, 5 = fax prive,
int m_nTypeTel4 ; // 6 = fax werk
CString m_strTel1 ;
CString m_strTel2 ;
CString m_strTel3 ;
CString m_strTel4 ;
int m_nTypeEmail1 ; // 0 = geen, 1 = Email prive 1,
int m_nTypeEmail2 ; // 2 = Email prive 2,
int m_nTypeEmail3 ; // 3 = Email prive 3,
int m_nTypeEmail4 ; // 4 = Email werk
CString m_strEmail1 ;
CString m_strEmail2 ;
CString m_strEmail3 ;
CString m_strEmail4 ;
int m_nTypeWebsite1 ; // 0 = geen, 1 = website prive,
int m_nTypeWebsite2 ; // 2 = website werk
CString m_strWebsite1 ;
CString m_strWebsite2 ;
int m_nTypeChat1 ; // 0 = geen, 1 = MSN messenger,
int m_nTypeChat2 ; // 2 = ICQ, 3 = AOL
CString m_strChat1 ;
CString m_strChat2 ;
CString m_strNota ;
} ;
#endif // !defined(AFX_PERSONEN_H__F1C3437E_9C5A_498A_8219_35BB0FB69B10__INCLUDED_)
-
Re: MFC : searching a database for the second time
implementation file
Code:
// Personen.cpp: implementation of the CPersonen class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Eindwerk.h"
#include "EindwerkSet.h"
#include "Personen.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPersonen::CPersonen()
{
}
CPersonen::~CPersonen()
{
}
long int CPersonen::GetID()
{
return m_lnID ;
}
CString CPersonen::GetNaam()
{
return m_strNaam ;
}
CString CPersonen::GetVoornaam()
{
return m_strVoornaam ;
}
CString CPersonen::GetBijnaam()
{
return m_strBijnaam ;
}
int CPersonen::GetTypeAdres1()
{
return m_nTypeAdres1 ;
}
int CPersonen::GetTypeAdres2()
{
return m_nTypeAdres2 ;
}
CString CPersonen::GetNaamAdres1()
{
return m_strNaamAdres1 ;
}
CString CPersonen::GetNaamAdres2()
{
return m_strNaamAdres2 ;
}
CString CPersonen::GetStraat1()
{
return m_strStraat1 ;
}
CString CPersonen::GetStraat2()
{
return m_strStraat2 ;
}
CString CPersonen::GetNummer1()
{
return m_strNummer1 ;
}
CString CPersonen::GetNummer2()
{
return m_strNummer2 ;
}
CString CPersonen::GetStad1()
{
return m_strStad1 ;
}
CString CPersonen::GetStad2()
{
return m_strStad2 ;
}
int CPersonen::GetPostcode1()
{
return m_nPostcode1 ;
}
int CPersonen::GetPostcode2()
{
return m_nPostcode2 ;
}
CString CPersonen::GetLand1()
{
return m_strLand1 ;
}
CString CPersonen::GetLand2()
{
return m_strLand2 ;
}
int CPersonen::GetTypeTel1()
{
return m_nTypeTel1 ;
}
int CPersonen::GetTypeTel2()
{
return m_nTypeTel2 ;
}
int CPersonen::GetTypeTel3()
{
return m_nTypeTel3 ;
}
int CPersonen::GetTypeTel4()
{
return m_nTypeTel4 ;
}
CString CPersonen::GetTel1()
{
return m_strTel1 ;
}
CString CPersonen::GetTel2()
{
return m_strTel2 ;
}
CString CPersonen::GetTel3()
{
return m_strTel3 ;
}
CString CPersonen::GetTel4()
{
return m_strTel4 ;
}
int CPersonen::GetTypeEmail1()
{
return m_nTypeEmail1 ;
}
int CPersonen::GetTypeEmail2()
{
return m_nTypeEmail2 ;
}
int CPersonen::GetTypeEmail3()
{
return m_nTypeEmail3 ;
}
int CPersonen::GetTypeEmail4()
{
return m_nTypeEmail4 ;
}
CString CPersonen::GetEmail1()
{
return m_strEmail1 ;
}
CString CPersonen::GetEmail2()
{
return m_strEmail2 ;
}
CString CPersonen::GetEmail3()
{
return m_strEmail3 ;
}
CString CPersonen::GetEmail4()
{
return m_strEmail4 ;
}
int CPersonen::GetTypeWebsite1()
{
return m_nTypeWebsite1 ;
}
int CPersonen::GetTypeWebsite2()
{
return m_nTypeWebsite2 ;
}
CString CPersonen::GetWebsite1()
{
return m_strWebsite1 ;
}
CString CPersonen::GetWebsite2()
{
return m_strWebsite2 ;
}
int CPersonen::GetTypeChat1()
{
return m_nTypeChat1 ;
}
int CPersonen::GetTypeChat2()
{
return m_nTypeChat2 ;
}
CString CPersonen::GetChat1()
{
return m_strChat1 ;
}
CString CPersonen::GetChat2()
{
return m_strChat2 ;
}
CString CPersonen::GetNota()
{
return m_strNota ;
}
void CPersonen::SetID(long lnID)
{
m_lnID = lnID ;
}
void CPersonen::SetNaam(CString strNaam)
{
m_strNaam = strNaam ;
}
void CPersonen::SetVoornaam(CString strVoornaam)
{
m_strVoornaam = strVoornaam ;
}
void CPersonen::SetBijnaam(CString strBijnaam)
{
m_strBijnaam = strBijnaam ;
}
void CPersonen::SetTypeAdres1(int nTypeAdres1)
{
m_nTypeAdres1 = nTypeAdres1 ;
}
void CPersonen::SetTypeAdres2(int nTypeAdres2)
{
m_nTypeAdres2 = nTypeAdres2 ;
}
void CPersonen::SetNaamAdres1(CString strNaamAdres1)
{
m_strNaamAdres1 = strNaamAdres1 ;
}
void CPersonen::SetNaamAdres2(CString strNaamAdres2)
{
m_strNaamAdres2 = strNaamAdres2 ;
}
void CPersonen::SetStraat1(CString strStraat1)
{
m_strStraat1 = strStraat1 ;
}
void CPersonen::SetStraat2(CString strStraat2)
{
m_strStraat2 = strStraat2 ;
}
void CPersonen::SetNummer1(CString strNummer1)
{
m_strNummer1 = strNummer1 ;
}
void CPersonen::SetNummer2(CString strNummer2)
{
m_strNummer2 = strNummer2 ;
}
void CPersonen::SetPostcode1(int nPostcode1)
{
m_nPostcode1 = nPostcode1 ;
}
void CPersonen::SetPostcode2(int nPostcode2)
{
m_nPostcode2 = nPostcode2 ;
}
void CPersonen::SetStad1(CString strStad1)
{
m_strStad1 = strStad1 ;
}
void CPersonen::SetStad2(CString strStad2)
{
m_strStad2 = strStad2 ;
}
void CPersonen::SetLand1(CString strLand1)
{
m_strLand1 = strLand1 ;
}
void CPersonen::SetLand2(CString strLand2)
{
m_strLand2 = strLand2 ;
}
void CPersonen::SetTypeTel1(int nTypeTel1)
{
m_nTypeTel1 = nTypeTel1 ;
}
void CPersonen::SetTypeTel2(int nTypeTel2)
{
m_nTypeTel2 = nTypeTel2 ;
}
void CPersonen::SetTypeTel3(int nTypeTel3)
{
m_nTypeTel3 = nTypeTel3 ;
}
void CPersonen::SetTypeTel4(int nTypeTel4)
{
m_nTypeTel4= nTypeTel4 ;
}
void CPersonen::SetTel1(CString strTel1)
{
m_strTel1 = strTel1 ;
}
void CPersonen::SetTel2(CString strTel2)
{
m_strTel2 = strTel2 ;
}
void CPersonen::SetTel3(CString strTel3)
{
m_strTel3 = strTel3 ;
}
void CPersonen::SetTel4(CString strTel4)
{
m_strTel4 = strTel4 ;
}
void CPersonen::SetTypeEmail1(int nTypeEmail1)
{
m_nTypeEmail1 = nTypeEmail1 ;
}
void CPersonen::SetTypeEmail2(int nTypeEmail2)
{
m_nTypeEmail2 = nTypeEmail2 ;
}
void CPersonen::SetTypeEmail3(int nTypeEmail3)
{
m_nTypeEmail3 = nTypeEmail3 ;
}
void CPersonen::SetTypeEmail4(int nTypeEmail4)
{
m_nTypeEmail4 = nTypeEmail4 ;
}
void CPersonen::SetEmail1(CString strEmail1)
{
m_strEmail1 = strEmail1 ;
}
void CPersonen::SetEmail2(CString strEmail2)
{
m_strEmail2 = strEmail2 ;
}
void CPersonen::SetEmail3(CString strEmail3)
{
m_strEmail3 = strEmail3 ;
}
void CPersonen::SetEmail4(CString strEmail4)
{
m_strEmail4 = strEmail4 ;
}
void CPersonen::SetTypeWebsite1(int nTypeWebsite1)
{
m_nTypeWebsite1 = nTypeWebsite1 ;
}
void CPersonen::SetTypeWebsite2(int nTypeWebsite2)
{
m_nTypeWebsite2 = nTypeWebsite2 ;
}
void CPersonen::SetWebsite1(CString strWebsite1)
{
m_strWebsite1 = strWebsite1 ;
}
void CPersonen::SetWebsite2(CString strWebsite2)
{
m_strWebsite2 = strWebsite2 ;
}
void CPersonen::SetTypeChat1(int nTypeChat1)
{
m_nTypeChat1 = nTypeChat1 ;
}
void CPersonen::SetTypeChat2(int nTypeChat2)
{
m_nTypeChat2 = nTypeChat2 ;
}
void CPersonen::SetChat1(CString strChat1)
{
m_strChat1 = strChat1 ;
}
void CPersonen::SetChat2(CString strChat2)
{
m_strChat2 = strChat2 ;
}
void CPersonen::SetNota(CString strNota)
{
m_strNota = strNota ;
}
HRESULT CPersonen::ToonWebsite(int nIndex)
{
return S_OK ;
}
HRESULT CPersonen::ToonAdres(int nIndex)
{
return S_OK ;
}
HRESULT CPersonen::MaakEmail(int nIndex)
{
return S_OK ;
}
HRESULT CPersonen::Toevoegen()
{
return S_OK ;
}
HRESULT CPersonen::Wijzigen()
{
return S_OK ;
}
-
Re: MFC : searching a database for the second time
implementation file part 2
Code:
HRESULT CPersonen::Verwijderen(long int lnID)
{
HRESULT hresult ;
hresult = MessageBox(NULL, "Weet je het zeker?", "Bevestiging!", MB_YESNO|MB_ICONQUESTION) ;
if (hresult == IDYES)
{
if (!ZoekID(lnID)) // Search for the specified ID
{
MessageBox(NULL, "Geen record gevonden met die ID!", "Fout!", MB_ICONEXCLAMATION) ;
return S_FALSE ;
}
else
{
m_pDB->Delete() ;
m_pDB->MoveNext() ;
if (m_pDB->IsEOF())
m_pDB->MoveLast() ;
if (m_pDB->IsBOF())
m_pDB->SetFieldNull(NULL) ;
m_pDB->Requery() ;
FilterStartMet('A') ; // Show all records again
return S_OK ;
}
}
FilterStartMet('A') ; // Show all records again
return S_FALSE ;
}
HRESULT CPersonen::Zoeken()
{
return S_OK ;
}
HRESULT CPersonen::Annuleren()
{
return S_OK ;
}
HRESULT CPersonen::Tonen(long int lnID)
{
return S_OK ;
}
HRESULT CPersonen::SetRecordSet(CEindwerkSet *pDatabaseSet)
{
if (pDatabaseSet)
{
m_pDB = pDatabaseSet ;
return S_OK ;
}
else
{
AfxMessageBox("Er kon geen pointer naar de database verkregen worden!", NULL, NULL) ;
exit(0) ;
}
return S_OK ;
}
HRESULT CPersonen::FilterStartMet(char ch)
{
CString strFilter ;
if (ch == '1') // Toon records die beginnen met een cijfer
{
strFilter = "Naam LIKE '1%' OR Naam LIKE '2%' OR Naam LIKE '3%' " ;
strFilter += "OR Naam LIKE '4%' OR Naam LIKE '5%' OR Naam LIKE '6%' OR Naam = '7%' " ;
strFilter += "OR Naam LIKE '8%' OR Naam LIKE '9%' OR Naam LIKE '0%'" ;
}
else if (ch == 'A') // Toon alle records
strFilter = "" ;
else // Toon records die beginnen met character ch
{
strFilter = "Naam LIKE '" ;
strFilter += ch ;
strFilter += "%'" ;
} ;
m_pDB->m_strFilter = strFilter ;
//MessageBox(NULL, m_pDB->m_strFilter, "Filter", MB_OK) ;
if (!m_pDB->CanRestart())
{
MessageBox(NULL, "filter, heropstart mislukt", "Fout!", MB_OK) ;
return S_FALSE ; // Unable to requery
}
else
if (!m_pDB->Requery())
{
MessageBox(NULL, "filter, requery mislukt", "Fout!", MB_OK) ;
return S_FALSE ; // requery failed
}
else
{
//MessageBox(NULL, "Requery!", "Requery!", MB_OK) ;
LaadRecord() ;
return S_OK ;
}
}
HRESULT CPersonen::ZoekID(int nID)
{
CString strID ; // needed to convert the ID
strID.Format("%d", nID) ; // convert ID to CString
CString strFilter = "ID = " ;
strFilter += strID ;
m_pDB->m_strFilter = strFilter ;
//MessageBox(NULL, m_pDB->m_strFilter, "Filter", MB_OK) ;
if (!m_pDB->CanRestart())
{
MessageBox(NULL, "Filter, heropstart mislukt", "Fout!", MB_OK) ;
return S_FALSE ; // Unable to requery
}
else
if (!m_pDB->Requery())
{
MessageBox(NULL, "Filter, requery mislukt", "Fout!", MB_OK) ;
return S_FALSE ; // requery failed
}
else
{
LaadRecord() ;
return S_OK ;
}
}
CEindwerkSet * CPersonen::GetRecordSet()
{
return m_pDB ;
}
HRESULT CPersonen::WisVelden()
{
m_lnID = 0 ;
m_strNaam = "" ;
m_strVoornaam = "" ;
m_strBijnaam = "" ;
m_nTypeAdres1 = 0 ; // 0 = geen, 1 = adres prive,
m_nTypeAdres2 = 0 ; // 2 = adres werk, 3 = andere
m_strNaamAdres1 = "" ;
m_strStraat1 = "" ;
m_strNummer1 = "" ;
m_strStad1 = "" ;
m_nPostcode1 = 0 ;
m_strLand1 = "" ;
m_strNaamAdres2 = "" ;
m_strStraat2 = "" ;
m_strNummer2 = "" ;
m_strStad2 = "" ;
m_nPostcode2 = 0 ;
m_strLand2 = "" ;
m_nTypeTel1 = 0 ; // 0 = geen, 1 = telefoon prive,
m_nTypeTel2 = 0 ; // 2 = telefoon werk, 3 = GSM prive,
m_nTypeTel3 = 0 ; // 4 = GSM werk, 5 = fax prive,
m_nTypeTel4 = 0 ; // 6 = fax werk
m_strTel1 = "" ;
m_strTel2 = "" ;
m_strTel3 = "" ;
m_strTel4 = "" ;
m_nTypeEmail1 = 0 ; // 0 = geen, 1 = Email prive 1,
m_nTypeEmail2 = 0 ; // 2 = Email prive 2,
m_nTypeEmail3 = 0 ; // 3 = Email prive 3,
m_nTypeEmail4 = 0 ; // 4 = Email werk
m_strEmail1 = "" ;
m_strEmail2 = "" ;
m_strEmail3 = "" ;
m_strEmail4 = "" ;
m_nTypeWebsite1 = 0 ; // 0 = geen, 1 = website prive,
m_nTypeWebsite2 = 0 ; // 2 = website werk
m_strWebsite1 = "" ;
m_strWebsite2 = "" ;
m_nTypeChat1 = 0 ; // 0 = geen, 1 = MSN messenger,
m_nTypeChat2 = 0 ; // 2 = ICQ, 3 = AOL
m_strChat1 = "" ;
m_strChat2 = "" ;
m_strNota = "" ;
return S_OK ;
}
HRESULT CPersonen::LaadRecord()
{
m_lnID = m_pDB->m_ID ;
m_strNaam = m_pDB->m_Naam ;
m_strVoornaam = m_pDB->m_Voornaam ;
m_strBijnaam = m_pDB->m_Bijnaam ;
m_nTypeAdres1 = m_pDB->m_Type_adres_1 ; // 0 = geen, 1 = adres prive,
m_nTypeAdres2 = m_pDB->m_Type_adres_2 ; // 2 = adres werk, 3 = andere
m_strNaamAdres1 = m_pDB->m_Naam_1 ;
m_strStraat1 = m_pDB->m_Straatnaam_1 ;
m_strNummer1 = m_pDB->m_Nummer_1 ;
m_strStad1 = m_pDB->m_Stad_1 ;
m_nPostcode1 = m_pDB->m_Postcode_1 ;
m_strLand1 = m_pDB->m_Land_1 ;
m_strNaamAdres2 = m_pDB->m_Naam_2 ;
m_strStraat2 = m_pDB->m_Straatnaam_2 ;
m_strNummer2 = m_pDB->m_Nummer_2 ;
m_strStad2 = m_pDB->m_Stad_2 ;
m_nPostcode2 = m_pDB->m_Postcode_2 ;
m_strLand2 = m_pDB->m_Land_2 ;
m_nTypeTel1 = m_pDB->m_Type_Telefoon_fax_1 ; // 0 = geen, 1 = telefoon prive,
m_nTypeTel2 = m_pDB->m_Type_Telefoon_fax_2 ; // 2 = telefoon werk, 3 = GSM prive,
m_nTypeTel3 = m_pDB->m_Type_Telefoon_fax_3 ; // 4 = GSM werk, 5 = fax prive,
m_nTypeTel4 = m_pDB->m_Type_Telefoon_fax_4 ; // 6 = fax werk
m_strTel1 = m_pDB->m_Telefoon_fax_1 ;
m_strTel2 = m_pDB->m_Telefoon_fax_2 ;
m_strTel3 = m_pDB->m_Telefoon_fax_3 ;
m_strTel4 = m_pDB->m_Telefoon_fax_4 ;
m_nTypeEmail1 = m_pDB->m_Type_Email_1 ; // 0 = geen, 1 = Email prive 1,
m_nTypeEmail2 = m_pDB->m_Type_Email_2 ; // 2 = Email prive 2,
m_nTypeEmail3 = m_pDB->m_Type_Email_3 ; // 3 = Email prive 3,
m_nTypeEmail4 = m_pDB->m_Type_Email_4 ; // 4 = Email werk
m_strEmail1 = m_pDB->m_Email_1 ;
m_strEmail2 = m_pDB->m_Email_2 ;
m_strEmail3 = m_pDB->m_Email_3 ;
m_strEmail4 = m_pDB->m_Email_4 ;
m_nTypeWebsite1 = m_pDB->m_Type_Website_1 ; // 0 = geen, 1 = website prive,
m_nTypeWebsite2 = m_pDB->m_Type_Website_2 ; // 2 = website werk
m_strWebsite1 = m_pDB->m_Website_1 ;
m_strWebsite2 = m_pDB->m_Website_2 ;
m_nTypeChat1 = m_pDB->m_Type_Online_chat_1 ; // 0 = geen, 1 = MSN messenger,
m_nTypeChat2 = m_pDB->m_Type_Online_chat_2 ; // 2 = ICQ, 3 = AOL
m_strChat1 = m_pDB->m_Online_chat_1 ;
m_strChat2 = m_pDB->m_Online_chat_2 ;
m_strNota = m_pDB->m_Nota ;
return S_OK ;
}
HRESULT CPersonen::OpslaanRecord(bool bToevoegen)
{
if (m_pDB->CanUpdate())
{
if (bToevoegen)
m_pDB->AddNew() ;
else
m_pDB->Edit() ;
m_pDB->m_Naam = m_strNaam ;
m_pDB->m_Voornaam = m_strVoornaam ;
m_pDB->m_Bijnaam = m_strBijnaam ;
m_pDB->m_Type_adres_1 = m_nTypeAdres1 ; // 0 = geen, 1 = adres prive,
m_pDB->m_Type_adres_2 = m_nTypeAdres2 ; // 2 = adres werk, 3 = andere
m_pDB->m_Naam_1 = m_strNaamAdres1 ;
m_pDB->m_Straatnaam_1 = m_strStraat1 ;
m_pDB->m_Nummer_1 = m_strNummer1 ;
m_pDB->m_Stad_1 = m_strStad1 ;
m_pDB->m_Postcode_1 = m_nPostcode1;
m_pDB->m_Land_1 = m_strLand1 ;
m_pDB->m_Naam_2 = m_strNaamAdres2 ;
m_pDB->m_Straatnaam_2 = m_strStraat2 ;
m_pDB->m_Nummer_2 = m_strNummer2 ;
m_pDB->m_Stad_2 = m_strStad2 ;
m_pDB->m_Postcode_2 = m_nPostcode2 ;
m_pDB->m_Land_2 = m_strLand2 ;
m_pDB->m_Type_Telefoon_fax_1 = m_nTypeTel1 ; // 0 = geen, 1 = telefoon prive,
m_pDB->m_Type_Telefoon_fax_2 = m_nTypeTel2 ; // 2 = telefoon werk, 3 = GSM prive,
m_pDB->m_Type_Telefoon_fax_3 = m_nTypeTel3 ; // 4 = GSM werk, 5 = fax prive,
m_pDB->m_Type_Telefoon_fax_4 = m_nTypeTel4 ; // 6 = fax werk
m_pDB->m_Telefoon_fax_1 = m_strTel1 ;
m_pDB->m_Telefoon_fax_2 = m_strTel2 ;
m_pDB->m_Telefoon_fax_3 = m_strTel3 ;
m_pDB->m_Telefoon_fax_4 = m_strTel4 ;
m_pDB->m_Type_Email_1 = m_nTypeEmail1 ; // 0 = geen, 1 = Email prive 1,
m_pDB->m_Type_Email_2 = m_nTypeEmail2 ; // 2 = Email prive 2,
m_pDB->m_Type_Email_3 = m_nTypeEmail3 ; // 3 = Email prive 3,
m_pDB->m_Type_Email_4 = m_nTypeEmail4 ; // 4 = Email werk
m_pDB->m_Email_1 = m_strEmail1 ;
m_pDB->m_Email_2 = m_strEmail2 ;
m_pDB->m_Email_3 = m_strEmail3 ;
m_pDB->m_Email_4 = m_strEmail4 ;
m_pDB->m_Type_Website_1 = m_nTypeWebsite1 ; // 0 = geen, 1 = website prive,
m_pDB->m_Type_Website_2 = m_nTypeWebsite2 ; // 2 = website werk
m_pDB->m_Website_1 = m_strWebsite1 ;
m_pDB->m_Website_2 = m_strWebsite2 ;
m_pDB->m_Type_Online_chat_1 = m_nTypeChat1 ; // 0 = geen, 1 = MSN messenger,
m_pDB->m_Type_Online_chat_2 = m_nTypeChat2 ; // 2 = ICQ, 3 = AOL
m_pDB->m_Online_chat_1 = m_strChat1 ;
m_pDB->m_Online_chat_2 = m_strChat2 ;
m_pDB->m_Nota = m_strNota ;
m_pDB->Update() ;
}
else
{
MessageBox(NULL, "Kan record niet opslaan!", "Fout!", MB_OK) ;
}
return S_OK ;
}
-
Re: MFC : searching a database for the second time
I also uploaded all the source to my website :
http:/:users.pandora.be/da_cobra/addressbook.zip
Although you won't be able to run the code, because it's an ODBC database and the access file isn't included
pls share your thoughts
-
Re: MFC : searching a database for the second time
I couldn't rearrange that URL in any way that it worked.
-
Re: MFC : searching a database for the second time
I think da_cobra meant something like this :
da_cobra's app
Regards
John
-
Re: MFC : searching a database for the second time
yeah there was a typo in that post
the right link is :
http://users.pandora.be/da_cobra/addressbook.zip
-
Re: MFC : searching a database for the second time
m_strFilter = "name = 'a%'" doesn't work
try
m_strFilter = "name LIKE 'a%'"