Link to home
Create AccountLog in
Avatar of LarryAMillsSr
LarryAMillsSr

asked on

How to Insert, Delete records/Fiels into a CDatabase MS Access database using CString- MFC

I am having trouble adding records into my MS Access DB. In debug I can see that the recordset is loaded but I get an exception error when I try to do AddNew. here's the code I'm using:(m_pSet is the recordset)
BOOL CDispatcherView::RecordAdd(void)
      {
      // If already in add mode, then complete previous new record
      m_pSet->TestData(m_pSet->m_ID+1);
      if (m_bIsAddMode)
            {
            OnMove(ID_RECORD_FIRST);//ID_RECORD_LAST  
            //OnMove(ID_RECORD_LAST);//ID_RECORD_LAST      ID_RECORD_NEXT      
            }
      OnGetRecordset()->AddNew();
      m_bIsAddMode = TRUE;
      UpdateData(FALSE);
      return TRUE;
      }
The error is "Attempt to update or delete failed"
Avatar of John Mc Hale
John Mc Hale
Flag of Ireland image

Assuming that you've single stepped the code in the Debugger, where exactly is the exception being thrown? Not knowing an awful lot about MFC's interaction with Jet databases, I suspect that the problem may be related to trying to move off an incomplete record, which has the 'Required' property of one or more fields = True. This would cause an 'Update' to fail until such time as the underlying fields of the m_pSet recordset variable have been set to something meaningful??
Avatar of LarryAMillsSr
LarryAMillsSr

ASKER

At the OnMove(....) statement I really don't know what varable I'm supposed to put in the OnMove(..) statement.FIRST,PREV,or LAST when trying to add a record.
ASKER CERTIFIED SOLUTION
Avatar of John Mc Hale
John Mc Hale
Flag of Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks for the info. I'll change from "autoNumbering" How do you select a staring record in MFC to your MS Access records. I tried ID_RECORD_FIRST but it keeps giving me the last record. I don't know how to index thru the records. Could you provide me with a code snippet showing how to index thru the records. Please..
Most of the underlying database access technologies used by MFC (at least CDaoRecordset and CRecordset (ODBC) supported a 'MoveFirst' method, which you could call directly; e.g. m_pSet->MoveFirst(); Also bear in mind that after insertion of a new record, you should Requery the recordset to ensure that the newly inserted record appears in the correct postion w.r.t. the recordset. Also bear in mind that if you don't provide a 'sort order' by altering the SQL source of the underlying recordset, the recordset will use the dedault sort order of the underlying table. One last thing, I was not trying to discourage you from using Autonumber fields in your access database (I in fact use them copiously when developing Access databases), but they do involve a different approach - instead of using MFC Wizard generated applications, the approach would be to develop your Forms only using MFC, and then add in the database access by creating the CDatabase and CRecordset objects by hand and database inserts, updates and deletes by creating hand-crafted code.

Regards.
I understand. But, unfortunately that is beyond my ability right now. I wouldn't know where to begin! This is my first attempt at using CDatabase, and I'm NOT impressed.  I do a lot of STL work; but someone asked if I could establish an MS Access and Excel Database, so I'm trying.
Found this interesting link ... 'how to use multiple accessors so you can update an Access table that contains an AutoNumber field to be retrieved' ... maybe you want to investigate further!

http://www.microsoft.com/downloadS/details.aspx?FamilyID=23dbafba-9d55-4b29-9bef-89644d55b5c9&displaylang=en