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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 181
C++ standard library based binary archive format 6 98
Safe conversion? 4 67
Issues with C++ Class 19 92
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

786 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