Solved

Opening an MS Access Database within a Delphi application

Posted on 2006-12-01
7
594 Views
Last Modified: 2010-04-05
I have a Delphi application which schedules the running of certain Access (2000) databases. Each database has an autoexec macro and performs a variety of functions.

This is running on my work computer though, and so atleast once an hour an MS Access database opens and stops me in the middle of work.

I would like it to open the MS Access databases within a Delphi form, or Delphi element of some sort.

Is this possible? If so, how?
0
Comment
Question by:wbstech
  • 3
  • 3
7 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 18054679
What version of Delphi do you have and what do you want to do with Access.

If you just want to open the access tables and you have an enterprise version of Delphi you can use the ADO components to do this very easily.
Just drop an ADOCOnnection object onto your form and set the connectionstring (via the wizard in the object inspector).
Turn off LoginPrompt if you don't have to actually login to the database.
Now you can just turn the connection to the access database on and use the standard ADOQueries to run against the new connection to your access database.

If this is not what you are looking for them please provide a little more information.

0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 250 total points
ID: 18054711
If you want to run access macros from delphi, here is some code from another PAQ.
First part was from me, second part from rllibby

I think its something like

procedure TForm1.Button1Click(Sender: TObject);
var
  Access: Variant;
begin
  Access := CreateOleObject('Access.Application');
  try
    Access.OpenCurrentDatabase('c:\db1.mdb');
    Access.DoCmd.RunMacro('Macro1');
  finally
    Access.Quit;
  end;
end;

Make sure the office server wrapper is installed in your delphi
componnets>install packages

Also not completely sure about the call to the macro, might be
AccessApplication1.DoCmd.RunMacro('Macro1',1,1);
 



Edit1 = name of your first edit box holding x:\path\msaccess.exe
Edit2 = name of your second edit box holding x:\path\database.mdb
MacroName = the name of your macro to run

  WinExec(PChar('"'+Edit1.Text+'" "'+Edit2.Text+'" /XMacroName'), SW_SHOWNORMAL);
0
 
LVL 4

Author Comment

by:wbstech
ID: 18054714
Ah should have mentioned, i'm on the ancient Delphi 4.

Each database being opened is running macros and queries of no specific type.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:wbstech
ID: 18054721
Would that above code function if I ran the autoexec macro, which in-turn runs multiple macros and queries? That would work for me.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 18054888
You need to make sure that the office server wrapper is installed first.
You could test the calling of the macros then, but its been a long time since I was using anything near delphi 4 so I couldnt guarentee. All i can suggest is try it out.
0
 
LVL 9

Assisted Solution

by:bernani
bernani earned 250 total points
ID: 18060201
Hi,

you can also try this if it helps:

{create a test.mdb database}
{add a test formular called Customers }
{add a second formular called Menu}
{Add a Macro called Autoexec wich call Macro1}
{Add a Macro1 wich opens the formular called "Customers" and which call Macro2}
{Add a Macro2 wich opens the formular Menu}
{Don't forget to change if needed the Security access for your macros: Tools->Security->... }

var
  Form1: TForm1;
  Access: Variant;     // Add this

implementation
 
{$R *.DFM}

Uses Comobj{for CreateOleObject};              // Add this

procedure TForm1.Button1Click(Sender: TObject);
begin
Access := CreateOleObject('Access.Application');
try
{Opens the database file with full qualified name, even in the same folder as your application}


  Access.OpenCurrentDatabase('C:\test.mdb');
  Access.DoCmd.RunMacro('Autoexec'); // execute Autoexec (wich invoke Macro1, display form Customers) which in turn execute macro2 (display form Menu).

//show Access application
  Access.visible:= true;                    // Add this
  finally
  {}
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Access.Quit;                                 //
Access:= unassigned;                   // Add this  
end;

0
 
LVL 4

Author Comment

by:wbstech
ID: 18301366
Thanks, both solutions helped me get to my final code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

896 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

18 Experts available now in Live!

Get 1:1 Help Now