Parameterised query Delphi

Posted on 2012-08-24
Last Modified: 2012-08-24
I am new to Delphi but it is great for knocking out a quick query from Paradox.
I attach the code on a button which pulls data from 2 tables; Orders and dates.
You will note that the SQL WHERE command is month = 8.
I would like to change this to read the value of an Edit field on the form, so a user can select a month. So month = (Edit1.value).
I have looked at Tparams but my head hurts.
Could anybody point me the way forward, please? Note the month is an accounting month, not  calender month so I cannot play with a date field.
Question by:ClaytonGlass
    LVL 36

    Accepted Solution

    change the query text to accept a parameter first and then fill in the month
    Query2.SQL.Add('SELECT  D.Delivered,  SUM(D.Nett) ' );
     Query2.SQL.Add(' FROM "GOOrders.DB" D, "vvdates.DB" D1  WHERE  (D1.Keydate = D.Delivered)AND D1.Months = :SELMONTH GROUP BY D.Delivered ORDER BY D.Delivered ');
    Query2.ParamByName('SELMONTH').AsInteger := StrToInt(Edit1.Text);
    Series1.DataSource := Query2 as tcomponent;
    series1.XLabelsSource := 'Delivered';
    series1.XValues.ValueSource := 'Delivered';
    series1.yValues.ValueSource := 'Sum of nett';

    Open in new window

    you could
    Query2.ParamByName('SELMONTH').AsString := Edit1.Text;

    but if the edit1 is empty it will not give any records
    and like this it throws an exception

    >> when my head hurts, i use an aspirine

    Author Closing Comment

    I tried aspirin but your solution is much better!
    Thank you very much!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Delphi XE7 Rest Client 2 149
    SIM number for APK using Delphi 10 2 353
    Splash screen too shortlived 11 102
    TcomboBox uninverted item 3 41
    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 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…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    732 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

    23 Experts available now in Live!

    Get 1:1 Help Now