Solved

Create MS Access Query from within Delphi

Posted on 2004-08-11
13
492 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 127
Delphi application Soap connection 5 96
Reconfigure Delphi Install? 2 47
Is anyone willing to have a look through this code and help debug? 25 61
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

20 Experts available now in Live!

Get 1:1 Help Now