CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1

    LIKE operator doen't work with MS Access

    I am trying to get data from MS Access database. But my query with LIKE operator doesn't work.
    I am using DSN less connection using DAO SDK.
    My database table is like this:
    EMPID age_category
    (number) (text)
    1 056478
    2 158769
    3 025796
    4 185246


    my query is: select empid from EmpDetails where age_category like '0[1-6]*'

    it doesn't work. it returns 0 rows.

    please see my code:

    #include <iostream>
    #include <rpc.h>
    #include <sql.h>
    #include <sqlext.h>

    using namespace std;
    #define MAXBUFLEN 255

    int main()
    {
    SQLHENV henv = SQL_NULL_HENV;
    SQLHDBC hdbc = SQL_NULL_HDBC;
    SQLHSTMT hstmt= SQL_NULL_HSTMT;
    RETCODE retcode;
    SQLCHAR ConnStrIn[MAXBUFLEN] =
    "DRIVER= {Microsoft Access Driver (*.mdb)};Dbq=c:\\MyDB.mdb";
    SQLCHAR ConnStrOut[MAXBUFLEN];
    SQLSMALLINT cbConnStrOut = 0;
    SQLINTEGER nEMPID, cbEMPID;

    retcode = SQLAllocEnv(&henv);
    retcode = SQLAllocConnect(henv, &hdbc);

    retcode = SQLDriverConnect(hdbc,NULL,ConnStrIn,SQL_NTS,ConnStrOut,MAXBUFLEN,&cbConnStrOut,SQL_DRIVER_NOPROMPT);
    if( retcode != SQL_SUCCESS)
    return 1;

    std::string strQuery="select empid from EmpDetails where age_category like '0[1-6]*'";

    retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
    if( retcode != SQL_SUCCESS)
    return 2;

    retcode = SQLExecDirect(hstmt,(SQLTCHAR *)strQuery.c_str(), SQL_NTS);
    if(( retcode != SQL_SUCCESS) && ( retcode != SQL_SUCCESS_WITH_INFO))
    return 3;

    retcode = SQLBindCol(hstmt, 1, SQL_C_LONG, &nEMPID, 0, &cbEMPID);
    if(retcode == SQL_ERROR)
    return 4;


    while ( SQLFetch(hstmt) != SQL_NO_DATA_FOUND)
    {
    cout << nEMPID << endl;
    }

    return 0;
    }


    please correct me.

  2. #2
    Join Date
    May 2002
    Posts
    11

    Exclamation

    You may want to use MFC and try and catch a CDBException object like to see if an error occours.

    Code:
    try
    {
      /// your code
    }
    catch (CDBException* e)
    {
      MessageBox (e->m_strError);
      e->Delete();
    }
    Also, try to use that select statement in the SQL view in Access directly to see if it returns any rows.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured