Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

mfc crecordset

Posted on 1997-12-29
1
Medium Priority
?
685 Views
Last Modified: 2012-06-27
i have 4 tables and the first recordset i made was with appwizard. The second one i am using is with CDialog with another crecordset class
in the first recordview if i want to update data of the second recordset what do i do? if i make an object of the
second recordset class in the view and use eg. second.Movelast() it says debug assertion failure.
Any help will be appreciated
0
Comment
Question by:nmangla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
jpk041897 earned 150 total points
ID: 1177235
For the CRecordSet's to work you need to create them in the heap (Don't ask me why). Following is some relevant code on a project I am currently writting that uses 5 record sets on a single (non wizard generated) dialog:

// Ops.h : header file
//

#include "ClientesRecordSet.h"
#include "OpsRecordSet.h"
#include "TabListBox.h"
#include "InstsRecordSet.h"
#include "SaldosRecordSet.h"
#include "PortafoliosRecordSet.h"


/////////////////////////////////////////////////////////////////////////////
// COps dialog
//{{AFX_INCLUDES()
#include "lbox1ctrl.h"
//}}AFX_INCLUDES

class COps : public CDialog
{
// Construction
public:
      COps(CWnd* pParent = NULL);   // standard constructor
      CDatabase m_dbInversim;
      CDatabase m_dbDatos;
      BOOL Create(CWnd *pParent = NULL);
      CDatabase* GetDatabase();
      CDatabase *GetDatosDatabase();
      void Ejecutar();
      PortafoliosRecordSet *PortafolioRS;
protected:
      CClientesRecordSet *ClientesRS;
      COpsRecordSet *OpsRS;
      CInstsRecordSet *InstRS;
      CInstsRecordSet *InstUnSortRS;
      CString Buffer;
      void Limpiar();
      BOOL Edit;
      CSaldosRecordSet *SaldosRS;


// Dialog Data
      //{{AFX_DATA(COps)
      enum { IDD = IDD_OPS };
      CButton      m_btnNuevo;
      CButton      m_btnGuardar;
      CEdit      m_EditUnidades;
      CButton      m_btnEditar;
      CButton      m_btnBorrar;
      CTabListBox      m_ListOperaciones;
      CTabListBox      m_Operaciones;
      CButton      m_CompraVenta;
      CButton      m_Inst;
      CButton      m_Tipo;
      CString      m_Fecha;
      CString      m_Paterno;
      CString      m_Clave;
      CString      m_Materno;
      CString      m_Nombres;
      CString      m_RFC;
      CString      m_Instrumento;
      long      m_Unidades;
      float      m_Saldo;
      CString      m_valCompra;
      CString      m_valVenta;
      CString      m_Valor;
      int            m_Buy;
      int            m_paper;
      CString      m_Registrado;
      //}}AFX_DATA


// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(COps)
      public:
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      // Generated message map functions
      //{{AFX_MSG(COps)
      afx_msg void OnBuscar();
      afx_msg void OnFirst();
      afx_msg void OnPrev();
      afx_msg void OnNext();
      afx_msg void OnLast();
      afx_msg void OnGuardar();
      afx_msg void OnNuevo();
      afx_msg void OnBorrar();
      afx_msg void OnEditar();
      afx_msg void OnCompraventa();
      afx_msg void OnTipo();
      virtual BOOL OnInitDialog();
      virtual void OnOK();
      afx_msg void OnClose();
      afx_msg void OnLimpiar1();
      afx_msg void OnSelchangeListinstrumento();
      afx_msg void OnCompra();
      afx_msg void OnInstrumentos();
      afx_msg void OnVenta();
      afx_msg void OnPesos();
      afx_msg void OnSelchangeListoperaciones();
      afx_msg void OnSetfocusUnidades();
      afx_msg void OnSetfocusListinstrumento();
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
      void ReadOps();
};



// Ops.cpp : implementation file
//

#include "stdafx.h"
#include "inversim.h"
#include "Ops.h"
#include "stdio.h"
#include "memory.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// COps dialog


COps::COps(CWnd* pParent /*=NULL*/)
      : CDialog(COps::IDD, pParent)
{
      //{{AFX_DATA_INIT(COps)
      m_Fecha = _T("");
      m_Paterno = _T("");
      m_Clave = _T("");
      m_Materno = _T("");
      m_Nombres = _T("");
      m_RFC = _T("");
      m_Instrumento = _T("");
      m_Unidades = 0;
      m_Saldo = 0.0f;
      m_valCompra = _T("");
      m_valVenta = _T("");
      m_Valor = _T("");
      m_Buy = -1;
      m_paper = -1;
      m_Registrado = _T("");
      //}}AFX_DATA_INIT
}


void COps::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(COps)
      DDX_Control(pDX, IDC_NUEVO, m_btnNuevo);
      DDX_Control(pDX, IDC_GUARDAR, m_btnGuardar);
      DDX_Control(pDX, IDC_UNIDADES, m_EditUnidades);
      DDX_Control(pDX, IDC_EDITAR, m_btnEditar);
      DDX_Control(pDX, IDC_BORRAR, m_btnBorrar);
      DDX_Control(pDX, IDC_LISTINSTRUMENTO, m_ListOperaciones);
      DDX_Control(pDX, IDC_LISTOPERACIONES, m_Operaciones);
      DDX_Control(pDX, IDC_COMPRA, m_CompraVenta);
      DDX_Control(pDX, IDC_INSTRUMENTOS, m_Inst);
      DDX_Control(pDX, IDC_TIPO, m_Tipo);
      DDX_Text(pDX, IDC_FECHA, m_Fecha);
      DDX_Text(pDX, IDC_APEPATERNO, m_Paterno);
      DDX_Text(pDX, IDC_CLIENTE, m_Clave);
      DDV_MaxChars(pDX, m_Clave, 9);
      DDX_Text(pDX, IDC_APEMATERNO, m_Materno);
      DDX_Text(pDX, IDC_NOMBRE, m_Nombres);
      DDX_Text(pDX, IDC_RFC, m_RFC);
      DDX_LBString(pDX, IDC_LISTINSTRUMENTO, m_Instrumento);
      DDX_Text(pDX, IDC_UNIDADES, m_Unidades);
      DDX_Text(pDX, IDC_SALDO, m_Saldo);
      DDX_Text(pDX, IDC_ValCompra, m_valCompra);
      DDX_Text(pDX, IDC_VALVENTA, m_valVenta);
      DDX_Text(pDX, IDC_VALOR, m_Valor);
      DDX_Radio(pDX, IDC_COMPRA, m_Buy);
      DDX_Radio(pDX, IDC_INSTRUMENTOS, m_paper);
      DDX_Text(pDX, IDC_REGISTRADO, m_Registrado);
      //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COps, CDialog)
      //{{AFX_MSG_MAP(COps)
      ON_BN_CLICKED(IDC_BUSCAR, OnBuscar)
      ON_BN_CLICKED(IDC_FIRST, OnFirst)
      ON_BN_CLICKED(IDC_PREV, OnPrev)
      ON_BN_CLICKED(IDC_NEXT, OnNext)
      ON_BN_CLICKED(IDC_LAST, OnLast)
      ON_BN_CLICKED(IDC_GUARDAR, OnGuardar)
      ON_BN_CLICKED(IDC_NUEVO, OnNuevo)
      ON_BN_CLICKED(IDC_BORRAR, OnBorrar)
      ON_BN_CLICKED(IDC_EDITAR, OnEditar)
      ON_BN_CLICKED(IDC_COMPRAVENTA, OnCompraventa)
      ON_BN_CLICKED(IDC_TIPO, OnTipo)
      ON_WM_CLOSE()
      ON_BN_CLICKED(IDC_LIMPIAR1, OnLimpiar1)
      ON_LBN_SELCHANGE(IDC_LISTINSTRUMENTO, OnSelchangeListinstrumento)
      ON_BN_CLICKED(IDC_COMPRA, OnCompra)
      ON_BN_CLICKED(IDC_INSTRUMENTOS, OnInstrumentos)
      ON_BN_CLICKED(IDC_VENTA, OnVenta)
      ON_BN_CLICKED(IDC_PESOS, OnPesos)
      ON_LBN_SELCHANGE(IDC_LISTOPERACIONES, OnSelchangeListoperaciones)
      ON_EN_SETFOCUS(IDC_UNIDADES, OnSetfocusUnidades)
      ON_LBN_SETFOCUS(IDC_LISTINSTRUMENTO, OnSetfocusListinstrumento)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

[sniped code]

BOOL COps::OnInitDialog()
{
      AfxGetApp()->LoadStandardCursor(IDC_WAIT);
      CDialog::OnInitDialog();
      
    m_dbInversim.Open( _T( "Inversim" ), FALSE,
               FALSE, _T( "ODBC;UID=admin;PWD=ncc1701d"));

      ASSERT(m_dbInversim.IsOpen());
      if (m_dbInversim.IsOpen() == FALSE){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox( "ERROR!\nNo se pudo abrir la base de datos Inversim!");
            return FALSE;
      }

    m_dbDatos.Open( _T( "Datos" ));

      ASSERT(m_dbDatos.IsOpen());
      if (m_dbDatos.IsOpen() == FALSE){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox( "ERROR!\nNo se pudo abrir la base de datos Datos 32!");
            return FALSE;
      }


      AfxGetApp()->LoadStandardCursor(IDC_ARROW);
      AfxGetApp()->LoadStandardCursor(IDC_WAIT);

      ClientesRS = new CClientesRecordSet(GetDatabase());
      
      ClientesRS->Open(CRecordset::snapshot, NULL, CRecordset::readOnly);

      ASSERT(ClientesRS->IsOpen());
      if (ClientesRS->IsOpen() == FALSE){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox( "ERROR!\nNo se pudo abrir el Querry de Clientes!");
            return FALSE;
      }

      OpsRS = new COpsRecordSet(GetDatabase());
      
      OpsRS->Open(CRecordset::snapshot, NULL);

      ASSERT(OpsRS->IsOpen());
      if (OpsRS->IsOpen() == FALSE){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox( "ERROR!\nNo se pudo abrir el Querry de Operaciones!");
            return FALSE;
      }


      AfxGetApp()->LoadStandardCursor(IDC_ARROW);
      AfxGetApp()->LoadStandardCursor(IDC_WAIT);

      try {
            InstRS = new CInstsRecordSet(GetDatabase());
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }
      InstRS->m_strSort = "Nombre";

      try{
            InstRS->Open(CRecordset::snapshot);
      }
            catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }
      InstRS->m_strSort = "Nombre";

      

      ASSERT(InstRS->IsOpen());
      if (InstRS->IsOpen() == FALSE){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox( "ERROR!\nNo se pudo abrir el Querry de Operaciones!");
            return FALSE;
      }

      InstUnSortRS = new CInstsRecordSet(GetDatabase());
      
      InstUnSortRS->Open(CRecordset::snapshot, NULL, CRecordset::readOnly);

      ASSERT(InstUnSortRS->IsOpen());
      if (InstUnSortRS->IsOpen() == FALSE){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox( "ERROR!\nNo se pudo abrir el Querry de Operaciones!");
            return FALSE;
      }


      AfxGetApp()->LoadStandardCursor(IDC_ARROW);
      AfxGetApp()->LoadStandardCursor(IDC_WAIT);

      do{
            CString Line;
            char dato[20];
            Line += InstRS->m_Nombre + '\t';
            sprintf(dato, "%ld", InstRS->m_InstrumentoID);
            CString d(dato);
            Line += d;

            m_ListOperaciones.AddString(LPCTSTR (Line) );

            try {
                  InstRS->MoveNext();
            }
            catch (CDBException *pEx ){
                  AfxMessageBox(pEx->m_strError);
                  pEx->Delete();
            }

            Line = "";

      } while (!InstRS->IsEOF());

      SaldosRS = new CSaldosRecordSet(GetDatabase());
      
      SaldosRS->Open(CRecordset::snapshot, NULL, CRecordset::none);
            
      ASSERT(SaldosRS->IsOpen());
      if (SaldosRS->IsOpen() == FALSE){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox( "ERROR!\nNo se pudo abrir el Querry de Saldos!");
            
            return FALSE;
      }

      PortafolioRS = new PortafoliosRecordSet(GetDatabase());
      
      PortafolioRS->Open(CRecordset::snapshot, NULL);
            
      ASSERT(PortafolioRS->IsOpen());
      if (PortafolioRS->IsOpen() == FALSE){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox( "ERROR!\nNo se pudo abrir el Querry de Portafolio!");
            
            return FALSE;
      }



      AfxGetApp()->LoadStandardCursor(IDC_ARROW);
      
      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}
[snipped code]

// Initialize when needed
CDatabase* COps::GetDatabase()
{
   // Connect the object to a data source
   if( !m_dbInversim.IsOpen( ) && 
       !m_dbInversim.OpenEx( NULL ) )
      return NULL;

   return &m_dbInversim;
}

CDatabase* COps::GetDatosDatabase()
{
   // Connect the object to a data source
   if( !m_dbDatos.IsOpen( ) && 
       !m_dbDatos.OpenEx( NULL ) )
      return NULL;

   return &m_dbDatos;
}



If you need the complete souce for these files, drop me note at jkelleghan@usa.net and I will gladly e-mail you the complete code  for this dialog.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question