Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying contents of database in a combobox of a dialog box

Status
Not open for further replies.

Larree

Programmer
Dec 31, 2002
20
SG
Hi,
I am a relatively new MFC programmer and I have the following problem. In adding a second recordset to my dialog box, I want to add a combo box to list the names of all that is contained in my database. I used the following code

m_pSet = &GetDocument()->m_viewrecordSet;
m_pSet->m_pDatabase = pDoc->m_viewrecordSet.m_pDatabase;
CDialog::OnInitDialog();

// Fill the combo box with all of the names
ViewRecordsDlg* pDoc = GetDocument();
pDoc->m_viewrecordSet.m_strSort = "Name";
if (!pDoc->m_viewrecordSet.Open())
return;

// Filter, parameterize and sort the CViewRecordSet recordset
m_pSet->m_strFilter = "Name = ?";
m_pSet->m_strNameParam = pDoc->m_viewrecordSet.m_Name;



When I compiled, the following errors were encountered....

error C2065: 'GetDocument' : undeclared identifier
error C2227: left of '->m_viewrecordSet' must point to class/struct/union
error C2440: 'initializing' : cannot convert from 'int' to 'class ViewRecordsDlg *'
error C2561: 'OnInitDialog' : function must return a value

The error lies within these portion of my code but I do not know how to go about solving them. Pleae advice....Thanks!!!
 
Try:

[tt]CYourDocumentClassName* pDoc = GetDocument();
m_pSet = pDoc->m_viewrecordSet;
m_pSet->m_pDatabase = pDoc->m_viewrecordSet.m_pDatabase;
return CDialog::OnInitDialog();[/tt]

and this looks wrong also

[tt]ViewRecordsDlg* pDoc = GetDocument();
[/tt]

if your need a pointer to the dialog and you are calling this from within the actual dialog class then simply use the [tt]this[/tt] pointer. Otherwise, GetDocument() does not return a dialog pointer!

This is kind of confusing because I don't know in what context you are calling these snippets. If it is a dialog based app or your are calling all these from within the dialog's class functions then you do not need to be using GetDocument() at all - if that's the case then you do not need a pointer to the dialog or even to use the [tt]this[/tt] pointer because [tt]m_viewrecordSet[/tt] should already be a member of the class!

If it's not a member of the dialog's class then you can use [tt]GetDlgItem()[/tt] and pass in the resource ID as the parameter (don't forget to 'typecast' the return value from [tt]CWnd[/tt] to the appropriate control type!
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Hi,
I think I am calling from within the dialog class so I do away with the GetDocument().My code now reads

BOOL ViewRecordsDlg::OnInitDialog()
{

// Fill the combo box with all of the names
pDoc->m_viewrecordSet.m_strSort = "Name";
if (!pDoc->m_viewrecordSet.Open())
return CDialog::OnInitDialog();


// Filter, parameterize and sort the
//CViewRecordSet recordset
m_pSet->m_strFilter = "Name = ?";
m_pSet->m_strNameParam = pDoc->m_viewrecordSet.m_Name;


m_ctlNameList.ResetContent();
if (pDoc->m_viewrecordSet.IsOpen())
{
while (!pDoc->m_viewrecordSet.IsEOF())
{
m_ctlNameList.AddString(
pDoc->m_viewrecordSet.m_Name);
pDoc->m_viewrecordSet.MoveNext();
}
}
m_ctlNameList.SetCurSel(0);


return TRUE; // return TRUE unless you set the focus to
// a control
// EXCEPTION: OCX Property Pages
//should return FALSE
}

When I compile, there is no error. But when I try to open the dialog, an error reads "The instruction at "0x5f377136" referenced memory at "0xcccccd3c". The memory could not be "read" Please advice....
Also to connect to a recordset for single document, the code
m_pSet = &GetDocument()->m_viewrecordSet;
is used in the OnInitialUpdate(). But for dialog class, the
GetDocument() function is not used. So how do I get the recordset to be opened in the OnInitDialog() function? Thanks!!
 
Where is the [tt]pDoc[/tt] coming from in the above code??

You can give your dialog access to an 'outside' object (ie. an object of some other class) very easily in a couple of ways. The easiest and fastest way is to add a public member to the dialog class which is of type [tt]T*[/tt] where 'T' is the type of object. When you instantiate your dialog but before you display it, set the value of [tt]T*[/tt] to the address of the original object. Your dialog class will now have access to that object!
Here's a simple example using a CString where the original CString is in the document class - the document class calls and displays the dialog:[tt]

class CMyDocument
{
... usual members here

CString m_MyString;
};

class CMyDialog
{
... usual members go here

CString* pStrPointer;
};


// in the document code we want to create
// and display the dialog but the dialog needs access
// to CMyDocument::m_MyString

m_MyString = "Hello World";

// now create the dialog
CMyDialog* dlg = new CMyDialog;

dlg->pStrPointer = &m_MyString;

dlg->DoModal(); // display dialog


[/tt]In this simple example, the CMyDialog class now has access to the original m_MyString object (which belongs to CMyDocument) through the pointer!

Does this make sense??
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top