Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Updating a field in Access database

Updating a field in Access database

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelpalgorithmsannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joy007
    wrote on last edited by
    #1

    Hi guys, I am using an Access database in my application. The problem arises then when I try to update or edit a field corresponding to another field in the data base. Consider the database contains 2 fields,namely m_NameE2 and m_NameB2. Now I try searching out the m_NameE2 field for a given text,if found then I go for updating the corresponding m_NameB2 field.But it gives like such errors "CRecordset is read-only"-when I use rs.Edit(),or "Attempt to Update or delete failed" for rs.Update(); I use the following code: CString ss,gg; int present=0; GetDlgItemText(IDC_EDIT1,ss); gg=m_gedit.Get_Text(); if(ss!="" && gg!="") { CDatabase db; db.OpenEx(_T("DSN=ForeignName"),CDatabase::noOdbcDialog); if(db.CanUpdate()==TRUE) { CNameRecords rs(&db); rs.Open(CRecordset::dynaset); rs.MoveFirst(); do { if(rs.m_NameE2==ss) { present=1; AfxMessageBox("Data found and will be tried for modifying"); break; } rs.MoveNext(); }while(rs.IsEOF()!=TRUE); if(present==0) { AfxMessageBox("Data not found in English Name column !!"); } else { rs.Edit(); rs.m_NameB2=gg; rs.Update(); SetDlgItemText(IDC_EDIT2,rs.m_Sex2); SetDlgItemText(IDC_EDIT3,rs.m_Origin2); AfxMessageBox("Data is saved successfully"); } rs.Close(); //AfxMessageBox("Data is present in database,enter a new one"); //m_save.EnableWindow(FALSE); } else AfxMessageBox("Data is not saved,because data can't be appended in database file !!!!... "); db.Close(); } else { AfxMessageBox("Data is not saved,because the fields are partially or not filled at all !!!!... "); //m_save.EnableWindow(FALSE); } Please help...

    J 1 Reply Last reply
    0
    • J joy007

      Hi guys, I am using an Access database in my application. The problem arises then when I try to update or edit a field corresponding to another field in the data base. Consider the database contains 2 fields,namely m_NameE2 and m_NameB2. Now I try searching out the m_NameE2 field for a given text,if found then I go for updating the corresponding m_NameB2 field.But it gives like such errors "CRecordset is read-only"-when I use rs.Edit(),or "Attempt to Update or delete failed" for rs.Update(); I use the following code: CString ss,gg; int present=0; GetDlgItemText(IDC_EDIT1,ss); gg=m_gedit.Get_Text(); if(ss!="" && gg!="") { CDatabase db; db.OpenEx(_T("DSN=ForeignName"),CDatabase::noOdbcDialog); if(db.CanUpdate()==TRUE) { CNameRecords rs(&db); rs.Open(CRecordset::dynaset); rs.MoveFirst(); do { if(rs.m_NameE2==ss) { present=1; AfxMessageBox("Data found and will be tried for modifying"); break; } rs.MoveNext(); }while(rs.IsEOF()!=TRUE); if(present==0) { AfxMessageBox("Data not found in English Name column !!"); } else { rs.Edit(); rs.m_NameB2=gg; rs.Update(); SetDlgItemText(IDC_EDIT2,rs.m_Sex2); SetDlgItemText(IDC_EDIT3,rs.m_Origin2); AfxMessageBox("Data is saved successfully"); } rs.Close(); //AfxMessageBox("Data is present in database,enter a new one"); //m_save.EnableWindow(FALSE); } else AfxMessageBox("Data is not saved,because data can't be appended in database file !!!!... "); db.Close(); } else { AfxMessageBox("Data is not saved,because the fields are partially or not filled at all !!!!... "); //m_save.EnableWindow(FALSE); } Please help...

      J Offline
      J Offline
      Jose Lamas Rios
      wrote on last edited by
      #2

      This may or may not be related to your problem, but joy007 wrote: if(db.CanUpdate()==TRUE) The documentation says CDatabase::CanUpdate returns "nonzero if the CDatabase object allows updates". "Nonzero" doesn't mean neccesarily TRUE, which is 1. You should always write your tests this way to be on the safe side:

      if (db.CanUpdate() != FALSE)
      {
      }

      You could also check rs.CanUpdate Besides, shouldn't you use a SELECT statement instead of traversing all the recordset looking for a record by yourself? -- jlr http://jlamas.blogspot.com/[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups