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

Posted on 1997-09-15
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 50 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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