Solved

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

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

Martin
0
Comment
Question by:MARTIN030397
  • 5
  • 4
  • 2
  • +1
12 Comments
 

Author Comment

by:MARTIN030397
Comment Utility
Edited text of question
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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.

var
  DBEngine, DB, Rs: Variant;
begin
  // 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);
  ...
  Rs.Close;
  DB.Close;
end;

Oh yeah, the following constants might help:
const
      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...
0
 

Author Comment

by:MARTIN030397
Comment Utility
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
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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...)
0
 

Author Comment

by:MARTIN030397
Comment Utility
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
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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...
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:MARTIN030397
Comment Utility
Yes Access 2 is 16 bit.   So i use ODBC ? how, refering to my orrigainl spec.

Martin
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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.
Dave
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
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;
0
 
LVL 5

Expert Comment

by:ronit051397
Comment Utility
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!

REQUIRED DRIVERS:
      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! }
0
 

Author Comment

by:MARTIN030397
Comment Utility
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

Martin.
0
 
LVL 5

Accepted Solution

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

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now