?
Solved

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

Posted on 2003-03-02
7
Medium Priority
?
286 Views
Last Modified: 2012-06-21
Hi,
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
     sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
     TRY
     {
          // Open the database
          database.Open(NULL,false,false,sDsn,false);
         
          // 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    
          recset.Close();

          database.Close();
     }
     CATCH(CDBException, e)
     {
          // If a database exception occured, show error msg
          AfxMessageBox("Database error: "+e->m_strError);
     }
     END_CATCH;
     UpdateData(FALSE);    
}
0
Comment
Question by:plho109
[X]
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
7 Comments
 
LVL 4

Expert Comment

by:Zmey2
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')"
0
 

Author Comment

by:plho109
ID: 8056054
Zmey2,

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.

 
0
 
LVL 4

Expert Comment

by:Zmey2
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.
0
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

by:plho109
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

0
 
LVL 4

Accepted Solution

by:
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...
0
 

Author Comment

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

Author Comment

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

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