mfc crecordset

Posted on 1997-12-29
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
Question by:nmangla
1 Comment

Accepted Solution

jpk041897 earned 50 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
#include "lbox1ctrl.h"

class COps : public CDialog
// Construction
      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;
      CClientesRecordSet *ClientesRS;
      COpsRecordSet *OpsRS;
      CInstsRecordSet *InstRS;
      CInstsRecordSet *InstUnSortRS;
      CString Buffer;
      void Limpiar();
      BOOL Edit;
      CSaldosRecordSet *SaldosRS;

// Dialog Data
      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;

// Overrides
      // ClassWizard generated virtual function overrides
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
      // Generated message map functions
      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();
      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__;

// COps dialog

COps::COps(CWnd* pParent /*=NULL*/)
      : CDialog(COps::IDD, pParent)
      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("");

void COps::DoDataExchange(CDataExchange* pDX)
      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);

      ON_LBN_SELCHANGE(IDC_LISTINSTRUMENTO, OnSelchangeListinstrumento)
      ON_LBN_SELCHANGE(IDC_LISTOPERACIONES, OnSelchangeListoperaciones)
      ON_EN_SETFOCUS(IDC_UNIDADES, OnSetfocusUnidades)
      ON_LBN_SETFOCUS(IDC_LISTINSTRUMENTO, OnSetfocusListinstrumento)

[sniped code]

BOOL COps::OnInitDialog()
    m_dbInversim.Open( _T( "Inversim" ), FALSE,
               FALSE, _T( "ODBC;UID=admin;PWD=ncc1701d"));

      if (m_dbInversim.IsOpen() == FALSE){
            AfxMessageBox( "ERROR!\nNo se pudo abrir la base de datos Inversim!");
            return FALSE;

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

      if (m_dbDatos.IsOpen() == FALSE){
            AfxMessageBox( "ERROR!\nNo se pudo abrir la base de datos Datos 32!");
            return FALSE;


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

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

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

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


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

            catch (CDBException *pEx ){
      InstRS->m_strSort = "Nombre";


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

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

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


            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 {
            catch (CDBException *pEx ){

            Line = "";

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

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

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

      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 and I will gladly e-mail you the complete code  for this dialog.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

821 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