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

Posted on 2003-03-02
Medium Priority
Last Modified: 2012-06-21
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);
Question by:plho109
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Expert Comment

ID: 8055750
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')"

Author Comment

ID: 8056054

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.


Expert Comment

ID: 8056081
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.
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf


Author Comment

ID: 8056225
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


Accepted Solution

Zmey2 earned 300 total points
ID: 8056247
any time, plho109!
Actually i don't understand why you need recordset here...
database.ExecuteSQL(SqlString); will do the append...

Author Comment

ID: 8056263
let me try it first
the recordset is just copied from some source

Author Comment

ID: 8070149
Thanks Zmey2, your comment helps me write data to DB.

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question