Create Access data source name dynamically

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


  • cXiwwH NI HZ VwK Kyfe XH

    Posted by TJXckuLtHs on 02/17/2013 01:27pm

    order tramadol online tramadol withdrawal for dogs - tramadol withdrawal long

  • Goal

    Posted by snareenactina on 11/02/2012 04:44pm

    France is one of Europe’s biggest players in the Information and Communications Technologies (ICT) segment because of its large pool of skilled workforce and a strong proclivity for innovation. alcippe This does not mean that equilibrating forces do not exist. It means only that we must consider open ended processes in markets. This is not a new proposition. Adam Smith, the founder of economics as a distinct discipline, thought in evolutionary and process orientated terms. vivienne spiked warrant noble stolem

  • Same solution using a pointer

    Posted by Legacy on 12/15/2002 12: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 12: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 12: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 12: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 12: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 12: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 12: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 12: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

  • Loading, Please Wait ...

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

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

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