Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading MS Access from Visual C++

Status
Not open for further replies.

phadobas

Technical User
Jul 30, 2005
607
0
16
US
I'm trying to write a very simple program that requires reading some data from MS Access. I'm doing it in Visual Studio C++. Essentially what I need to create is a console application doing this:

cout "Dear customer, please enter your account number";
get account number
...
Some code that reads the balance from an MS Access table based on the entered account number
...
cout "your balance is" + balance + ". Bye";

For the life of me, I can't get the middle part figured out. I'm new to c++ but all other parts of the program are working.
I can go to Server Explorer, and add a connection to the MS Access database. I can see the tables, I can see the data in the tables.
But how do I write some code that gets data from MS Access?

I've found many example codes elsewhere, and one for one, they fail with some missing external link or reference, or undefined variables, and those errors are all way over my head.

I can't find some book or tutorial that walks me through, as a beginner, from knowing almost nothing, to the point of being able to do the above.

Can anyone help with this?
 
Easiest way is to use ODBC.

For the connection string, Start => Windows Administrative Tools => ODBC Data Source (either 32 or 64 bit, depending on what you are using). Click on the Drivers tab. There will be a while bunch of names (some in Spanish). Look for the one with Microsoft Access Driver.

You could also have a look at MS Access Code Example
 
Ok thank you. I've done the first part where I defined ODBC Data source under Control Panel -> Administrative tools -> ODBC, and even tested the connection there and was successful.

It's just I have seen a single "example" code that I was able to run without a bunch of errors that I'm unable to debug. Your referenced example code is new, so I'll definitely give it a go.
 
Why are you unable to debug that code? Could you post the site where you got the code from and say why you can't debug it?
 
Ok, I tried this link in MS Visual Studio 2022 Community Edition:


While the actual example doesn't show, I've added:
#include <sql.h>
#include <sqlext.h>
#inlcude <iostream>
#include <string>
#include <Windows.h>

There is a line there in the code:

***
SQLDriverConnect(handleConnection, NULL, (SQLWCHAR*)TEXT("DRIVER={SQL Native Client};SERVER=MyComputerName;DATABASE=MyDatabase;Trusted_Connection=yes;"), SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT)
***

When I just copy/paste it as it is, the "SQL_NTS" is underlined red, indicating there is an error. It says there are too many arguments in there. So I take it out, and I have to take all the succeeding arguments out for the red error indictation to go away. I'm left with this:

******
SQLDriverConnect(handleConnection, NULL, (SQLWCHAR*)TEXT("DRIVER={SQL Native Client};SERVER=MyComputerName;DATABASE=MyDatabase;Trusted_Connection=yes;"));
******

With this code, all the red-line arrors disappear, but when I try to compile, I get
"Error count exceeds 100; stopping compilation"

The first error is "error C4430: missing type specifer - int assumed. Note: C++ does not support default-int".
And there is a ton of it later.

So that's where I'm now and I'm stuck. Any idea?
 
That is for SQL Server: not Access. Are you still trying to connect to Access or have you changed to SQL Server Express or full blown SQL Server?
 
I'm still trying to connect to MS Access
 
Thank you for the suggestion. In the meantime, I came accross this:

It gave a set of libraries that I was able to use.

I'll read up on your refereced material also.
 
I could build almost all the MS examples except for the MFC ODBC one. You just need to add a main surrounding all the code. Some of them need a definition of DAM and the HRESULT hr declaration. They all need either a stdio.h or iostream include.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top