Whats wrong with my ADO code?

My Access database has three tables with same primary key (Field "Item") and a query. The query has three joined tables linking with primary key.

if one record in Table 1 was deleted /updated, the record with same primary key in all the linked tables ( eg. Table 2, Table 3) in the query would be deleted / updated automatically.

Now I try to use ADO to update or delete record in Access database through a Query.

But I found ADO can not auto delete or update the record in all the joined tables. Only the record (eg, "Tool" in Item field) in Table 1 was deleted or updated. The record with same primary key in the jointed Table 2, 3 were not deleted or updated at all.

The following is my code:

_bstr_t sConn = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = c:\\ItemDB.mdb";
_bstr_t sQuery = "SELECT * FROM Query1";
::CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
pConn->Open(sConn, "","", adModeUnknown);
_RecordsetPtr pRecordset(__uuidof(Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->CursorType = adOpenDynamic;
pRecordset->LockType = adLockOptimistic;
pRecordset->Open(sQuery, pConn.GetInterfacePtr(), adOpenDynamic,
adLockOptimistic,adCmdText);
pRecordset->MoveFirst();
CString test = "Tool";
pRecordset->PutCollect(_T("Item"), _variant_t(test));
pRecordset->Update();

//only "Item" field in Table 1 was updated, Table 2 and 2 were not updated

pRecordset->MoveNext();

pRecordset->Close();
pRecordset.Release();
pConn->Close();
pConn.Release();
::CoUninitialize();

What's wrong with my ADO code?
How to update or delete the record in all joined tables through a query using ADO?

Thanks a lot.
[1855 byte] By [pyh1970] at [2007-11-20 1:35:41]
# 1 Re: Whats wrong with my ADO code?
That sounds to me more like an SQL question than an ADO question.
ADSOFT at 2007-11-10 23:17:52 >
# 2 Re: Whats wrong with my ADO code?
My Access database has three tables with same primary key (Field "Item") and a query. The query has three joined tables linking with primary key. The fact that your primary keys have the same names means absolutely nothing. These are different keys for different tables. Therefore, being deleting the record from one of them you never can expect some other tables get aware of this.

To provide a way of automated deletion of logically "linked" records in different tables you must establish proper relations between the tables. Your dependent tables must contain foreign key with cascade deletion. Thus, deletion of a record in a "master" table will result in deletion of referred records in "slave" tables.

I'm not sure if Access supports such relations (I mean cascade delete), but this way this is done in "adult" database engines. :)
Igor Vartanov at 2007-11-10 23:18:59 >
# 3 Re: Whats wrong with my ADO code?
The fact that your primary keys have the same names means absolutely nothing. These are different keys for different tables. Therefore, being deleting the record from one of them you never can expect some other tables get aware of this.

To provide a way of automated deletion of logically "linked" records in different tables you must establish proper relations between the tables. Your dependent tables must contain foreign key with cascade deletion. Thus, deletion of a record in a "master" table will result in deletion of referred records in "slave" tables.

I'm not sure if Access supports such relations (I mean cascade delete), but this way this is done in "adult" database engines. :)

Igor,

So Sql doesn't have a statement the handles that?
ADSOFT at 2007-11-10 23:19:58 >
# 4 Re: Whats wrong with my ADO code?
Guys,

I just test to use DAO instead of ADO to update or delete record through Query1. It works very well.

Access database has three tables. Each table used "item" field as primary key. ( the three tables have exactly same records in "item" field)

Query1 was set up by linking Table1 and Table 2 and 3 through primary key -"item' field.

if one record in Table 1 was updated or deleted, then the corresponding record in Table 2 and 3 was updated or deleted auto.

But I found ADO did not work in that way. Maybe DAO is better than ADO.
pyh1970 at 2007-11-10 23:20:57 >
# 5 Re: Whats wrong with my ADO code?
That sounds to me more like an SQL question than an ADO question.
Nice remark. ;) The problem it's into the tables relationship, as Igor said.

To provide a way of automated deletion of logically "linked" records in different tables you must establish proper relations between the tables. Your dependent tables must contain foreign key with cascade deletion. Thus, deletion of a record in a "master" table will result in deletion of referred records in "slave" tables.
I'm not sure if Access supports such relations (I mean cascade delete), but this way this is done in "adult" database engines. :)
Access supports such relations. ;) So, pyh1970's problem it's in his database design.

But I found ADO did not work in that way. Maybe DAO is better than ADO.
No way! Forget DAO. ;)
Maximus_X at 2007-11-10 23:22:03 >
# 6 Re: Whats wrong with my ADO code?
I used same database and same query. just used DAO and ADO.

Using DAO, it works well. But using ADO, it did not work.

I still did not get it. There is nothing wrong with database or Query because it works when using DAO.
pyh1970 at 2007-11-10 23:22:56 >
# 7 Re: Whats wrong with my ADO code?
DAO it's build on Microsoft Jet Engine and was design to work with Microsoft Access.
I'm not sure that your ADO code it's correct 100%.
I strongly recommand to you, the using of a wrapped class written by Carlos Antollini:
http://www.codeproject.com/database/caaadoclass1.asp
http://www.codeproject.com/database/connectionstrings.asp
I used it many times, and I had no problems. ;)
Maximus_X at 2007-11-10 23:24:00 >
# 8 Re: Whats wrong with my ADO code?
Maximus,

Question,

One of the links you posted mentioned something about about a DSN-less connection to an ODBC database, i.e, CRecordset.

Is is possible to use ODBC w/o having to register the database on the system and just use the datapath?
ADSOFT at 2007-11-10 23:24:58 >
# 9 Re: Whats wrong with my ADO code?
Maximus,

Question,

One of the links you posted mentioned something about about a DSN-less connection to an ODBC database, i.e, CRecordset.

Is is possible to use ODBC w/o having to register the database on the system and just use the datapath?

Man, I'm not so sure. I didn't try it. I read that if on your database machine there is a OLE DB Driver, it's possible to use some of ODBC strings. You may try.
I always used ADO string connections for my applications based on ADO. Also, I used this string to connect to a database through an instance of CADODatabase class, not with CRecordset class.
Sample of a CADODatabase uses:

CString strConnection = _T("");
strConnection.Format("Driver={Microsoft Access Driver (*.mdb)};"
"Dbq=%s;Uid= ;Pwd=s1;", strDBPath);

CADORecordset rs;
CADODatabase db;
int ValRet = 0;

if(db.Open(strConnection))
{
rs = CADORecordset(&db);

if (rs.Open( strsql, CADORecordset::openQuery))
{
ValRet = rs.GetRecordCount();
}
rs.Close();
}

I'm not remenber what it was the string used by me, but comparated it with the strings from http://www.codeproject.com/database/connectionstrings.asp you can find that this string it's similary with OLE DB and ODBC strings for a connection to a Access database.
Maximus_X at 2007-11-10 23:26:07 >