Solved

Opening an MS Access Database within a Delphi application

Posted on 2006-12-01
7
583 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

11 Experts available now in Live!

Get 1:1 Help Now