Thread: Accessing a database from a dialog.

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

    Accessing a database from a dialog.

    In an MDI created with MFC, with database support, the form is already given access to the database, and it's valus can be accessed through variable names (for example m_Name gives access to the Name field in the current record) How do I add this functionality to a dialog that is not part of my form?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    you can do this one of two ways....

    the first is of course.... and it is also the recommend way

    MyDialog dlg;


    dlg.m_strFName = m_pSet->m_First_Name;
    dlg.m_strLName = m_pSet->m_Last_Name;

    int iResult = dlg.DoModal();

    if( iResult == IDOK )
    {
    if( dlg.m_strFName != m_pSet->m_FirstName;
    ....write some code to edit the record set... or add new..

    }



    the other way is to map.... the variables directly to the dialog

    because the record set is being created in your form or in your doc somewhere... you must not create a new one in your dialog, instead create a member variable pointer to the recordset like this...

    class MyDialog: publice CDialog
    {
    ...
    public:
    CMyRecordSet* m_pSet;

    //you can also assign the record set in the constructor
    void SetRecordSet( CMyRecordSet* pSet){ m_pSet = pSet; }
    }


    then go to the MFC class wizard the Class Info tab select the proper dialog class int the CLASS NAME drop box... in the foreign class select the derived recordset class... and assign the variable a name like m_pSet... this action will aumatically add the m_pSet variable to your dialog and set it to NULL upon construction....

    now whenever you call call MyDialog.... you can update the recordset... through the dialog ..



    //assuming that the recordset is open for and is editable

    ASSERT( m_pSet->IsOpen() == TRUE )
    ASSERT( m_pSet->GetEditMode() != dbEditNone )

    CMyDialog dlg( m_pSet ); or CMyDialog dlg; dlg.SetRecordSet( m_pSet );

    dlg.DoModal();


    this will allow you to map variables directly to the edit and combo controls of your dialog....
    This can also create some nasty problems if the recordset is NULL or Closed so make sure it is Open prior to calling dlg.DoModal...
    zMan

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    wow. Thanks a lot zMan I'll try that now

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    Perfect! I'm using the first method you suggested, but how do I implement that method into the propertysheets of my form. It's the method you gave me a while ago:

    Code:
    void  CTykeView::CreateTabControl()
    {
    
    
    pSheet = new CPropertySheet( "My Tab", this, 0); 
    
    pSheet->AddPage( &m_Page1 ); 
    pSheet->AddPage( &m_Page2 ); 
    pSheet->AddPage( &m_Page3 ); 
    
    
    pSheet->Create( this, WS_CHILD | WS_VISIBLE ); 
    
    CRect r; 
    m_staticPlaceHolder.GetWindowRect( &r ); 
    ScreenToClient( &r ); 
    
    pSheet->SetWindowPos( pSheet, r.top, r.left, 0, 0, SWP_NOSIZE | SWP_NOZORDER ); 
    
    
    return;
    }
    I've tried initialising the string with m_pSet->m_Name ( m_str = _T(m_pSet->m_Name); ) That compiles but doesnt run

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    property sheets with database access

    Code:
    //** prior to calling this function insure to create the recordset and open it
    //** CDaoDatabase db;
    //** db.Open( strFullPathAndFileName );
    //** m_pSet = new CMyRecordset( &db )
    //** m_pSet->Open();
    void  CTykeView::CreateTabControl()
    {
    
    
    
    
    pSheet = new CPropertySheet( "My Tab", this, 0); 
    
    pSheet->AddPage( &m_Page1 ); 
    pSheet->AddPage( &m_Page2 ); 
    pSheet->AddPage( &m_Page3 ); 
    
    
    //***********************************
    //** do this prior to creation
    //** If you have mapped the RS to the pages
    m_page1.SetRecordSetPointer( &m_pSet );
    m_page2.SetRecordSetPointer( &m_pSet );
    m_page3.SetRecordSetPointer( &m_pSet );
    
    /*
            where the function looks like this
            for each of the property pages
            CMyProPage1::SetRecordSetPointer(CMyRecordset* pSet)
            {
                    m_pSet = pSet;
            }
    
    */
    
    pSheet->Create( this, WS_CHILD | WS_VISIBLE ); 
    
    CRect r; 
    m_staticPlaceHolder.GetWindowRect( &r ); 
    ScreenToClient( &r ); 
    
    pSheet->SetWindowPos( pSheet, r.top, r.left, 0, 0, SWP_NOSIZE | SWP_NOZORDER ); 
    
    
    return;
    }
    
    
    to access the pages remember to call pSheet->SetActivePage( n )....otherwise you will get some nasty assertion errors trying to access a non active page... by default the first page is active.. 
    
    and don't forget that prior to editing a recordset you must set edit in the following steps
    
    1. Move to the record
    2. call m_pSet->Edit()
    3. update the data through the dialog
    4. call m_pSet->Update()
    5. move to another record or close it...
    
    or
    1. m_pSet->AddNew()
    2. Update the data m_pSet->m_Name = strName;...etc
    3. m_pSet->AddNew()
    zMan

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    oops on CDaoRecordset::Update()

    correction on that

    m_pSet->AddNew();
    ....assign values
    m_pSet->Update();
    zMan

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

    More problems :(

    I've added all the code that you have given me, and I'm only dealing with the first property sheet at the moment until I can get it working. It compiles without errors or asserts, but how do I get the edit box to actually show m_Name? I've tried
    proppage1.m_str = m_pSet->m_Name;, before and after creation in TykeView.cpp, and
    m_str = _T(m_pSet->m_Name); in Page1.cpp
    I've even tried calling m_pSet->Update(); before and after creation.
    All compile without errors, except m_str = _T(m_pSet->m_Name); which gives an assert error at run time. Also if it matters I'm not using DAO, I'm using ODBC, and the recordset is definately already open.

    The one problem I did run in to while putting the code you have given me into my app was with SetRecordSetPointer(CMyRecordset* pSet). I got this error at first:

    'SetRecordSetPointer' : cannot convert parameter 1 from 'class CTykeSet ** ' to 'class CTykeSet *'. Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    I solved this (I think) by using
    m_page1.SetRecordSetPointer( m_pSet );
    instead of
    m_page1.SetRecordSetPointer( &m_pSet );
    is this right?

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    property shee

    I somehow created a new thread instead of replying look for the new thread....
    I sent you a link to a demo I built...
    www.vvm.com/~fzamora/DaoDemo.zip

    I hope this helps
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing an SQL database through C++
    By abachler in forum Windows Programming
    Replies: 11
    Last Post: 11-20-2008, 11:41 PM
  2. accessing a database made in Access..
    By willc0de4food in forum Windows Programming
    Replies: 4
    Last Post: 10-10-2005, 07:40 PM
  3. make Child Dialog not Popup?
    By Zeusbwr in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2005, 02:42 PM
  4. Getting the position of a dialog without parent on the screen
    By stormbringer in forum Windows Programming
    Replies: 1
    Last Post: 08-27-2003, 02:59 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM