Thread: Combo Box / DAO / PropertyPage troubles

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121

    Combo Box / DAO / PropertyPage troubles

    Hi, I'm trying to list all the data from a field in an mdb database in a combo box, on a property page. I'm using MFC and I have some code to populate a combo box from a database on the main form. Where do I put most of the code, as a CPropertyPage doesnt have an OnInitialUpdate() function, which is where all the examples I have seen are, do I put it immediately after the page is created on the sheet in the MyFormView.cpp file? Or in the MyPropertyPage.cpp file? The database is quite large so I would prefer to use a seperate recordset for each table.

    m_pCustomersSet is a pointer to my seperate Customers recordset

    CL1_2Customers is my PropertyPage
    m_CustomerList is the my CComboBox variable

    Code:
    UINT CL1_2Customers::PopulateCustomerList()
    {
    	UINT nCount = 0;
    
    	ASSERT( m_pCustomersSet->IsOpen() == TRUE);		
    
    	
    	if( !m_pCustomersSet->IsBOF() )// if customer table is not currently at the beginning of the file
    	{
    		m_CustomerList.ResetContent(); //reset the combo box
    		m_pCustomersSet->MoveFirst(); //Move to the first record
    
    		while( !m_pCustomersSet->IsEOF() ) //While the current record is not the last record
    		{
    			m_CustomerList.AddString( m_pCustomersSet->m_CCompany_Name ); //Add the Company name for the current record
    			m_pCustomersSet->MoveNext(); // Move to the next record
    			nCount++; //count the total number of records added
    		}
    	}
    
    	return nCount;
    }
    
    
    BOOL CL1_2Customers::OnSetActive() 
    {
    
    	CTykeDoc* pDoc = (CTykeDoc*)IsKindOf(RUNTIME_CLASS(CTykeDoc));
    
    	//** get pointers to the recordsets
    	m_pCustomersSet      = pDoc->m_pCustomersSet;
    
    
    	UINT nCustomerCount = PopulateCustomerList();
    	
    	if( nCustomerCount > 0 )
    	{
    		this->m_CustomerList.SetCurSel( 0 );
    	}
    	
    	return CPropertyPage::OnSetActive();
    }
    I have used OnSetActive in place of the normal OnInitialUpdate because as far as I can tell it is the closest equivalent in property pages.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    25

    property page

    CPropertyPage is derived from CDialog. Therefore you can handle the OnInitDialog and load the list box there.... You would not want to handle it onSetActive since this could result in multiple loads ...one for each time the page was selected....

    I hope that this helps.....

    I have a question though ... does your code work??

    I have never seen a pointer to the doc retrieved that way....

    Usually it is

    Code:
    CMyDoc* pdoc = GetDocument();
    
    where get document returns
    
    
    CMyDoc* CMyView::GetDocument()
    {	
             ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS CMyDoc)));
    	return (CMyDoc*)m_pDocument;
    }
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    I've moved the CTykeDoc* pDoc = GetDocument(); call to OnInitDialog (thanks for the help with that), but I still get an assertion error at run time. If I call GetDocument from a CView derived class it works ok, so it seems I cant use GetDocument in a CDialog derived class.

    What would be the best way to populate a combo box on a property page with DAO data?

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    25
    it depends on when it needs it...

    If you only need to load it once when the page is created...
    do it by Handling the OnInitDialog for the property page....
    other wise say for instance it changes each time that you select the page say because the data requied in the combo may have changed, do it by handling the OnSetActivePage...

    You can also just create your very member function and call it from the view like this....

    void MyProPage::LoadCombo( CMyRecordset* pset )
    {
    ....load the combo provided that you have a pointer to the set..

    }



    void CMyView::OnInitialUpdate()
    {

    //assuming that the sheet has been created
    //and the pages have been added....
    //where n is the index of the page
    //determined by the order in which it was added to the page
    m_pSheet->SetActivePage( n );
    m_pSheet->GetPage( n )->LoadCombo(m_pSet);

    }
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    InZane: I'm getting the error 'LoadCombo': is not a member of CPropertyPage, although I have created LoadCombo as a member function of CMyPropPage.

    Also what is the difference between
    pSheet->GetPage(1)->LoadCombo(m_pSet);
    -and-
    pSheet->SetActivePage(1);
    CPropPage2 proppage2;
    proppage2.LoadCombo(m_pSet);

    If I use the latter method I can get it to compile, but I still get lots of assertion errors for almost everything I try to do with m_pMyRecordSet.

    Code:
    void CL1_2Customers::LoadCombo(CDBCustomers* pset)
    {
    	m_pCustomersSet = pset;
    	UINT nCount = 0;
    	ASSERT( m_pCustomersSet->IsOpen() == TRUE);
    		
    
    
    		if( !m_pCustomersSet->IsBOF() )
    		{
    			m_CustomerList.ResetContent(); //ASSERT HERE
    			
    			//m_pCustomersSet->MoveFirst();
    
    			while( !m_pCustomersSet->IsEOF() )
    			{
    				m_CustomerList.AddString( m_pCustomersSet->m_CCompany_Name ); //AND HERE
    				//m_pCustomersSet->MoveNext();
    				nCount++;
    			}
    		}
    
    	
    
    	if( nCount > 0 )
    	{
    	m_CustomerList.SetCurSel( 1 );
    	}
    	return;
    }
    The debugger points to these lines for the asserts:

    _AFXWIN_INLINE void CComboBox::ResetContent()
    { ASSERT(::IsWindow(m_hWnd)); ::SendMessage(m_hWnd, CB_RESETCONTENT, 0, 0); }

    and

    _AFXWIN_INLINE int CComboBox::AddString(LPCTSTR lpszString)
    { ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, CB_ADDSTRING, 0, (LPARAM)lpszString); }

    I'm pulling my hair out over this, I've spent over 10 hours on populating a combo box and I've got a month to finish the whole thing, so any help is greatly appreciated.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    25
    ((CMyPage*)(pSheet->GetPage(1)))->LoadCombo(m_pSet);

    try casting it... or just use the page directly as you suggested
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

  7. #7
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    Thanks for the help, but I still need to get rid of the assert errors. I've added an update button to the property page, OnUpdate populates the combo box (to make sure the page is active and recordsets are open) and I'm now getting an error which tells me I have an illegal vtable pointer. Can anyone tell me what this means?

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    OK the vtable is the where the locations of your virtual functions are store. This table associates the the correct virtual function with its invoking object. Look somewhere to make sure that you are if you are overiding a virtual function that you call it's ancestors virtual function.... also make sure to use the keyword virtual function for any virtual functions that you may have overriden...
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 09:50 AM
  2. No data showing in combo box
    By PJYelton in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2005, 07:20 PM
  3. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM