INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Connecting to access database

Connecting to access database

(OP)
For some reason I have all these header file errors but not sure why, here's my code:

CODE

#include "stdafx.h"
#include <sqlext.h>
#include <iostream>
#include <string>

using namespace std;

char szDSN[256] = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\Users\\Sasuke\\Documents\\Database1.accdb;";

//LPCTSTR lpszConnect = _T("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\Users\\Sasuke\\Documents\\Database1.accdb;");


//*****************************************************************************************

int main()
{
/* Data Access Method used in this sample */
const char* DAM = "Direct ODBC";

HENV    hEnv;
HDBC    hDbc;

/* ODBC API return status */
RETCODE rc;

int     iConnStrLength2Ptr;
char    szConnStrOut[256];

//unsigned char* query = "SELECT Customers.[Company], Customers.[First Name] FROM Customers;";

string query = "SELECT Customers.[Company], Customers.[First Name] FROM Customers;";

SQLCHAR         chval1[128], chval2[128], colName[128];
int             ret1;
int             ret2;

/* Number of rows and columns in result set */
SQLINTEGER      rowCount = 0;
SQLSMALLINT     fieldCount = 0, currentField = 0;
HSTMT           hStmt;

/* Allocate an environment handle */
rc = SQLAllocEnv(&hEnv);
/* Allocate a connection handle */
rc = SQLAllocConnect(hEnv, &hDbc);

/* Connect to the 'Northwind 2007.accdb' database */
rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
    SQL_NTS, (unsigned char*)szConnStrOut,
    255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc))
{
    printf("%s: Successfully connected to database. Data source name: \n  %s\n",
       DAM, szConnStrOut);

    /* Prepare SQL query */
    printf("%s: SQL query:\n  %s\n", DAM, query);

    rc = SQLAllocStmt(hDbc,&hStmt);
    rc = SQLPrepare(hStmt, query, SQL_NTS);
   
    /* Bind result set columns to the local buffers */
    rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval1, 128, (SQLINTEGER*)&ret1);
    rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval2, 128, (SQLINTEGER*)&ret2);
   
    /* Excecute the query and create a record set */
    rc = SQLExecute(hStmt);
    if (SQL_SUCCEEDED(rc))
    {
        printf("%s: Retrieve schema info for the given result set:\n", DAM);
        SQLNumResultCols(hStmt, &fieldCount);
        if (fieldCount > 0)
        {
            for (currentField=1; currentField <= fieldCount; currentField++)
            {
                SQLDescribeCol(hStmt, currentField, colName, sizeof(colName), 0, 0, 0, 0, 0);
                printf(" | %s", colName);    
            }
            printf("\n");
        }
        else
        {
            printf("%s: Error: Number of fields in the result set is 0.\n", DAM);
        }

        printf("%s: Fetch the actual data:\n", DAM);
        /* Loop through the rows in the result set */
        rc = SQLFetch(hStmt);
        while (SQL_SUCCEEDED(rc))
        {
            printf(" | %s | %s\n", chval1, chval2);
            rc = SQLFetch(hStmt);
            rowCount++;
        };

        printf("%s: Total Row Count: %d\n", DAM, rowCount);
        rc = SQLFreeStmt(hStmt, SQL_DROP);
    }
}
else
{
    printf("%s: Couldn't connect to %s.\n", DAM, szDSN);
}

/* Disconnect and free up allocated handles */
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

printf("%s: Cleanup. Done.\n", DAM);
}

I tried to use the debugger but still could not manage to fix them. Here are some of the errors

CODE

c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(137): error C2146: syntax error : missing ';' before identifier 'SQLHWND'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(137): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(137): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(275): error C2146: syntax error : missing ';' before identifier 'Data1'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(275): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(275): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(276): error C2146: syntax error : missing ';' before identifier 'Data2'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(276): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(276): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(277): error C2146: syntax error : missing ';' before identifier 'Data3'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(277): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(277): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(278): error C2146: syntax error : missing ';' before identifier 'Data4'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(278): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sqltypes.h(278): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(606): error C2061: syntax error : identifier '__inout_xcount_opt'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(607): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(607): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(711): error C2061: syntax error : identifier '__out_xcount_opt'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(712): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(712): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(729): error C2061: syntax error : identifier '__out_xcount_opt'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(730): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(730): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(735): error C2061: syntax error : identifier '__out_xcount_opt'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(736): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(736): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(747): error C2061: syntax error : identifier '__out_xcount_opt'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(748): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(748): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(764): error C2061: syntax error : identifier '__out_xcount'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(765): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(765): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(770): error C2061: syntax error : identifier '__out_xcount_opt'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(771): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(771): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(780): error C2061: syntax error : identifier '__out_xcount_opt'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(781): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(781): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(804): error C2061: syntax error : identifier '__in_xcount'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(804): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(804): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(828): error C2061: syntax error : identifier '__in_xcount'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(828): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(828): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(845): error C2061: syntax error : identifier '__in_xcount'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(846): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(846): error C2143: syntax error : missing ')' before ';'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sql.h(850): error C2061: syntax error : identifier '__in_xcount'

RE: Connecting to access database

Include <windows.h> before <sqlext.h> (better place this include in stdafx.h if you compile with pre-compiled headers).

The <stdext.h> header uses lots of symbols from <windows.h> (see error messages list;)...
 

RE: Connecting to access database

(OP)
Thanks, I did not think about the windows.h

RE: Connecting to access database

(OP)
well now I have a problem connecting to the database, is there something in this line that is doing it:

CODE

char szDSN[256] = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\Users\\Sasuke\\Documents

or something in the reset of my code that sends to to the bottom else?

RE: Connecting to access database

Luckily (for me), I don't deal with Access now but I venture a remark:

The true problem is: nobody (except you) knows what (and where) is your problem (OS, ODBC configuration and installed drivers, failed statement, return code(s) etc).

Ask good question == have a good chance of a good answer...
 

RE: Connecting to access database

(OP)
well I will try to answer the questions to the best of my ability.

OS: Windows 7
ODBC config: not sure where to look for that, access or Visual C++
installed drivers: not sure where to look
failed statement: not sure but the following line(which I indicated with arrows returns -1

CODE

#include "stdafx.h"
#include <sqlext.h>
#include <iostream>
#include <string>

using namespace std;

char szDSN[256] = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\Users\\Sasuke\\Documents\\Database1.accdb;";

//LPCTSTR lpszConnect = _T("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\Users\\Sasuke\\Documents\\Database1.accdb;");


//*****************************************************************************************

int main()
{
/* Data Access Method used in this sample */
const char* DAM = "Direct ODBC";

HENV    hEnv;
HDBC    hDbc;

/* ODBC API return status */
RETCODE rc;

int     iConnStrLength2Ptr;
char    szConnStrOut[256];

//unsigned char* query = "SELECT Customers.[Company], Customers.[First Name] FROM Customers;";

string query = "SELECT Customers.[Company], Customers.[First Name] FROM Customers;";

SQLCHAR         chval1[128], chval2[128], colName[128];
int             ret1;
int             ret2;

/* Number of rows and columns in result set */
SQLINTEGER      rowCount = 0;
SQLSMALLINT     fieldCount = 0, currentField = 0;
HSTMT           hStmt;

/* Allocate an environment handle */
rc = SQLAllocEnv(&hEnv);
/* Allocate a connection handle */
rc = SQLAllocConnect(hEnv, &hDbc);

/* Connect to the 'Northwind 2007.accdb' database */
rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
    SQL_NTS, (unsigned char*)szConnStrOut,
    255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
->->if (SQL_SUCCEEDED(rc))
{
    printf("%s: Successfully connected to database. Data source name: \n  %s\n",
       DAM, szConnStrOut);

    /* Prepare SQL query */
    printf("%s: SQL query:\n  %s\n", DAM, query);

    rc = SQLAllocStmt(hDbc,&hStmt);
    rc = SQLPrepare(hStmt, query, SQL_NTS);
   
    /* Bind result set columns to the local buffers */
    rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval1, 128, (SQLINTEGER*)&ret1);
    rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval2, 128, (SQLINTEGER*)&ret2);
   
    /* Excecute the query and create a record set */
    rc = SQLExecute(hStmt);
    if (SQL_SUCCEEDED(rc))
    {
        printf("%s: Retrieve schema info for the given result set:\n", DAM);
        SQLNumResultCols(hStmt, &fieldCount);
        if (fieldCount > 0)
        {
            for (currentField=1; currentField <= fieldCount; currentField++)
            {
                SQLDescribeCol(hStmt, currentField, colName, sizeof(colName), 0, 0, 0, 0, 0);
                printf(" | %s", colName);    
            }
            printf("\n");
        }
        else
        {
            printf("%s: Error: Number of fields in the result set is 0.\n", DAM);
        }

        printf("%s: Fetch the actual data:\n", DAM);
        /* Loop through the rows in the result set */
        rc = SQLFetch(hStmt);
        while (SQL_SUCCEEDED(rc))
        {
            printf(" | %s | %s\n", chval1, chval2);
            rc = SQLFetch(hStmt);
            rowCount++;
        };

        printf("%s: Total Row Count: %d\n", DAM, rowCount);
        rc = SQLFreeStmt(hStmt, SQL_DROP);
    }
}
else
{
    printf("%s: Couldn't connect to %s.\n", DAM, szDSN);
}

/* Disconnect and free up allocated handles */
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

printf("%s: Cleanup. Done.\n", DAM);

if you need anymore if please let me know

 

RE: Connecting to access database

(OP)
oh yeah and the error is:

CODE

Direct ODBC: Couldn't connect to Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DSN='';DBQ=C:\Users\User\Documents\Database1.accdb;.

RE: Connecting to access database

Now you have problems with ODBC Access driver (possibly this driver is not installed on your system).

Regrettably, I can't help you with ODBC driver problems on Windows 7 (I'm working on old good XP till now). I know that you are not alone in this wild area. See, for example: http://answers.microsoft.com/en-us/office/forum/office_2007-excel/no-odbc-drivers-available-for-excel-or-access-in/001c234b-dfd5-4378-a325-c4f1482fb6fd
...and lots of other links on this topic from Google...

Ask question on specialized forums or wait ODBC gurus here...

Good luck!
 

RE: Connecting to access database

(OP)
thanks, I will try the link for answers, but the ODBC forums here have not had any questions answered since almost a year ago, not sure what that means but I am also looking on microsoft forums for answers

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close