Sign in to follow this  

Questions on querying MS Access Database w/ MFC C++ App!

This topic is 4107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

-------------------------------------------------------------------------------- I have created a SDI MFC application w/ database support connecting to a MS Access Database. I have found material on how to connect to a database, and how to update an existing record or add a new record. However, I can't find information on the following: 1) how to generate my own queries to the database. Currently it appears that MFC creates with (forward/backwards) controls to move sequentially through the records. I have created edit controls with directly correlation to the database fields which the DoDataExchange function already uses, that I would like the end-user to fill out and use to generate a query to the MS Access Database based on the edit box's filled out, and display them back in a List box. I am more or less concerned with just figuring out how to do a query on the database. I assume it would be with some type of *SELECT statement to the database, but possible through a already pre-existing function in one of the MFC classes (i.e. CDatabase) - but I havent found one. 2) the other thing I would be interested in, is not allowing the program to automatically load the first record fields into the program. Since the edit controls on the application are the same ones used in the (RFX_TEXT) and (DDX_TEXTFIELD) functions of DoDataExchange function of the View and Set classes, it loads the first record into the Edit controls when you first run the application. Any suggestions? I have even tried goggling querying access database with MFC C++ without any hit on any type of search criteria.

Share this post


Link to post
Share on other sites
1.) Not sure if MFC has any built in database libraries, but the standard way of connecting to a database in Win32 is by using the ADO library. There is also a newer version of ADO called ADO .NET built into the .NET Framework. It makes .NET applications very easy to integrate with database solutions. Do a search on MSDN & Google, you should find alot of information on it (Just make sure its information on ADO not ADO .NET since your using MFC). MS Access also has some documentation on ADO with the builtin VBA language. Yes, generally to query information from the database you would use an SQL SELECT statement.

2.) Not sure if you mean that it already is loading the datasets, or when you get them loading you want to prevent data from loading. If your doing a SELECT on the data, just filter out the results. If not, what method are you using to populate the controls right now?

Share this post


Link to post
Share on other sites
Trying looking up OLEDB. You'll probably want to research OLEDB Consumer Templates also. I believe that when using the MFC Wizards to create a database project it creates a set of OLEDB classes for you and automatically hooks them up for you.

Alternatively, the book OLEDB Consumer Templates by Pierre Nallet is an excellent resource for all of the above.

To Billr17: ADO is one way of accessing a database in Windows, but I don't know that I'd call it *the* standard way. ADO is just a data object model that uses OLEDB or ODBC for actual database connectivity.

Share this post


Link to post
Share on other sites
I wouldn't use the built-in MFC stuff and I wouldn't keep a recordset open for a long time like the MFC seems to.

I'd also use SQL 2005 Express as it's better and free.

Doing it as follows with SQL means you can write your programs so they are easy to port to ASP Web applications.

Once you practise it's quite simple to build SQL strings.

To get the first record I'd use "SELECT TOP 1 ID, "... as the first line of your query so you only return one row of data.
then " FROM whatever"
the last line of the query should be "ORDER BY whatever.ID ASC"

The ADO Connection.Execute(SQLQuery) will return a recordset

You can read data in the Recordset, but I'd check that the Recordset.EOF is not true first.

Store the current ID on the page like this ID = oRsRecordset("ID")

Store any other info that you SELECTed to display just like the ID.

Close the recordset!

to navigate forwards create the SQL using a CString
"SELECT TOP 1 ID, "
" FROM whatever"
" WHERE whatever.ID > " + ID
" ORDER BY whatever.ID ASC"

to navigate backwards create the SQL
"SELECT TOP 1 ID, "
" FROM whatever"
" WHERE whatever.ID < " + ID
" ORDER BY whatever.ID DESC"

If the Recordset.EOF is true then you are at the start or the end of the records depending on whether you are navigating forwards or backwards. This works well for multi-users systems and also avoids cursors (yuck).

Share this post


Link to post
Share on other sites

This topic is 4107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this