Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create MS Access Query from within Delphi

Posted on 2004-08-11
13
Medium Priority
?
533 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:Stef Merlijn
  • 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:Stef Merlijn
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:Stef Merlijn
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:Stef Merlijn
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:Stef Merlijn
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 800 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 1200 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:Stef Merlijn
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

824 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