DAO ComboBox

If you are working with relational databases you will often encounter the following problem:
You’ve got a source table (for example a list of customers) and a target table (for example a bill).
The target table contains an ID of the source table and you want to have a ComboBox to display the customer name but it should return the customers ID to the the dialog or form it is nested in.
To reach this you often have to write a lot of code.

The CDaoComboBox class covers this problem. It should work with every CDaoRecordset as datasource and every dialog or form. And the best: you just need one or two lines of code to use it !

All you have to do is:

1) Add the CDaoComboBox class to your project.

2) Add a ComboBox control to your dialog or form.

3) Associate the control with CDaoComboBox via class wizard.

4) Initialize the control in the OnInitDialog() method of your dialog or the OnInitialUpdate() method of your form view (only one line of code ! See example below) with a pointer to the source recordset, the display field name and the return field name.

5) If the data type of the variable to store the result code is CString you can associate the control to this variable via class wizard. If it is not (in the example below it’s a long value) you need to manually add a DDX funtion to the DoDataExchange() method of your dialog or form view (again only one line of code – the example below).

If you need to update the data displayed in the ComboBox (for example after the contents of the source table have changed) call CDaoComboBox::Fill()


The only method of the CDaoComboBox class you really need is Init(). It has the following syntax:


void Init(CDaoRecordset * pSet, CString strDispField, CString strReturnField, bool bIsRecordsetConstant = true)

Parameters:

pSet:

pointer to the source CDaoRecordset containing the display and return fields.

strDispField:

name of the field to display to the user.

strReturnField:

name of the field whichs value to return to the dialog or form.

bIsRecordsetConstant:

– true (default): the source recordset is queried only once and the content of the ComboBox remains constant for its lifetime.

– false: the source recordset is requeried every time the return value is set by the dialog or form (every time when a WM_SETTEXT message is sent to the ComboBox).

Remarks:

– If pSet is already opened, you have to close it manually. If it is not already opened, it will be automatically closed and it’s memory will be deallocated when the ComboBox is destroyed.

– You can set the m_strFilter or m_strSort member variables of pSet if you need to.

—-

Example for using:


[ConTestView.h]
#include “DaoComboBox.h” // include the class header in your dialog or form view header
[…]
CDaoComboBox m_Combo; // generated by class wizard

[ConTestView.cpp]
#include “QuellenSet.h” // CQuellen is a DaoRecordset containing the fields “ID” and “Name”
[…]

void CConTestView::DoDataExchange(CDataExchange* pDX)
{
CDaoRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConTestView)
DDX_Control(pDX, IDC_COMBO, m_Combo);
DDX_Control(pDX, IDC_VEKTOR, m_Vektor);
DDX_Control(pDX, IDC_Vorname, m_Vorname);
DDX_Control(pDX, IDC_GebDatum, m_GebDatum);
DDX_Control(pDX, IDC_TELEFON, m_Telefon);
DDX_Control(pDX, IDC_BETRAG, m_Betrag);
DDX_FieldText(pDX, IDC_BETRAG, m_pSet->m_Kontostand, m_pSet);
DDX_FieldText(pDX, IDC_GebDatum, m_pSet->m_GebDatum, m_pSet);
DDX_FieldText(pDX, IDC_TELEFON, m_pSet->m_Telefon, m_pSet);
DDX_FieldText(pDX, IDC_Vorname, m_pSet->m_Vorname, m_pSet);
DDX_FieldText(pDX, IDC_VEKTOR, m_pSet->m_Vektor, m_pSet);
DDV_MinMaxDouble(pDX, m_pSet->m_Vektor, -9999999.99, 9999999.99);
//}}AFX_DATA_MAP
DDX_FieldText(pDX, IDC_COMBO, m_pSet->m_QuelleID, m_pSet);
// The return string of the ComboBox is associated with the variable “m_QuelleID” (data type: long) in the target recordset “m_pSet”
}

void CConTestView::OnInitialUpdate() {
m_Combo.Init( new CQuellenSet, “Name”, “ID”);
// The combobox is associated with DaoRecordset “CQuellenSet”,
// the field values of the field “Name” are displayed,
// the field value of the field “ID” is returned to “m_pSet->m_QuelleID”

// […]
m_pSet = &GetDocument()->m_conTestSet;
CDaoRecordView::OnInitialUpdate();
}
[…]

That’s all ! No code in the UpdateData() method or somewhere else is needed !!!

Downloads

Download demo project – 40 Kb

Download source – 3 Kb

More by Author

Get the Free Newsletter!

Subscribe to Data Insider for top news, trends & analysis

Must Read