Board index » delphi » Creating an .mdb database via Delphi 5 and ADO

Creating an .mdb database via Delphi 5 and ADO

Hello,

Does anybody know how I can create an .mdb access database (via code) using
Delphi 5 and ADO?

Thanks in advance.

Richard Mace

Richa...@mhsoftware.co.uk

 

Re:Creating an .mdb database via Delphi 5 and ADO


I know of no way to create the actual MDB file without
using either Jet or the ODBC drivers. To do it from
Delphi using code is quite simple (once you know
how). I posted this in the inprise ng's a few weeks
back.

Apologies for the multiple appendages, but they are
relevant if you want it to work properly. The key to it
all is to access the ODBCCP32.DLL and the function
SQLConfigDataSource(...) contained therein. The
various Jet (Access MDB) functions supported are
outlined in the MSDN. Search for CREATE_DB
and you should find the shortest list....

HTH

----------------------------------------------------------------------------
---------------
(original post)

A few little points (not immediately apparent from my original post).

1.    You *NEED* the "DBQ=<path+filename>" entry, that specifies
        the  actual MDB file to create. That's why your call is failing.
2.    "Exclusive=1" is optional (I needed a forced-single-user system)
3.    I've found that this *seems* to work best if you make the double
       call as I had in the original code to first create the DSN then
       create the MDB file attached to that DSN.

Cheers

--

-------------------------------------------------------------------
"If you think you know the answer,
then you don't understand the question !"
-------------------------------------------------------------------

Aksel De Meester <aksel.demees...@teleatlas.com> wrote in message
news:38622945.65AEB0F4@teleatlas.com...

Quote
> Hi,

> I tried this code. But I always get a return false.

> There is a thing I don't understand: in the ODBC Microsoft desktop
Database
> Drivers Help, one finds parameter info for the function

SQLConfigDatasource. The
Quote
> most important
> parameter to me seems the name of the datasource itself, but this one is
not
> mentioned in the help file. There is the parameter DSN though, which seems
to
> fulfil this
> function...

> Here my trial of his code:

>    function SQLConfigDataSource(Hwnd:
> THandle;State:Byte;Driver:PChar;Attributes:PChar): Boolean; stdcall;
>    external 'ODBCCP32.DLL' index 4;

{---------------------------------------------------------------------------
---}
Quote

> procedure TForm1.Button1Click(Sender: TObject);

>  var
>    fResult:   Boolean;
>    Attribs    : String;
>    strFile    : String;

>  begin
>    strFile := 'c:\testdb.mdb';

>    Attribs := Format( 'DSN=testje'+#0+
>                       'CREATE_DB=%s'+#0+
>                       'Exclusive=1'+#0+
>                       'Description=Tooltime Toys'+#0+#0,
>                       [strFile] );

>    fResult := SQLConfigDataSource(0,ODBC_ADD_DSN,'Microsoft Access Driver
> (*.mdb)',PChar(Attribs));
>    if( fResult = false ) then ShowMessage( 'Create DSN (Datasource)
failed!' );

>  end;

> fResult always is false.

> thanks for your help,

> Aksel

> Zorallin wrote:

> > Hi,
> >     You need the ODBCCP32.DLL (ODBC Administrator DLL) and
> > access to the SQLConfigDataSource(...) function in that DLL.
> > Below is a snippet from the beginning of part of an app I'm playing
> > with at the moment. (I usually don't post code because my boss gets
> > "strange" about it...) It's been "modified" to protect the guilty ;-)
Note
> > that it will auto-create BOTH the DSN and the MDB forcibly in the
> > current home directory.

> > A special note. You *should* ask the ODBC administrator what drivers
> > are available and use the returned string. I took a short-cut and hard-
> > coded the "Microsoft Access Driver (*.mdb)". That's the reason it
> > is assigned to a variable, so I can put the search-and-load function
> > in later....  ie: It's a work in progress, be gentle with it...

> > HTH

> --------------------------------------------------------------------------
--
> > -------------

> > procedure Form1.FormCreate(Sender: TObject);
> > var
> >   pFn: TSQLConfigDataSource;
> >   hLib: LongWord;
> >   strDriver: string;
> >   strHome: string;
> >   strAttr: string;
> >   strFile: string;
> >   fResult: BOOL;
> >   ModName: array[0..MAX_PATH] of Char;
> >   iTempVar: integer;
> >   tstrTables: TStringList;
> >   dADOCommand1: TdADOCommand;
> >   Reg: TRegistry;
> >   srInfo : TSearchRec;
> > begin
> >   fIsMaster := false;
> >   Windows.GetModuleFileName( HInstance, ModName, SizeOf(ModName) );
> >   strHome := ModName;
> >   while ( strHome[length(strHome)] <> '\' ) do
> >     Delete( strHome, length(strHome), 1 );
> >   strFile := strHome + 'TOOLTOYS.MDB';
> >   hLib := LoadLibrary( 'ODBCCP32' );    // load from default path
> >   if( hLib <> NULL ) then
> >   begin
> >     @pFn := GetProcAddress( hLib, 'SQLConfigDataSource' );
> >     if( @pFn <> nil ) then
> >     begin
> >       // force (re-)create DSN
> >       strDriver := 'Microsoft Access Driver (*.mdb)';
> >       strAttr := Format( 'DSN=TOOLTIME'+#0+
> >                          'DBQ=%s'+#0+
> >                          'Exclusive=1'+#0+
> >                          'Description=Tooltime Toys'+#0+#0,
> >                          [strFile] );
> >       fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
> >       if( fResult = false ) then ShowMessage( 'Create DSN (Datasource)
> > failed!' );

> >       // test/create MDB file associated with DSN
> >       if( FindFirst( strFile, 0, srInfo ) <> 0 ) then
> >       begin
> >         strDriver := 'Microsoft Access Driver (*.mdb)';
> >         strAttr := Format( 'DSN=TOOLTIME'+#0+
> >                            'DBQ=%s'+#0+
> >                            'Exclusive=1'+#0+
> >                            'Description=Tooltime Toys'+#0+
> >                            'CREATE_DB="%s"'#0+#0,
> >                            [strFile,strFile] );
> >         fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1],
@strAttr[1] );
> >         if( fResult = false ) then ShowMessage( 'Create MDB (Database
file)
> > failed!' );
> >       end;
> >       FindClose( srInfo );
> > .....
> > (* rest missing, do what you have to for the rest of your form's
creation *)
> > .....
> > end;

> --------------------------------------------------------------------------
--
> > -------------

> > The above is copyright (c) 1999 White slavers Inc. Free for private use
!
> > ;-)))) ROTFL

> > Tom Guarino <programm...@ads-corp.com> wrote in message
> > news:83nvur$g4315@forums.borland.com...
> > > Hi all,

> > >     I am using a DSN for connecting to an ACCESS table for a
standalone
> > > application.

> > >     I am looking to do the INSTALL for it now, and wondering the best
way
> > to
> > > install a DSN.  It is a softwared thing and needs to be automated, I
know
> > > how to set it up using the control panel.  I saw some messages come
> > through
> > > here, but I can't believe that going into the registry and
"hardcoding"
> > the
> > > DSN is the "Official" way of doing it.

> > >     Please respond if you know how to do this during an install, or
better
> > > yet, a "temp" DSN that I can set up when my app is running.

> > > Thanks everyone, and happy holidays...

> > > Tom

Other Threads