Solved

Create MS Access Query from within Delphi

Posted on 2004-08-11
13
489 Views
Last Modified: 2010-04-05
Hi,

Is it possible to create a Query in MS Access from within Delphi.

The SQL statement I need is:

UPDATE Waardebonnen SET Waardebonnen.Status = "Vervallen"
WHERE (((Waardebonnen.GeldigheidsDatum)<Date()));

Please supply with code.
Thanks, Stef
0
Comment
Question by:Delphiwizard
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Hello

   That's easy, Just use AdoQuery and call ExecSql method to execute the sql statmenet, here's the example

  AdoQuery1.SQL.Add('UPDATE Waardebonnen SET Waardebonnen.Status = "Vervallen"');
  AdoQuery1.SQL.Add(' WHERE Waardebonnen.GeldigheidsDatum < :D ');
  AdoQuery1.Parameters.ParamByName('D').Value := Date;
  AdoQuery1.ExecSQL;

HTH

Regards,
Mohammed
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
download a tested example from :

page:        http://www.geocities.com/esoftbg/
  link:        Q_21090790.zip

emil
0
 

Author Comment

by:Delphiwizard
Comment Utility
This is not what I wanted to accomplish.

Just like when I use this statement for creating a TABLE in MS ACCESS

  try
     // Tabel FactuurINHistory toevoegen
      QUpdateDatabase.SQL.Clear;
      QUpdateDatabase.SQL.Text:='CREATE TABLE FactuurINHistory';
      QUpdateDatabase.ExecSQL;
      QUpdateDatabase.SQL.Clear;
      QUpdateDatabase.SQL.Text:='ALTER TABLE FactuurINHistory ADD COLUMN Volgnr COUNTER';
      QUpdateDatabase.ExecSQL;
      QUpdateDatabase.SQL.Clear;
      QUpdateDatabase.SQL.Text:='ALTER TABLE FactuurINHistory ADD CONSTRAINT Volgnr PRIMARY KEY (Volgnr)';
      QUpdateDatabase.ExecSQL;
      QUpdateDatabase.SQL.Clear;
      QUpdateDatabase.SQL.Text:='ALTER TABLE FactuurINHistory ADD COLUMN Factuurnr Integer';
      QUpdateDatabase.ExecSQL;
      QUpdateDatabase.SQL.Clear;
      QUpdateDatabase.SQL.Text:='ALTER TABLE FactuurINHistory ADD COLUMN Datum     DATETIME';
      QUpdateDatabase.ExecSQL;
      QUpdateDatabase.SQL.Clear;
      QUpdateDatabase.SQL.Text:='ALTER TABLE FactuurINHistory ADD COLUMN Omschrijving  Text(50) with compression';
      QUpdateDatabase.ExecSQL;
      QUpdateDatabase.SQL.Clear;
      QUpdateDatabase.SQL.Text:='ALTER TABLE FactuurINHistory ADD COLUMN Bedrag               FLOAT';
      QUpdateDatabase.ExecSQL;
    Finally
      QUpdateDatabase.Free;
    end;

Now I don't want to create a TABLE but a QUERY (based on mentioned SQL-statement).

Stef
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
Try with begin - end block. I am not familiar with Access and I don't know if it support it...

BEGIN
  CREATE TABLE FactuurINHistory;
  ALTER TABLE FactuurINHistory ADD COLUMN Volgnr COUNTER;
  ...
END;
0
 

Author Comment

by:Delphiwizard
Comment Utility
Mohammed,
Your solution does work when I do it like this.

  With QADOQuery1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('UPDATE Waardebonnen SET Waardebonnen.Status = "Vervallen"');
    SQL.Add(' WHERE Waardebonnen.GeldigheidsDatum < :Datum ');
    Parameters.ParamByName('Datum').DataType := ftDate;                              << added
    Parameters.ParamByName('Datum').Value := Date;
    ExecSQL;
  end;

But then again that is not what I needed. See my second comment.
Stef
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
a super example from :

page:        http://www.geocities.com/esoftbg/
  link:        Q_21090790.zip

1). Creates the DATABASE.MDB if it does not exist;
2). Updates correctly .... Waardebonnen.Status = "Vervallen" ....
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Delphiwizard
Comment Utility
Emil, Your example works great.

BUT

Maybe I'm not clear??
I need a Query inside MS Access, so I can execute it through the ADOCommand control.

  with ADOCommand1 do
  begin
    CommandType := cmdStoredProc;
    CommandText := 'QWaardebonStatusBijwerken';
    Execute;
  end;

Waiting for another SUPER example...   :-)

Stef
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
Hi Delphiwizard,
I did create a new version of the example: with executing an ADOCommand....
(If Database does not exists: it is created....
 if table does not exist: it is created just by executing an ADOCommand)
Please download it from above link.
Best Regards,
Emil
0
 

Author Comment

by:Delphiwizard
Comment Utility
When I looked inside the Database.MDB with MS Access, there is no Query added.

Is it possible to add the query statement:

  UPDATE Waardebonnen SET Waardebonnen.Status = "Vervallen"
  WHERE (((Waardebonnen.GeldigheidsDatum)<Date()));

as a MS Access query (name = QWaardebonStatusBijwerken) into MS Access, so in Delphi I just have to call it like:

  with ADOCommand1 do
  begin
    CommandType := cmdStoredProc;
    CommandText := 'QWaardebonStatusBijwerken';
    Execute;
  end;

With kind regards, Stef
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Hello Stef

Use Create procedure syntex with AdoCommand to create query that saved into MS Access
as following

  AdoCommand1.CommandText := ' Create procedure MyProc as select * from MyTable;
  AdoCommand1.Execute;
end;

HTH

Regards,
Mohammed Nasman
0
 
LVL 22

Accepted Solution

by:
mnasman earned 200 total points
Comment Utility
for your sample it will look like this

  AdoCommand1.CommandText := ' Create procedure MyProc as UPDATE Waardebonnen SET '
                           + ' Waardebonnen.Status = "Vervallen" '
                           + ' WHERE (((Waardebonnen.GeldigheidsDatum)<Date()))';
  AdoCommand1.Execute;

Regards,
Mohammed Nasman
0
 
LVL 12

Assisted Solution

by:esoftbg
esoftbg earned 300 total points
Comment Utility
Hi Delphiwizard,
I used the mnasman's suggestion in my new example....
Please download it from above link.
Best Regards,
Emil
0
 

Author Comment

by:Delphiwizard
Comment Utility
Emil and Mohammed thank you very much.
Emil got some extra points because of his great effort in helping me.
But Mohammed came with the solution.

Stef
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 226
ADO Memory leak with DELPHI 2007 37 153
Delphi OLE Error 8 82
Using idhttp to login to instagram 2 63
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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

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

11 Experts available now in Live!

Get 1:1 Help Now