Serrano
asked on
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.DefaultCursorDrive r = 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.Create QueryDef(" ")
On Error GoTo xxxxx
Set Rec_BDatos_1 =BDatos_1.OpenRecordset(Ta bla_BDatos _1, dbOpenDynamic,
0, dbOptimistic)
If Rec_BDatos_1.Fields(Elem_B Datos_1).V alue = 2 Then
Rec_BDatos_1.Edit
Rec_BDatos_1.Fields(Elem_B Datos_1).V alue = 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.
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.DefaultCursorDrive
....
....
Set BDatos_1 = Trabajo.OpenDatabase(DSN, False, False, "ODBC;DSN=MS
Access 97
Database;DBQ=D:\BDatos\BD_
BDatos_1.QueryTimeout = TimeOut
Set Query_BDatos_1 = BDatos_1.Connection.Create
On Error GoTo xxxxx
Set Rec_BDatos_1 =BDatos_1.OpenRecordset(Ta
0, dbOptimistic)
If Rec_BDatos_1.Fields(Elem_B
Rec_BDatos_1.Edit
Rec_BDatos_1.Fields(Elem_B
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.
Ask whatever you aint clear and I will answer you.
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
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
ASKER
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.
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.
so,what do you want indeed?
You need a WIZARD-generated project or plain vc project for database?
You need a WIZARD-generated project or plain vc project for database?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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
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
That is a locked question and have far more than you need.
Enjoy
Wyn