Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Please give help on press a button to write a CString to a simple MS Access database

I would like to create a MS Access Database management interface without using the application wizard. It is OK to Read data from DB. However, I have tried a week's time still cannot write data to database. Can anyone give me some comments on my code ?

The objective of following code is to save the CString of a editbox(m_EditNewCom) to the field(CompanyName) in table(Company) when pressing a button.

void CPickCompanyDlg::OnBAddok()
     CDatabase database;
     CString SqlString;
     CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
     CString sDsn;
     CString sFile = "db.mdb";
     // Build ODBC connection string
          // Open the database
          // Allocate the recordset
          CRecordset recset( &database );

          // Build the SQL statement
          // Execute the query
          recset.Open(CRecordset::dynamic, SqlString,CRecordset::appendOnly);
          SqlString.Format("insert into Company set CompanyName=%s",m_EditNewCom);
          database.ExecuteSQL(SqlString); // update or insert    

     CATCH(CDBException, e)
          // If a database exception occured, show error msg
          AfxMessageBox("Database error: "+e->m_strError);
  • 4
  • 3
1 Solution
It seems to me, that error is in sql string. Since it is interpreted by access im must have correct access syntax.
Try something like:
"insert into Table1 (FieldName) VALUES ('Value')"
plho109Author Commented:

i have amended the sql string like this,
SqlString.Format("insert into Company(CompanyName) VALUE('%s')",m_EditNewCom);

but it also report
"Database error: ODBCé{“®’öŽ®Ÿ“—LŽx‰‡“®‘ÔŸà•W" (in Chinese)
"Database error: ODBC driver does not support dynamic pointer" (the meaning in english)

i am a newbie to visual C++, it is my first VC project.

You should examine MS Access help for SQL syntax. Notice, that % symbol in access is * and that string
insert into Company(CompanyName) VALUE('%s')
means nothing, for it is syntax to add one record to specific table with one value.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

plho109Author Commented:
I doubt the bug is coming from  recset.Open(CRecordset::dynamic, SqlString,CRecordset::appendOnly);

this statement, because i remarked this to recset.close(). it runs normally

anyway, thanks Zmey2's advice first

any time, plho109!
Actually i don't understand why you need recordset here...
database.ExecuteSQL(SqlString); will do the append...
plho109Author Commented:
let me try it first
the recordset is just copied from some source
plho109Author Commented:
Thanks Zmey2, your comment helps me write data to DB.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now