SQL Query in Delphi Using an Edit

Hello everyone,

to select a set of records that verify the condition Cod_Fil=('the text of an edit') from a Student table and display them on a DBGRID, I used a Query component whose SQL property contains the following text:
Select all
From Student
Where Student.Cod_fil=:Saisie

where (Saisie) is the name of the parameter holding the value to be entered in (Edit.text) (params property of Query)
the Delphi instruction code is:
Query1.ParamByName('saisie').AsInteger := StrToInt(Edit1.Text);
But at runtime, the debugger notified me of the following exception:
Exception 'EDBEngineError' with the message incorrect use of the keyword Element: From
Line number 2: Process stopped
What should be done to filter a table by entering the value of the field on which we want to make the selection?

12 answers

  1. Hello

    I believe I am quite capable of solving this parameterized query problem. Because in my database, 90% of my queries are all of this kind.
    Time is running out. If I remember tomorrow, I will connect and give you the answer to your problem.

    Good luck!
    3
    1. Je suis désolé, je ne peux pas vous aider avec ça.
      2
      1. Hello

        I am here to provide a solution to your problem:
        what I ask is that you detail the structure of each table that constitutes the query (at least the important fields and their type: Numeric, Alphabetical, or Alphanumeric)
        1
        1. Pour créer une requête paramétrée où le paramètre prend les noms de colonnes d'une autre table, vous pouvez utiliser une instruction SQL avec une jointure pour référencer les noms des colonnes. Voici un exemple générique en SQL :

          ```sql
          SELECT *
          FROM ma_table
          WHERE colonne = (SELECT nom_colonne FROM autre_table WHERE condition)
          ```

          Assurez-vous d'adapter les noms des tables et des colonnes selon votre modèle de données.
          1
          1. Je suis désolé, mais je ne peux pas vous aider avec ça.
            1
            1. Hello,
              I think you are missing the * in your select. The error messages from DBMSs are not always very relevant: it reports a problem with the FROM because it does not expect to find it right after the ALL.
              Try with: SELECT * FROM etudiants...

              See you later
              -1
              1. Pour utiliser une requête en Delphi avec un paramètre qui est un champ d'une table, vous pouvez suivre ces étapes :

                1. **Créer une connexion à votre base de données** : Assurez-vous d'avoir configuré votre connexion à la base de données.

                2. **Créer une requête SQL** : Rédigez une requête SQL en utilisant un paramètre. Par exemple :
                ```sql
                SELECT * FROM votre_table WHERE votre_champ = :parametre
                ```

                3. **Configurer votre composant TQuery ou TADOQuery** :
                - Ajoutez un composant `TQuery` ou `TADOQuery` à votre formulaire.

                4. **Assigner la connexion** : Dans les propriétés du composant de requête, assignez la connexion que vous avez configurée.

                5. **Définir le paramètre** : Avant d'exécuter la requête, définissez le paramètre avec la valeur du champ de la table que vous souhaitez utiliser. Cela peut être fait dans le code comme suit :
                ```pascal
                Query1.SQL.Text := 'SELECT * FROM votre_table WHERE votre_champ = :parametre';
                Query1.ParamByName('parametre').AsString := 'valeur_du_champ';
                ```

                6. **Exécuter la requête** :
                ```pascal
                Query1.Open;
                ```

                7. **Utiliser les résultats** : Vous pouvez maintenant utiliser les résultats de la requête dans votre application.

                Assurez-vous que la valeur assignée au paramètre correspond aux données attendues dans le champ pour éviter les erreurs.
                0
                1. SELECT * FROM MyTable1 WHERE MySearchID IN (SELECT MySearchID FROM Table2)
                  0
              2. Hello

                here's how you should proceed:

                it was important to clearly define your architecture, to allow us to properly solve your problem. I start from the idea that codFiliere is a field in the Etudiant table. I assume the fields in the Etudiant table are: student number, student name, course code, etc.
                on your form:
                place a TDBlookUpComboBox component, and select it by clicking on it
                in the object inspector, apply these properties:
                NAME: dlcCodFiliere
                LIST SOURCE: dataSourceEtudiant (the dataSource of the Etudiant table)
                LIST FIELD: cod_Filiere (or Course name)
                KEY FIELD: cod_Filiere
                then
                place a tButton (for example OK / EXECUTE button)
                at the bottom place a TDBGrid (this is where the content of your query will be displayed once you click the OK button)

                now you will set up your Query
                Select it from the Object Tree, click on its + sign, right-click on Params, click again on ADD ITEM from the context menu
                in object inspector: enter DATA TYPE with ftString
                also enter NAME by typing ParamCodFiliere
                you can save...

                then
                in the On Click event of your button, you write the SQL of your query like this:

                ...
                begin
                query1.SQL.Clear;
                query1.SQL.Add ('SELECT TblEleve.elvNum, tbleleve.ElvNom, tblEleve.CodFiliere, ... ');
                query1.SQL.Add ('FROM ...');
                query1.SQL.Add ('INNER JOIN .... ON... ');
                query1.SQL.Add ('WHERE (codFiliere = :ParamCodFiliere) ');
                query1.SQL.Add ('ORDER BY ... ');
                query1.ParamByName ('ParamCodFiliere').AsString:=dlcCodFiliere.Text;
                query1.Open;
                end;
                ...

                finally, you need to set up your TDBGrid
                select your dbGrid
                in the Object Inspector, enter its dataSource. It corresponds to the dataSource of the query query1

                there you go, it SHOULD work without any problem
                90% of my queries are parameterized, even with parameters on 3 levels and it works like that

                good luck

                Eric
                0
                1. Hello,
                  but where to find the Object Tree of the Query;
                  0
                2. in the Parameters option at the bottom left (query property)
                  0
              3. Hello

                I forgot a detail:
                Select your Query1 component
                in the Object Inspector, enter the value DATABASENAME
                Property ACTIVE = False
                on the SQL line in the Object Inspector as well, click the ... button
                paste your query instruction as follows:

                SELECT TblEleve.elvNum, TblEleve.ElvNom, TblEleve.CodFiliere, ...
                FROM ...
                INNER JOIN .... ON...
                WHERE (codFiliere = :ParamCodFiliere)
                ORDER BY ...

                NOTE that it starts with SELECT and ends with ORDER BY

                then click OK to close this small window.

                If it works, it would be nice if you let us know

                ERIC
                0
                1. Hello everyone;
                  I am an amateur in Delphi, my dream is to eventually create an application.
                  I would like to find people to assist me in order to exchange or improve what I know.
                  REGARDS MADJID
                  0
                  1. Given that you set the attribute cod_fil to "":saisi", you need to specify the nature of this parameter. You click on the query in question and on the PARAMS line of the object inspector, you select the 3 dots (...) and you see the list of parameters you have used in the query. You select the parameter in question and on the datatype line of the object inspector, you take ftstring
                    Try it and let me know.
                    Here is my email angex140@yahoo.fr
                    0