Solved

Delete Record from MS Access using MFC

Posted on 2008-06-19
12
627 Views
Last Modified: 2013-11-20
Hi all,

I downloaded this project: http://www.codeproject.com/KB/database/readdb.aspx

and I can't seem to delete a record from this DB.

When i call Open on the recordset i get this exception:

"Dynamic cursors not supported by ODBC driver."

Help anyone?
// I added this function to the project:
 
void CReadDBDlg::OnDelete() 
{
		// TODO: Add your control notification handler code here
	CDatabase database;
	CString SqlString;
	CString sCatID, sCategory;
	CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
	CString sDsn;
	CString sFile = "d:\\works\\ReadDB\\Test.mdb";
	// You must change above path if it's different
	int iRec = 0; 	
	
	// Build ODBC connection string
	sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
 
	// Open the database
	if(!database.Open(NULL,false,false,sDsn))
		return;
 
	CRecordset rsStudentSet(&database);
 
	TRY
	{
		
	// remove student from all the classes
    // the student is enrolled in
 
		if ( !database.BeginTrans( ) )
			return;
 
		
		CString SqlString =  "SELECT CatID, Category "
				"FROM Categories WHERE CatID = 22";
 
		if ( !rsStudentSet.Open(CRecordset::dynamic    , SqlString  , CRecordset::none) )
			return;
 
        while ( !rsStudentSet.IsEOF( ) )
        {
			CString sCatID,sCategory;
 
			rsStudentSet.GetFieldValue("CatID",sCatID);
			rsStudentSet.GetFieldValue("Category",sCategory);
 
 
			AfxMessageBox(sCatID + " - " + sCategory);
 
			if(rsStudentSet.CanUpdate())
			{
				rsStudentSet.Delete( );
				rsStudentSet.MoveNext( );
			}
        }
 
        database.CommitTrans( );
    }
 
    CATCH_ALL(e)
    {
        database.Rollback( );
        return;
    }
    END_CATCH_ALL
 
    rsStudentSet.Close( );
 
    return;
 
}

Open in new window

0
Comment
Question by:kozmo_jonny
  • 4
  • 4
  • 4
12 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 21823077
Well, did you try with Static?

But why ODBC with dotNet? Because that code is dated 2001?

Create a DataTableAdapter using the native driver for OLE db and a DataTable, then all the plumbing for CRUD (create, read, update, delete) is created automatically.

/gustav
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 21823095
CRecordset::dynamic
Isn't that telling it to use a dynamic cursor?  (See the error message).

Change that to another value (static cursor) and see if it works.



ps. There are a complete classes in MFC (CDaoRecordset...) specifically for using access databases in MFC.
0
 
LVL 2

Author Comment

by:kozmo_jonny
ID: 21824139
i tried it with all open options. none work.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 49

Expert Comment

by:Gustav Brock
ID: 21824177
Are you running Visual Studio?

A bunch of examples and learning videos for reading/updating an Access table exist. None of these, I believe, use ODBC.

/gustav
0
 
LVL 2

Author Comment

by:kozmo_jonny
ID: 21824213
in my questions you can see my whole project.

http://www.codeproject.com/KB/database/readdb.aspx

i'm using VS6
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 21824309
>>i tried it with all open options. none work.

Do you get other error messages?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 21824319
0
 
LVL 2

Author Comment

by:kozmo_jonny
ID: 21824408
I tried them all :-) none work my friend.

Option: rsStudentSet.Open(CRecordset::dynamic
Exception: {"Dynamic cursors not supported by ODBC driver."}

Option: rsStudentSet.Open(CRecordset::dynaset
Exception: {"ODBC driver does not support dynasets."}

Option: rsStudentSet.Open(CRecordset::snapshot
Exception: {"No columns were bound prior to calling SQLFetchScroll/SQLExtendedFetch"}

help me :'(
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 21824451
?
As I said earlier - any reason you don't want to use the MFC classes specifically for access databases?
0
 
LVL 2

Author Comment

by:kozmo_jonny
ID: 21824488
no reason, it's the only sample project i found that uses MFC & MSACCESS.

I have a project in MFC (VS6) that I need to add MS ACCESS capabilities. so i started this as a small test case for me.

If you have another example that works i would love to see it.

10x
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 21826285
I haven't worked with MFC but I noticed these samples - in C++ but might be helpful anyway:

http://msdn.microsoft.com/en-us/library/aa728901(VS.71).aspx

/gustav
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 21828761
Look up CDaoDatabase, CDaoRecordset and other classes that begin with CDao in the help files.  There should be code snippets there.

There is at least one sample application and code that comes with VS6 that deals specifically with these classes.  (MFC and samples in the help file).
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
A Function to parse a text string 4 36
deduplicating based on criteria 2 21
Create macro from runcode 30 26
MS Access 2010 Close Form  Event - Stop Form Closing 4 28
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

803 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