unable to open MS-Access DB on QT Application



  • i have a simple program in qt and what is this program do is to open\read a microsoft access datebase (.mdb or .accdb) and display the db tables.
    however my problem is that i cant even open the db at the first place
    i have db located in the c drive (C:\Qt_Project\Database1.accdb)
    in the (.pro) file i have included

    QT       += core gui sql
    

    included libraries are:

    #include <QSqlDatabase>
    #include <QDebug>
    #include <QSqlError>
    

    my code is:

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","AccessDB");
    db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=C://Qt_Project//Database1.accdb");
    if(db.open())
      qDebug() << "oK";
    else
      qDebug() << db.lastError().text();
    

    and finally my output is

     "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect"
    

    i've goggled the web about this problem i'm facing and maybe "not sure" it's because of the registry because i usually use a cleaner to clean my pc which is cleaning registry also

    when i use the code

    bool ok = QSqlDatabase::isDriverAvailable("QODBC");
    

    it retures

    true
    

    that means there is no problems with the driver

    please help , thanks u



  • Hi, double forward slashes I think confuses Windows, try backward slashes instead:

    db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=C:\\Qt_Project\\Database1.accdb");
    


  • hello @hskoglund i did what you say and this was the output

    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect"
    


  • Hmm, try simplifying by skipping the FILxx stuff::

    db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=C:\\Qt_Project\\Database1.accdb");
    

    Also, check that you have MS-Access installed on your computer and that you can open C:\Qt_Project\Database1.accdb from MS-Access.



  • @mustafa
    Please check this:

    bool ok=QSqlDatabase::isDriverAvailable("QODBC");
        if(!ok)
        {
            ErrorMsg="No ODBC driver available!";
            return 1;
        }
    


  • @Andy314

    @mustafa wrote in the first post:

    when i use the code
    bool ok = QSqlDatabase::isDriverAvailable("QODBC");

    it retures
    true

    that means there is no problems with the driver



  • @Mustafa
    Apart from trying without the FIL= as @hskoglund has said, is it possible these days you have a 64- vs 32-bit issue with your drivers & Qt & your app? The sort of thing discussed in https://stackoverflow.com/a/25428598/489865 ?

    Alternatively, have you read the same question from a year ago and resolution at https://forum.qt.io/topic/81321/qtsql-data-source-name-not-found/4 ?

    Just a couple of thoughts, no point asking me further about this!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.