We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Accessing Access in Delphi 2, (How? / Best Way)

MARTIN030397 asked
Medium Priority
Last Modified: 2010-04-04
What is the Best way to get access to Access, BDE, OLE, third party links?

I would preferably like to know the easyest and cheapest way, without third party tools.  I believe it is possible with Delphi 3 but what abaout Delphi 2?

Watch Question


Edited text of question
It depends what you want to do with access.  If you want to link it to a grid, you've got a bit of work, but if you just want to create a dynaset or open a table it's quite easy with OLE.

The BDE in Delphi 3 links to Access, but not in Delphi 2.  The following code will work in Delphi 2 & 3.

  DBEngine, DB, Rs: Variant;
  // For Access 95:
  DBEngine := CreateOLEObject('DAO.DBEngine');
  // For Access 97:
  DBEngine := CreateOLEObject('DAO.DBEngine.35');
  DB := DBEngine.OpenDatabase('C:\MyDB.mdb');
  Rs := DB.OpenRecordset('SELECT * FROM Main', dbOpenDynaset);

Oh yeah, the following constants might help:
      dbFreeLocks = 1;

      dbOpenTable = 1;
      dbOpenDynaset = 2;
      dbOpenSnapshot = 4;

      dbDenyWrite = 1;
      dbDenyRead = 2;
      dbReadOnly = 4;
      dbAppendOnly = 8;
      dbInconsistent = 16;
      dbConsistent = 32;
      dbSQLPassThrough = 64;
      dbForwardOnly = 256;
      dbSeeChanges = 512;

      dbUseJet = 2;            // Used in CreateWorkspace.

      // Field types.
      dbBoolean = 1;
      dbByte = 2;
      dbInteger = 3;
      dbLong = 4;
      dbCurrency = 5;
      dbSingle = 6;
      dbDouble = 7;
      dbDate = 8;
      dbText = 10;
      dbLongBinary = 11;
      dbMemo = 12;

      // Field attributes.
      dbAutoIncrField = 16;

If you try to open an Access database on a PC without Access installed there are all sorts of problems with licences!!  I have Microsoft-approved registry licencing code if you need it...


Thanks for your help but when i run your code, (which i add to a button), i get a message OLE error 8004112 on the first line createOLEObject.  am i missing something?

yours thankfully martin
I gather you're using Delphi 2.  What version of Access are you using?

You may need to run RegSvr32.exe on the DAO dll file.  This file is in C:\Program Files\Common Files\Microsoft Shared\Dao.  The file name depends on the version of MS Access.

Have you got Access installed on the PC you're working on?  (It may sound like a stupid question, but we've had similar problems before...)


I am using Access version 2.  it is installed on  my machine although i don not have a regsvr32.exe in the directory specified i do have it in the c:\windows\system path also with regsvr.exe
Sorry, what I meant was that the DAO file should be in C:\Program Files\...

The actual problem is that you need a 32-bit version of Access (i.e. Access 95 or 97), and I think that Access 2 is 16-bit.

If this is so, then ODBC is what you must consider...


Yes Access 2 is 16 bit.   So i use ODBC ? how, refering to my orrigainl spec.

I haven't used ODBC for years, but you have to set up an alias in ODBC (in Control Panel) for your specific database.  You then connect, from Delphi, to this alias.

Bear in mind that it's a pain in the neck, and ODBC was never said to be very quick...

Let me know how you get along.

Hi Martin,

If you just want an example of how to make a connection to an access database, i can send you an example of it. Although I don't know if that is what you want, because you are talking about OLE and ... But reading the comment of JimBob on the otherhand, it's just what I can give you. Connection through ODBC.

If that's what you want and haven't found out, yet. Give me a sign. And I'll submit an example.

Have fun,
c.u. ZifNab;
This is from Lloyd's help file, maybe it will help:

Q:  How do I connect to Access via ODBC?

A:  Alright gang.  Here is my most straight forward explanation of how to  connect your Delphi application to an Access 2.0 database.  With this  method, I have connected to Access 2.0 databases, and been able to read and  write records to my hearts content.  NOTE: You MUST have the proper ODBC  drivers.  If you don't, this will not work!

      ODBCJT16.DLL            dated 11/18/94 or later
      ODBCINST.DLL            dated 08/17/94 or later
      ODBCCTL16.DLL        dated 11/18/94 or later
      MSAJT200.DLL            dated 11/18/94 or later
      MSJETERR.DLL         dated 11/18/94 or later
      MSJETINT.DLL            dated 11/18/94 or later

To the best of my knowledge, these are all of the drivers that are above and  beyond your base ODBC drivers.  You should be able to obtain these from MS  on their FTP/WWW site, however I do not know exact directories.  My company  is a MS Solutions Provider, and we obtained these drivers on one of the many  CDs we received from MS.

Now for the meat of this posting:

To access a database via ODBC, you must first create an ODBC connection to  the database.
   1) Open Control Panel, and then select ODBC.
   2) When the Data Sources dialog appears, select Add.
   3) If you have installed all of the drivers properly, you should see the  following Access drivers:
         Access Data (*.mdb)
         Access Files(*.mdb)
         Microsoft Access Driver (*.mdb)
   4) The one you MUST choose is the third one, Microsoft Access Driver.   Choosing any of the others will not work.
   5) You will then be presented with the ODBC Microsoft Access 2.0 Setup  dialog.  Fill this in with the information regarding the database to which  you wish to connect.
   6) Save the settings and then exit all the way out of Control Panel.

After having made a proper connection to the database with ODBC, the rest is  quite simple.
   7) Open BDE Config.
   8) Choose New ODBC Driver.
   9) Give your SQL link a name
   10) From the Default ODBC Driver combo box, you must choose the Microsoft  Access Driver, just the same as you did in the ODBC setup.  Do not choose  Access Data/Files drivers.
   11) If everything is setup properly, the name of your database should  appear in the Default Data Source Name combo box.  Select it now.
   12) Select OK, you will be back to the main dialog for BDE Config.   Select the aliases tab.
   13) Create a new alias, using the SQL link that you just created (it will  start with ODBC_) as your Alias Type.

   14) Now save and exit from BDE Config.

You should now be able to get to you Access 2.0 database with both read and  write instructions.

And finally...
I must say that having got to Access, via ODBC, you will probably wish you  hadn't.  It is sssllllooowww.  Anytime we start adding layers, it gets  slower and slower.  Normally, I would recommend staying away from VB like
the plague (I'm an old C++ hacker of many years, and the term BASIC makes me  shudder).  However, at this point in time, the best way to use Access  databases is through either Access or VB.  Microsoft is very close-mouthed  about the structure of Access databases, and there is no indication that  they are planning to change ("You either use VB/Access or we'll make you pay  through the ODBC monster!").  Sorry, I digress.  My recommendation, if you  absolutely MUST use Access, then use VB.  Otherwise, migrate your database  to a more open system (Paradox,dBase/FoxPro,anything you can get a native  engine), and use Delphi.

Lance Leverich

{ This code came from Lloyd's help file! }


this is just what i required.  thankyou very much.  Do you have ODBCCTL16.DLL per chance as it is the only one i am missing.

Thanks once again

can you add sonmthing soi that i can give you the points.  i do not seem to be able to get into mark / give you your points

I think Access2.0 includes the ODBCCTL16.DLL, if not then you can contact Microsoft and ask them how to get this file.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.