Solved

Create MS Access Query from within Delphi

Posted on 2004-08-11
13
512 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
[X]
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
  • 3
  • +1
13 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11779563
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
ID: 11779811
download a tested example from :

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

emil
0
 

Author Comment

by:Delphiwizard
ID: 11780895
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
Independent Software Vendors: 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!

 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11781086
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
ID: 11781253
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
ID: 11781700
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
 

Author Comment

by:Delphiwizard
ID: 11781818
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
ID: 11784922
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
ID: 11790938
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:Mohammed Nasman
ID: 11799132
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:
Mohammed Nasman earned 200 total points
ID: 11799139
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
ID: 11799773
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
ID: 11801020
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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