Updating a field in Access database
-
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... -
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...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/[^]