Link to home
Start Free TrialLog in
Avatar of mitchell042997
mitchell042997

asked on

Updating Multiple Recordsets using Access '97 & ODBC Drivers

OK, here is my big problem.  I have two Recordsets open.  I can view them fine, scroll through them fine, delete fine.  In access, these two recordsets have a 1 to many relationship.  I can insert, but I can only enter values for the main data set, the one explicitly coded in the CView.  Here is the code:

void CTestView::OnDatabaseoptionsAddnewrecord()
{
      CAddHotelDialog dlg;
      if (dlg.DoModal()==IDOK) {            
            m_pSet->m_HotelID=CURRENT_PRIMARY_INDEX++;
            m_pSet->m_Name=dlg.m_HotelName;
            UpdateData(FALSE);
            m_pSet->AddNew();
            UpdateData(TRUE);            
            if (m_pSet->CanUpdate())
                  m_pSet->Update();


            CDateSet db_DateInfo(&GetDocument()->m_database);
            db_DateInfo.Open();
            db_DateInfo.m_Date=CTime(1999, 3, 19, 22, 15, 0);
            db_DateInfo.m_Status=TRUE;
            db_DateInfo.m_HotelID=CURRENT_PRIMARY_INDEX-1;
            db_DateInfo.m_DateID=20L;
            db_DateInfo.AddNew();
            
            m_pSet->Requery();
            if (!m_pSet->IsEOF())
                  m_pSet->MoveLast();
            
            LoadListBox();            
      }
}

This code creates another entry in teh table, but affects only the Hotel table, and doesn't update the Date table.  Any ideas on how to get this working?  Thanks!
Avatar of jpk041897
jpk041897

From a quick look at your code, I notice that you are calling db_DateInfo.AddNew() but you are not calling db_DateInfo.Update() afterwards. So the buffers will be full but not written to disk.

Based on the ammount of points you placed here though, I don't believe that this is the complete nature of your problem

I have some code that updates multiple RecordSets but is not based  on CRecordView, let me know if you feel that would help and I will post it.
Avatar of mitchell042997

ASKER

I had these two lines in there, but it still didn't work, so I took them out.

if (db_DateInfo.CanUpdate())
  db_DateInfo.Update();

These directly proceeded db_DateInfo.AddNew()

(Did I offer too many points, or too few?  That last comment has me curious)
ASKER CERTIFIED SOLUTION
Avatar of flaviosousa
flaviosousa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Assides from flaviosousa's answer, which is correct by the way, I have had a teerible time getting any multiple table ODBC program working while using instances of the CRecordSet created on the stack. I.e.: db_DateInfo.AddNew(), etc.

I have had no problem when creating the instance on the heap though, i.e.: db_DateInfo->AddNew()

I've never seem to have enough time to try to figure out why, but changinge instantiation from the stack to the heap seems to almost magicaly solve all kinds of problems.
I'll try out the code tonight and get back to you...  as far as the ODBC updating multiple records on the stack... do you think it would beehove me to just say screw it and go with DAO?  I know Access will work with DAO, but will MS SQL?  Thanks!!

(This is just a question, not worth any points, so if you don't want to answer it, understood.)
Although I somehow ended up with a rather embarasingly large ammount of points, I don't get paid for it. I answer the questions for only 2 reasons. 1) I have been exasperatedly stuck myself, and know what it feels like. 2) It actualy helps me relax.

So ask away...

Yes, DAO will work with MS-SQL, via an ODBC bridge (sic). SO thats not going to be much help, besides, the strange behaviour I indicated regarding stack/heap behaviour also repeats on DAO.

If you are going to make the code changes, the following program I wrote will probably help you in getting to run faster:

// 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()

/////////////////////////////////////////////////////////////////////////////
// COps message handlers

void COps::OnBuscar()
{
      UpdateData(TRUE);

      CString where = "UserID LIKE \'";

      if (m_Clave.IsEmpty()){
            AfxMessageBox("Debe capturar una clave de usuaro!");
            return;
      }

      where += m_Clave + "%\'";

      ClientesRS->m_strFilter = where;
      
      try {
            ClientesRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }
      
      if( ClientesRS->IsBOF()){
            AfxMessageBox(_T("No se encontrs el registro"));
            return;
      }

      try {
            ClientesRS->MoveFirst();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      m_Materno = ClientesRS->m_Materno;
      m_Paterno = ClientesRS->m_Paterno;
      m_Nombres = ClientesRS->m_Nombres;
      m_RFC =            ClientesRS->m_RFC;
      m_Clave =      ClientesRS->m_UserID;

      ReadOps();

      m_btnEditar.EnableWindow(TRUE);
      m_btnBorrar.EnableWindow(TRUE);
      m_btnNuevo.EnableWindow(TRUE);
      UpdateData(FALSE);
      
}

void COps::OnFirst()
{

      if( ClientesRS->IsBOF()){
            AfxMessageBox("La tabla esta Vacia");
            return;
      }

      try {
            ClientesRS->MoveFirst();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      m_Materno = ClientesRS->m_Materno;
      m_Paterno = ClientesRS->m_Paterno;
      m_Nombres = ClientesRS->m_Nombres;
      m_RFC =            ClientesRS->m_RFC;
      m_Clave =      ClientesRS->m_UserID;

      ReadOps();

      m_btnEditar.EnableWindow(TRUE);
      m_btnBorrar.EnableWindow(TRUE);
      m_btnNuevo.EnableWindow(TRUE);
      UpdateData(FALSE);
      
}

void COps::OnPrev()
{
      if( ClientesRS->IsBOF()){
            AfxMessageBox("Principio de la Tabla");
            return;
      }

      try {
            ClientesRS->MovePrev();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      m_Materno = ClientesRS->m_Materno;
      m_Paterno = ClientesRS->m_Paterno;
      m_Nombres = ClientesRS->m_Nombres;
      m_RFC =            ClientesRS->m_RFC;
      m_Clave =      ClientesRS->m_UserID;

      ReadOps();

      m_btnEditar.EnableWindow(TRUE);
      m_btnBorrar.EnableWindow(TRUE);
      m_btnNuevo.EnableWindow(TRUE);
      UpdateData(FALSE);
      
}

void COps::OnNext()
{
      if( ClientesRS->IsEOF()){
            AfxMessageBox("Final de la Tabla");
            return;
      }

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

      m_Materno = ClientesRS->m_Materno;
      m_Paterno = ClientesRS->m_Paterno;
      m_Nombres = ClientesRS->m_Nombres;
      m_RFC =            ClientesRS->m_RFC;
      m_Clave =      ClientesRS->m_UserID;

      ReadOps();

      m_btnEditar.EnableWindow(TRUE);
      m_btnBorrar.EnableWindow(TRUE);
      m_btnNuevo.EnableWindow(TRUE);
      UpdateData(FALSE);
      
}

void COps::OnLast()
{
      if( ClientesRS->IsEOF()){
            AfxMessageBox("La tabla esta Vacia");
            return;
      }

      try {
            ClientesRS->MoveLast();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      m_Materno = ClientesRS->m_Materno;
      m_Paterno = ClientesRS->m_Paterno;
      m_Nombres = ClientesRS->m_Nombres;
      m_RFC =            ClientesRS->m_RFC;
      m_Clave =      ClientesRS->m_UserID;

      ReadOps();

      m_btnEditar.EnableWindow(TRUE);
      m_btnBorrar.EnableWindow(TRUE);
      m_btnNuevo.EnableWindow(TRUE);
      UpdateData(FALSE);
      
}

void COps::Limpiar()
{
      m_Instrumento = "";
      m_Valor = "";
      m_valCompra = "";
      m_valVenta = "";
      m_Unidades = (long)0;
      m_Saldo = (float)0.0;
            
      UpdateData(FALSE);
}

void COps::OnGuardar()
{
      Ejecutar();
      UpdateData(TRUE);

      int d = atoi(m_Fecha.Left(m_Fecha.Find("/")));
      int j = m_Fecha.Find("/")+1;
      int k = m_Fecha.GetLength() - m_Fecha.ReverseFind('/');
      int m = atoi(m_Fecha.Mid(j,k));
      j = m_Fecha.GetLength() - m_Fecha.ReverseFind('/') - 1;
      int y = atoi(m_Fecha.Right(j))+1900;

      CTime t(y,m,d,0,0,0);
      if (!Edit)
            t = t.GetCurrentTime();


      OpsRS->m_Fecha = t;
      OpsRS->m_NumValores = m_Unidades;
      OpsRS->m_Baja = FALSE;

      if (m_Buy == 0)
            OpsRS->m_Compra = "T";
      else
            OpsRS->m_Compra = "F";
      if (m_Inst.GetCheck() == 1)
            OpsRS->m_Tipo = "I";
      else
            OpsRS->m_Tipo = "P";

      if (Edit)
            Buffer = m_Instrumento;
        
      OpsRS->m_IDInstrumento = atol(LPCSTR(Buffer.Mid(Buffer.Find('\t') + 1)));

      if (!Edit){
            OpsRS->m_IDCliente=m_Clave;
            if (!m_Valor.IsEmpty())
                  OpsRS->m_Valor = m_Valor;
            else{
                  if (OpsRS->m_Compra == "F")
                        OpsRS->m_valCompra = m_valCompra;
                  else
                        OpsRS->m_valVenta = m_valVenta;
            }
      }
      else{
            OpsRS->m_Valor = m_Registrado;
            OpsRS->m_valCompra = "";
            OpsRS->m_valVenta = "";
      }


      CString where = "UserID = \'" + m_Clave + "\'";

      SaldosRS->m_strFilter = where;
      
      try {
            SaldosRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      try {
            SaldosRS->Edit();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      float saldo = (float) atof(SaldosRS->m_Saldo1);
      float opValue;
      float st;
      float st2 = (float)0.0;

      if (m_Buy == 0){
            opValue = (float)-1.0;
            st = (float) atof(m_valVenta);
      }
      else{
            opValue = (float)1.0;
            st = (float) atof(m_valCompra);
      }

      if (st == 0.0) st = (float) atof(m_Valor);
      
      if (Edit){
            if ((InstRS->m_MercadoID == 12) || (InstRS->m_MercadoID == 13))
                  st2 = (-1) * opValue * (float)atof(OldRegistrado);
            else
                  st2 = (-1) * opValue * (float)atof(OldRegistrado) * (long)m_OldUnidades;
            st = (float)atof(m_Registrado);
      }

      if (m_Inst.GetCheck() == 1){
            float st1;
            if ((InstRS->m_MercadoID == 12) || (InstRS->m_MercadoID == 13))
                  st1 = st  * opValue;
            else
                  st1 = st * (long)m_Unidades * opValue;
            saldo += st1 + st2 ;
      }else{
            float st1;
            if ((InstRS->m_MercadoID == 12) || (InstRS->m_MercadoID == 13))
                  st1 = st * opValue;
            else {
                  st1 = (long)m_Unidades/st;
                  st1 = (float) ((long) st1 );    // Actual # of units purchased
                  st1 = st1 * st * opValue;
            }
            saldo += st1 + st2;
      }

      char tot [20];
      sprintf(tot, "%.4f", saldo);
      SaldosRS->m_Saldo1 = tot;
      m_Saldo = saldo;

      try {
            SaldosRS->Update();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      try {
            OpsRS->Update();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }
      
      ReadOps();

      t = CTime::GetCurrentTime();
      m_Fecha = t.Format("%d/%m/%y");

      m_btnGuardar.EnableWindow(FALSE);
      m_EditUnidades.EnableWindow(FALSE);
      m_CompraVenta.EnableWindow(FALSE);
      m_Tipo.EnableWindow(FALSE);
      m_ListOperaciones.EnableWindow(FALSE);

      UpdateData(FALSE);


// Actualizar Portafolio

      char x[6];
      sprintf(x, "%ld", atol(LPCSTR(Buffer.Mid(Buffer.Find('\t') + 1))));
      where = "((ClientID = \'" + m_Clave + "\' ) AND ( InstrumentoID = "+ x + "))";
      PortafolioRS->m_strFilter = where;

      if( !PortafolioRS->IsBOF()){

            try {
                  PortafolioRS->Requery();
            }
            catch (CDBException *pEx ){
                  AfxMessageBox(pEx->m_strError);
                  pEx->Delete();
                  return;
            }
      }

      if( PortafolioRS->IsBOF()){
            try {
                  PortafolioRS->AddNew();
                  PortafolioRS->m_CantidadAnterior=0L;
                  PortafolioRS->m_ClientID = m_Clave;
                  PortafolioRS->m_InstrumentoID = atol(LPCSTR(Buffer.Mid(Buffer.Find('\t') + 1)));
                  PortafolioRS->m_Cantidad = 0L;
                  PortafolioRS->m_Instancias=0L;
                  PortafolioRS->m_PortafolioID = m_Clave + LPCSTR(Buffer.Mid(Buffer.Find('\t') + 1));
            }
            catch (CDBException *pEx ){
                  AfxMessageBox(pEx->m_strError);
                  pEx->Delete();
            }
      } else {
            try {
                  PortafolioRS->Edit();
            }
            catch (CDBException *pEx ){
                  AfxMessageBox(pEx->m_strError);
                  pEx->Delete();
            }
      }
      
      if (m_Buy == 1){ //Venta
            PortafolioRS->m_Cantidad -= m_Unidades;
            if (Edit) PortafolioRS->m_Cantidad += m_OldUnidades;
      }else{
            PortafolioRS->m_Cantidad += m_Unidades;
            if (Edit){
                  PortafolioRS->m_Cantidad -= m_OldUnidades;
                  if (Edit) PortafolioRS->m_Instancias--;
            }
            PortafolioRS->m_Instancias++;
            char y[20];
            sprintf(y,"%.2f", st + ((float)atof(OldRegistrado) * (-1)) + atof(PortafolioRS->m_Costo));
            PortafolioRS->m_Costo=y;
      }

      try {
            PortafolioRS->Update();
            try {
                  PortafolioRS->Requery();
            }
            catch (CDBException *pEx ){
                  AfxMessageBox(pEx->m_strError);
                  pEx->Delete();
                  return;
            }
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            Limpiar();
            Edit = FALSE;
            return;
      }

      Limpiar();
      Edit = FALSE;
      AfxMessageBox("Actualizacisn Exitosa!");

}

void COps::OnNuevo()
{
      Limpiar();
      CTime t = CTime::GetCurrentTime();
      m_Fecha = t.Format("%d/%m/%y");
      
      try {
            OpsRS->AddNew();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }
      
      UpdateData(FALSE);
      m_Fecha = t.Format("%d/%m/%y");

      m_ListOperaciones.EnableWindow(TRUE);
      
}

void COps::OnBorrar()
{
      int index = m_Operaciones.GetCurSel();
      
      if (index == LB_ERR){
            AfxMessageBox("Debe selccionar una operacisn!");
            return;
      }

      CString where = "IDCliente = \'";

      where += m_Clave + "\'";

      OpsRS->m_strFilter = where;
      
      try {
            OpsRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      try {
            OpsRS->SetAbsolutePosition((long) index + 1);
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      try {
            OpsRS->Edit();
            OpsRS->m_Baja = TRUE;
            OpsRS->Update();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }


      m_Fecha = OpsRS->m_Fecha.Format("%d/%m/%y");
      m_Unidades = OpsRS->m_NumValores;

      if (atof(OpsRS->m_valCompra) > 0.0)
            m_Registrado = OpsRS->m_valCompra;

      if (atof(OpsRS->m_Valor) > 0.0)
            m_Registrado = OpsRS->m_Valor;
      
      if (atof(OpsRS->m_valVenta) > 0.0)
            m_Registrado = OpsRS->m_valVenta;

      if (OpsRS->m_Compra == "T" )
            m_Buy=0;
      else
            m_Buy=1;
      
      if (OpsRS->m_Tipo == "I")
             m_paper=0;
      else
            m_paper = 1;


      where = "UserID = \'" + m_Clave + "\'";

      SaldosRS->m_strFilter = where;
      
      try {
            SaldosRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      try {
            SaldosRS->Edit();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      float saldo = (float) atof(SaldosRS->m_Saldo1);
      float opValue;
      float st;

      if (m_Buy == 0){
            opValue = (float)1.0;
            st = (float) atof(m_Registrado);
      }
      else{
            opValue = (float)-1.0;
            st = (float) atof(m_Registrado);
      }

      if (st == 0.0) st = (float) atof(m_Registrado);

      if (m_Inst.GetCheck() == 1){
            float st1 = st * (long)m_Unidades * opValue;
            saldo += st1;
      }else{
            float st1 = (long)m_Unidades/st;
            st1 = (float) ((long) st1 );    // Actual # of units purchased
            st1 = st1 * st * opValue;
            saldo += st1;
      }

      char tot [20];
      sprintf(tot, "%.4f", saldo);
      SaldosRS->m_Saldo1 = tot;
      m_Saldo = saldo;

      try {
            SaldosRS->Update();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      char x [6];
      sprintf(x, "%ld", OpsRS->m_IDInstrumento);
      where = "(PortafolioID = \'" + m_Clave + x +"\' )";
      PortafolioRS->m_strFilter = where;

      try {
            PortafolioRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      try {
            PortafolioRS->Edit();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }


      PortafolioRS->m_Cantidad -= m_Unidades;
      PortafolioRS->m_Instancias--;
      char y[20];
      sprintf(y,"%.2f", atof(PortafolioRS->m_Costo)-st);
      PortafolioRS->m_Costo=y;

      try {
            PortafolioRS->Update();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }
      ReadOps();

}

void COps::OnEditar()
{
      int index = m_Operaciones.GetCurSel();
      
      if (index == LB_ERR){
            AfxMessageBox("Debe selccionar una operacisn!");
            return;
      }

      CString where = "IDCliente = \'";

      where += m_Clave + "\'";

      OpsRS->m_strFilter = where;
      
      try {
            OpsRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      try {
            OpsRS->SetAbsolutePosition((long) index + 1);
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      m_Fecha = OpsRS->m_Fecha.Format("%d/%m/%y");
      m_Unidades = m_OldUnidades = OpsRS->m_NumValores;
      
      if (atof(OpsRS->m_valCompra) > 0.0)
            m_Registrado = OpsRS->m_valCompra;

      if (atof(OpsRS->m_Valor) > 0.0)
            m_Registrado = OpsRS->m_Valor;
      
      if (atof(OpsRS->m_valVenta) > 0.0)
            m_Registrado = OpsRS->m_valVenta;

      if (OpsRS->m_Compra == "T" )
            m_Buy=0;
      else
            m_Buy=1;
      
      if (OpsRS->m_Tipo == "I")
             m_paper=0;
      else
            m_paper = 1;

      char c[20];

      sprintf(c, "%ld", OpsRS->m_IDInstrumento);
      
      try {
            index = OpsRS->m_IDInstrumento /*- 5L*/; /* -5L Due to offset in Instrumentos DB*/
            InstUnSortRS->SetAbsolutePosition( index);
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      index = m_ListOperaciones.FindString( -1, InstUnSortRS->m_Nombre );
      m_ListOperaciones.SetCurSel(index);

      UpdateData(FALSE);

      try {
            OpsRS->Edit();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      Edit = TRUE;
      m_btnGuardar.EnableWindow(TRUE);
      m_EditUnidades.EnableWindow(TRUE);
      m_CompraVenta.EnableWindow(TRUE);
      m_Tipo.EnableWindow(TRUE);
      m_ListOperaciones.EnableWindow(TRUE);

      OldRegistrado = m_Registrado;

}

void COps::OnCompraventa()
{
      m_Tipo.EnableWindow(TRUE);      
}

void COps::OnTipo()
{
      m_btnGuardar.EnableWindow(TRUE);
      
}


// 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;
}




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
}

void COps::OnOK()
{
      ClientesRS->Close();
      delete ClientesRS;
      ClientesRS=NULL;
      OpsRS->Close();
      delete OpsRS;
      OpsRS=NULL;
      InstRS->Close();
      delete InstRS;
      InstRS=NULL;
      InstUnSortRS->Close();
      delete InstUnSortRS;
      InstUnSortRS=NULL;
      PortafolioRS->Close();
      delete PortafolioRS;
      PortafolioRS = NULL;
      m_dbDatos.Close();
      m_dbInversim.Close();
      
      CDialog::OnClose();
      DestroyWindow();
}

BOOL COps::Create(CWnd *pParent)
{
            if (IsWindow(m_hWnd)){
            SetActiveWindow();
            return TRUE;
      }

      else return (CDialog::Create(COps::IDD, pParent));
}


void COps::OnClose()
{
      ClientesRS->Close();
      OpsRS->Close();
      InstRS->Close();
      m_dbInversim.Close();
      CDialog::OnClose();
      DestroyWindow();
      
}

void COps::OnLimpiar1()
{
      m_Materno = _T("");
      m_Paterno = _T("");
      m_Nombres = _T("");
      m_RFC =            _T("");
      m_Saldo =      (float)0.0;
      m_Clave =      _T("");
      m_Registrado = _T("");

      m_Operaciones.ResetContent();

      UpdateData(FALSE);

      ClientesRS->m_strFilter = "";
      
      try {
            ClientesRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

}

void COps::ReadOps(){

      // Get operations for client

      AfxGetApp()->LoadStandardCursor(IDC_WAIT);

      CString where = "IDCliente = \'";

      where += m_Clave + "\'";

      OpsRS->m_strFilter = where;
      
      try {
            OpsRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      m_Saldo = (float)0.0;
      
      if( OpsRS->IsEOF()){
            m_Operaciones.ResetContent();
            UpdateData(FALSE);
            return;
      }

      try {
            OpsRS->MoveFirst();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      m_Operaciones.ResetContent();

      
      if( OpsRS->IsBOF()){
            return;
      }

      char data[20], data1[20];
      CString d(data), d1(data1);

      CString Line = "";

      do {
            wsprintf(data, "%ld", OpsRS->m_IDInstrumento);
            wsprintf(data1, "%ld", OpsRS->m_NumValores);

            d=data; d1=data1;
      
            where = "(Instrumentos.InstrumentoID=" + d + ")";

            try {
                  InstUnSortRS->SetAbsolutePosition( atol(data) /*- 5L*/);/* -5L Due to offset in Instrumentos DB*/
            }
            catch (CDBException *pEx ){
                  AfxMessageBox(pEx->m_strError);
                  pEx->Delete();
            }

            Line += OpsRS->m_Fecha.Format("%d/%m/%y") + '\t';
            Line += InstUnSortRS->m_Nombre + '\t';
            Line += OpsRS->m_Tipo +'\t';

            if (*(LPCTSTR)OpsRS->m_Compra == 'T')
                  Line += (CString)"Compra" + '\t';
            else
                  Line += (CString)"Venta" + '\t';
            
            wsprintf(data1, "%ld", OpsRS->m_NumValores);

            CString dd(data1);
            Line += dd + '\t';                        // Numero de Valores comprados
            
            char data3[20];

            memset(data3, 0, 20);
            
            double num = atof(LPCTSTR(OpsRS->m_Valor));
            if (num != 0.0){
                  sprintf(data3, "%.2f", num) ;
                  CString d3(data3);
                  Line += d3;
            }

            num = atof(LPCTSTR(OpsRS->m_valVenta));
            if (num != 0.0){
                  sprintf(data3, "%.2f", num) ;
                  CString d3(data3);
                  Line += d3;
            }

            num = atof(LPCTSTR(OpsRS->m_valCompra));
            if (num != 0.0){
                  sprintf(data3, "%.2f", num) ;
                  CString d3(data3);
                  Line += d3;
            }

            if (OpsRS->m_Baja == TRUE)
                  Line += "\tDado de Baja";

            m_Operaciones.AddString(LPCTSTR (Line) );

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

            Line = "";

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

            where = "UserID = \'" + m_Clave + "\'";

      SaldosRS->m_strFilter = where;
      
      try {
            SaldosRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      m_Saldo = (float) atof(SaldosRS->m_Saldo1);

      AfxGetApp()->LoadStandardCursor(IDC_ARROW);

      
}

void COps::OnSelchangeListinstrumento()
{
      long IDInst;

      m_ListOperaciones.GetText(m_ListOperaciones.GetCurSel(), Buffer);
      
      IDInst = atol(LPCSTR(Buffer.Mid(Buffer.Find('\t') + 1)));

      char data[20];
      wsprintf(data, "%ld", IDInst);
      
      CString d(data);
      
      CString where = "(Instrumentos.InstrumentoID=" + d + ")";

      InstRS->m_strFilter = where;
      
      try {
            InstRS->Requery();
      }
      catch (CDBException *pEx ){
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
      }

      m_Valor ="";
      m_valCompra="";
      m_valVenta="";

      char data3[20];

      double num = atof(LPCTSTR(InstRS->m_Valor));
      if (num != 0.0){
            sprintf(data3, "%.2f", num) ;
            CString d3(data3);
            m_Valor = d3;
      }

      num = atof(LPCTSTR(InstRS->m_Venta));
      if (num != 0.0){
            sprintf(data3, "%.2f", num) ;
            CString d3(data3);
            m_valVenta = d3;
      }

      num = atof(LPCTSTR(InstRS->m_Compra));
      if (num != 0.0){
            sprintf(data3, "%.2f", num) ;
            CString d3(data3);
            m_valCompra = d3;
      }

    UpdateData(FALSE);


      
}

void COps::OnCompra()
{
      m_Inst.EnableWindow(TRUE);
      
}

void COps::OnInstrumentos()
{
      m_btnGuardar.EnableWindow(TRUE);
      
}

void COps::OnVenta()
{
      m_Inst.EnableWindow(TRUE);
      
}

void COps::OnPesos()
{
      m_btnGuardar.EnableWindow(TRUE);
      
}

void COps::Ejecutar()
{
      CDatabase m_dbInversim;

      HCURSOR hCursor;
 
   hCursor = AfxGetApp()->LoadStandardCursor(IDC_WAIT);

      
      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!");
            return;
      }

      // Acciones

      CString str = "UPDATE Instrumentos INNER JOIN T_NCap_Accion ON Instrumentos.Nombre = T_NCap_Accion.Emisora SET Instrumentos.Valor = T_NCap_Accion.Precio_actual WHERE Instrumentos.MercadoID = 5";
      
      try{
            m_dbInversim.ExecuteSQL(str);
      }
      catch (CDBException *pEx){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      //Cambios

      str = "UPDATE Instrumentos INNER JOIN T_NCap_Cambios ON Instrumentos.Nombre = T_NCap_Cambios.Clave_pizarra SET Instrumentos.Valor = T_NCap_Cambios.Precio_actual WHERE Instrumentos.MercadoID = 6";
      
      try{
            m_dbInversim.ExecuteSQL(str);
      }
      catch (CDBException *pEx){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      //Cambios-Coberturas

      str = "UPDATE Instrumentos INNER JOIN T_NCam_Coberturas ON Instrumentos.Nombre = T_NCam_Coberturas.Fuente_plazo SET Instrumentos.Compra = T_NCam_Coberturas.Compra, Instrumentos.Venta = T_NCam_Coberturas.Venta WHERE Instrumentos.MercadoID = 2";
      
      try{
            m_dbInversim.ExecuteSQL(str);
      }
      catch (CDBException *pEx){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      //Cambios-Dolar Internacional

      str = "UPDATE Instrumentos INNER JOIN T_NCam_Dolar_Int ON Instrumentos.Nombre = T_NCam_Dolar_Int.Fuente_valor SET Instrumentos.Compra = T_NCam_Dolar_Int.Compra, Instrumentos.Venta = T_NCam_Dolar_Int.Venta WHERE Instrumentos.MercadoID = 3";
      
      try{
            m_dbInversim.ExecuteSQL(str);
      }
      catch (CDBException *pEx){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      //Cambios-Monedas Internacional

      str = "UPDATE Instrumentos INNER JOIN T_ICam_Monedas_Int ON Instrumentos.Nombre = T_ICam_Monedas_Int.Divisa SET Instrumentos.Compra = T_ICam_Monedas_Int.Compra, Instrumentos.Venta = T_ICam_Monedas_Int.Venta WHERE Instrumentos.MercadoID = 1";
      
      try{
            m_dbInversim.ExecuteSQL(str);
      }
      catch (CDBException *pEx){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      //Dinero-Promedio

      str = "UPDATE Instrumentos INNER JOIN T_NDin_Promedio ON Instrumentos.Nombre = T_NDin_Promedio.Instrumento_plazo SET Instrumentos.Valor = T_NDin_Promedio.Tasa_actual WHERE Instrumentos.MercadoID = 9";
      
      try{
            m_dbInversim.ExecuteSQL(str);
      }
      catch (CDBException *pEx){
            AfxGetApp()->LoadStandardCursor(IDC_ARROW);
            AfxMessageBox(pEx->m_strError);
            pEx->Delete();
            return;
      }

      //Intermedio

      str = "UPDATE Instrumentos INNER JOIN T_Nint_Mdo_Intermedio ON Instrumentos.Nombre = T_Nint_Mdo_Intermedio.Emisora SET Instrumentos.Valor = T_Nint_Mdo_Intermedio.P
Don't worry, I will grade your answer soon enough, just been veru busy lately and unable to try out your suggestions.  Sorry, I should be able to get to it by the weekend.  Don't worry, I haven't forgotten!!!
Not worried at all. You are not grading my comment. This one was to answer your no point question.
Thank you for your answer... sorry it took so long to grade!