-
April 1st, 2004, 06:50 AM
#1
DAO 3.6 delete problem
Hi,
I use the following code with dao 3.5 and dao 3.6. It works with 3.5, but with 3.6 at the second deletion a exception is thrown (from DoFieldExchange a String deletion fails)
Code:
pSet2->Open();
while(!pSet2->IsEOF())
{
pSet2->Delete();
pSet2->MoveFirst();
}
pSet2->Close();
Whats the proble?
Thanks
Michael
-
April 1st, 2004, 08:02 AM
#2
with Close() and Open() instead of MoveNext() it works, but is very slow.
-
April 1st, 2004, 10:20 AM
#3
I'm not really sure, but I can suggest some things to try.
Is your recordset empty before your code executes?
If so perhaps first refreshing the EOF flag will help:
Code:
pSet2->Open();
pSet2->MoveLast();
pSet2->MoveFirst();
while(!pSet2->IsEOF())
{
pSet2->Delete();
pSet2->MoveFirst();
}
pSet2->Close();
Is there any chance it is deleting the same record twice?
If so, perhaps an Update will help. Though you might not be able to update a deleted record.
Code:
pSet2->Open();
pSet2->MoveLast();
pSet2->MoveFirst();
while(!pSet2->IsEOF())
{
pSet2->Delete();
pSet2->Update();
pSet2->MoveFirst();
}
pSet2->Close();
Good luck.
Sincerely,
- Ron
-
April 1st, 2004, 10:56 AM
#4
If so, perhaps an Update will help. Though you might not be able to update a deleted record.
code:--------------------------------------------------------------------------------
pSet2->Open();
pSet2->MoveLast();
pSet2->MoveFirst();
while(!pSet2->IsEOF())
{
pSet2->Delete();
pSet2->Update();
pSet2->MoveFirst();
}
pSet2->Close();
--------------------------------------------------------------------------------
I am sorry, but this suggestion to use Update is not correct. According to MSDN ("DAO Recordset: Recordset Operations"):
Deleting Records in DAO
For general information about deleting records in DAO, see the topic "Delete Method" in DAO Help.
To delete a record from a recordset
Determine whether your recordset is updatable.
Call the recordset'sCanUpdate member function.
Move to the record you want to edit.
Use any of the recordset's navigation mechanisms that take you to a specific record.
Call the recordset's Delete member function.
You don't call Update for a deletion.
Move to another record before you attempt any other recordset operations.
In table-type and dynaset-type recordset objects, Delete removes the current record and makes it inaccessible. Although you can't edit or use the deleted record, it remains current. Once you move to another record, however, you can’t make the deleted record current again. Subsequent references to a deleted record in a recordset are invalid and cause an exception to be thrown. For more information, see Delete. You can tell whether you’re on a deleted record by calling IsDeleted.
-
April 1st, 2004, 11:02 AM
#5
VictorN,
You are correct. Update() will not work after Delete().
So then how to correct MGS76's problem? Perhaps:
Code:
pSet2->Open();
pSet2->MoveLast();
pSet2->MoveFirst();
while(!pSet2->IsEOF())
{
pSet2->Delete();
pSet2->MoveLast();
pSet2->MoveFirst();
}
pSet2->Close();
Sincerely,
- Ron
-
April 1st, 2004, 11:39 AM
#6
I'd suggest to follow MS code from MSDN article "DAO Workspace: Managing Transactions" and to do as:
Code:
try
{
pSet2->Open();
while(!pSet2->IsEOF())
{
pSet2->Delete();
pSet2->MoveNext();
}
pSet2->Close();
}
catch(CDaoException* e)
{
AfxMessageBox( e->m_pErrorInfo->m_strDescription);
e->Delete( );
...........
}
Another possibility - to use CDaoDatabase::Execute method: create SQL-delete query, do Execute, then call CDaoRecordset::Requery if necessary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|