Virtual Developer Workshop: Containerized Development with Docker

This sample in this article was created using VC 5.0. The necessary .h and .lib files are present in the zip.

To create a DSN at run time you could use the SQLConfigDataSource API. Internally this information is stored in the registry. The syntax is attached below

SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0","DSN=TestDB\0DBQ=D:\\Database\\Friends.mdb\0DEFAULTDIR=D:\\DATABASE\0\0");

The problem is that if you want to accept the values from the user or set these values at run time by passing CString or char* will not work. This is because, when sprintf encounters a /0 it assumes it is the end of the string and ignores the rest of the data.

As a workaround you have to use the below mentioned code.

The following code places : where a /0 is expected and there is a loop which replaces a ":" with "/0". You will not be in a position to use sprintf because, when it encounters a /0 it assumes it is the end of the string and ignores the rest of the data.

char* szDesc;

int mlen;

szDesc=new char[256];

sprintf(szDesc,"DSN=%s: DESCRIPTION=TOC support source: DBQ=%s: FIL=MicrosoftAccess: DEFAULTDIR=D:\\Database:: ","TestDB","D:\\Friends.mdb");

mlen = strlen(szDesc);

for (int i=0; i<mlen; i++)


if (szDesc[i] == ':')

szDesc[i] = '\0';


if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0",(LPCSTR)szDesc))




Download ZIP - 48KB


  • Same solution using a pointer

    Posted by Legacy on 12/15/2002 08:00am

    Originally posted by: Juan Subotich

    A slightly optimized solution using a pointer, and changing
    the ':' character by '#' because of posible conflicts if
    it is used in de path name of the database :

    char buffer [256];


    char * p=buffer;

    while(*p != '\0')
    if(*p == '#')

  • abou access connectivity

    Posted by Legacy on 09/07/2002 07:00am

    Originally posted by: sachin

    this is sachin
    and i want to connect database as access
    but it gives error so tell me the proper way to connect it

  • Java-Access combination

    Posted by Legacy on 06/12/2002 07:00am

    Originally posted by: sanjay

    this is sanjay
    presently i am doing a project on java which has backend as instant db but i want to replace it with access and things should be done dynamically.if any help plz mail me asap
    thank u.

  • The Same Problem Of UID,PWD

    Posted by Legacy on 05/26/2002 07:00am

    Originally posted by: Noura

    The Same Problem Of UID,PWD

    I need My client to connect to sql server using ODBC using the function SQLConfigDataSource every thing is OK but I can't specify user and password for this connection ...
    When not writting UID,PWD attributes ,An authentication Dialog box displayed for asking the user to verify the user and password....and it's not desired to display such a message...
    I need Help....PLZ

  • Adding UID and PWD fails

    Posted by Legacy on 04/11/2002 07:00am

    Originally posted by: Rajesh

    When I create a DSN for SQL Server everything works fine, except a UID and PWD is specified. If they are not specified registration succeeds using Win NT Login as the Login for SQL Server. Has anybody tried this?

  • Creating MDB with password Dynamically

    Posted by Legacy on 02/15/2002 08:00am

    Originally posted by: nagendra

    i 'am able to create a mdb dynamically using CREATE_DB in SQLConfigDataSource
    can any one help me create mdb with password dynamically

  • How can i see a report from access in VC++ using ADO?

    Posted by Legacy on 01/20/2002 08:00am

    Originally posted by: Nuno Rapaz

    How can i see a report from access in VC++ using ADO?
    I don't no very well how reports do, but what i want is to execute querys from VC++ in reports from access.
    I can do it in tables but i don't no if it's possible to do it in reports.


  • Ms Access 2000 Data Types

    Posted by Legacy on 10/29/2001 08:00am

    Originally posted by: Suji

    This is Suji
    I am Using MS Access 2000 as Backend for Java Applications.
    If you have any idea on Ms Access DataTypes those will be used in Java Programming pl tell me.
    Or if u found any web page contains brief Description about Ms Access 2000 Datatypes pl suggest me

  • Don't use ':' !!!!

    Posted by Legacy on 02/04/2001 08:00am

    Originally posted by: D Sole

    I know that this was mentioned in other comments, but I wanted to have it in the title, so it would be noticed better.

    Don't use the ':' character , when replacing with '\0', it will replace the ':''s in your paths to the database.

    Use something else, such as $ which cannot be in a path.

  • something is overlooked

    Posted by Legacy on 11/20/2000 08:00am

    Originally posted by: Prashant Jani

    the code works perfect.
    however there was something overlooked. the ':' is being replaced by a '\0'.
    the path to databse may contain a ':'. this will also be replaced by a '\0'. thus the path would not be specified correctly.
    here the change has to be made. replace the':' by any other char which is not likely to be included in the database path.
    i am giving the modified version:

    char* szDesc;
    int mlen;

    szDesc=new char[256];
    sprintf(szDesc,"DSN=%s$ DESCRIPTION=Client data$ DBQ=%s$FIL=MicrosoftAccess$ :: ","Client",argv[1]);
    mlen = strlen(szDesc);
    for (int i=0; i<mlen; i++)
    if (szDesc[i] == '$') szDesc[i] = '\0';
    if (FALSE ==SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0",(LPCSTR)szDesc))
    printf("DSN Creation Failure");
    // code was run from cmd prompt
    printf("DSN Creation Successful");

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date