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
Solved

Create MS Access Query from within Delphi

Posted on 2004-08-11
13
502 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: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
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.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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