MANAGE_ACCESS_WITH_C++_&_ODBC

I need to developpe a project using Visual C++.
I need also to manage Access Data Base with ODBC.
Below you can read an example using Visual Basic to do it.
---------------------------------------------------------------------------

.....
.....
Public Trabajo As Workspace
.....
Public BDatos_1 As Database
.....
Public Query_BDatos_1 As QueryDef
....
Public Rec_BBatos_1 As Recordset
....
...
Public Const Tabla_BDatos_1 = "Tabla_1"
Public Const Elem_BDatos_1 = "Elem_1"
....
....
....
Set Trabajo = CreateWorkspace("", "", "", dbUseODBC)
Trabajo.DefaultCursorDriver = dbUseODBCCursor
....
....
Set BDatos_1 = Trabajo.OpenDatabase(DSN, False, False, "ODBC;DSN=MS
Access 97
Database;DBQ=D:\BDatos\BD_1")

BDatos_1.QueryTimeout = TimeOut

Set Query_BDatos_1 = BDatos_1.Connection.CreateQueryDef("")

On Error GoTo xxxxx
Set Rec_BDatos_1 =BDatos_1.OpenRecordset(Tabla_BDatos_1, dbOpenDynamic,
0, dbOptimistic)
    If Rec_BDatos_1.Fields(Elem_BDatos_1).Value = 2 Then
        Rec_BDatos_1.Edit
        Rec_BDatos_1.Fields(Elem_BDatos_1).Value = 0
        Rec_BDatos_1.Update
    End If
    Rec_BDatos1.Close
.....
.....
.....
---------------------------------------------------------------------------

My question is: what is the way to do it with C/C++ using Visual C++ ??. Could you send me an example about.

Thank you.  Serrano.



SerranoAsked:
Who is Participating?
 
V_BapatConnect With a Mentor Commented:
To work with database, first you have to include afxdao.h in stdafx.h in your project.

To open a database:
CDaoDatabase db();
db.Open(/*file name of the access file*/);

To open a recordset:
CDaoRecordset rs(&db);
CString strSQL("SELECT * FROM table1"); /* here table1 is the table in the database*/
rs.Open(dbOpenDynaset, strSQL);

To manipulate data in the tables, you have to either call Execute(/*SQL string*/) or create a CDaoRecordset object and perform operations on it.

To navigate in the recordset, use Move(), MoveFirst(), MoveLast(), MovePrev() and MoveNext(). To find records in recordset use Find(), FindFirst(), FindNext() etc. See VC++ documentation for details.

I would suggest you to go through Daotable and Daoview examples in MSDN. You can understand how to create tables and fields from Daotable. You can even reuse a lot of code in your project. From Daoview you can learn how to navigate through the recordset object.

Read CDaoQueryDef on how to create queries. I don't know much about them. But you can find code in Daotable sample in MSDN.

If you need detailed code, feel free to ask. I can send mail you sample workspace if you want one.

Vicky
0
 
WynCommented:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=10265751

That is a locked question and have far more than you need.
Enjoy
Wyn
0
 
WynCommented:
Ask whatever you aint clear and I will answer you.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
V_BapatCommented:
If you are using MS Access it is better to use DAO classes provided in VC++.

But if the requirement is such that you have to use ODBC, there is not much problem in that too.

1. In VC++ you can skip creating a new workspace. Just pass a null in the constructor of CDaoDatabase object and a workspace will be created for your app.
2. To use recordsets, derive your class from CDaoRecordset. This will allow you to use RFX method which is easier to use. This will help you to access the fields directly though member variables.

Hope I am clear enough.

Vicky
0
 
SerranoAuthor Commented:
Answer always are satisfactory. Also in this case.
But I'd need a byte of code example because I has never used Visual C++ to developpe and I don't now what are the sequence of instruccions to create a workspace, to connect with a DataBase (Access in this case), to open that DataBase, to create a query ...
The example, I sent you some days ago, with Visual Basic is not developped for me, it is only an example to show you that I want with Visual C++.

Thanks a lot. Serrano.

Sorry, my level in english is not good.
0
 
WynCommented:
so,what do you want indeed?
You need a WIZARD-generated project or plain vc project for database?
0
 
SerranoAuthor Commented:
Hello V. Bapat. Thank you for your message on database handling with Visual C++.  
As I said in my last message, I have not used Visual C++ up to now.    
Bellow I indicate you the errors that I have obtained of Visual C++ when I have tried to compile the code that you sent me:  
-------------------------------------------------------------
......  

/* To work with database, first you have to include afxdao.h in stdafx.h  in your project. */
 
# include <afxdao.h>  
# include <stdafx.h>   /* ERROR ==> No such file or directory !!!! */  

......  
......  
/* To open a database:  */  

CDaoDatabase db();  
db.Open("C:\DataBase1");      /* ERROR ==> left of '.Open' must have class/struct/union type */  

 
/* To open a recordset: */
 
CDaoRecordset rs(&db);      /*  ERROR ==> cannot convert parameter 1 from 'class CDaoDatabase (__cdecl *)(void)' to 'class CDaoDatabase  *'  */
 
CString strSQL("SELECT * FROM table1");  
rs.Open(dbOpenDynaset, strSQL);  
 
......  
......  
--------------------------------------------------------------  
 
Is't possible that to be able to use the header " stdafx.h " it should include some special library of Visual C++??
 
Should I define the variable " db " with some specific type???  

I'd like again to receive your answer.
Thank you. Manuel.
0
 
V_BapatCommented:
Hi Serrano
I think there is some misunderstanding. Open StdAfx.h (which is a part of your project) and add the line

#include <afxdao.h>

I expect that you have created the project using the AppWizard!!!

Vicky
0
All Courses

From novice to tech pro — start learning today.