Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 1997-09-15
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?

Question by:MARTIN030397
  • 5
  • 4
  • 2
  • +1

Author Comment

ID: 1344867
Edited text of question

Expert Comment

ID: 1344868
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...

Author Comment

ID: 1344869
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Expert Comment

ID: 1344870
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...)

Author Comment

ID: 1344871
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

Expert Comment

ID: 1344872
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...

Author Comment

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


Expert Comment

ID: 1344874
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.

Expert Comment

ID: 1344875
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;

Expert Comment

ID: 1344876
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! }

Author Comment

ID: 1344877
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


Accepted Solution

ronit051397 earned 100 total points
ID: 1344878
I think Access2.0 includes the ODBCCTL16.DLL, if not then you can contact Microsoft and ask them how to get this file.


Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question