My task is to make a program by which a person can view student performance and attendance. DB did in Access, connected via ADOQuery (through it everything is done and necessary). I also use DBGrid. In the first DBGrid I have a college specialty, in the second group of college. And the problem is that I do not know how to make the connection between them! So that when you click on a specialty in the second DBGrid, it automatically shows the groups of this specialty (after that you will need to make a similar connection group - group students and students - attendance / performance). I know how to do this in ADOTable, but you can't use it, you need to use ADOQuery. I used this code

procedure TForm3.DBGrid1CellClick(Column: TColumn); begin with form2.ADOQuery2 do begin Close; SQL.Clear; SQL.Add('select * from groups where spec = "' + ADOQuery1.FieldByName('spec').AsString + '"'); Open; end; end; 

but the compiler constantly swears! Now he does not like quotes, then ADOQuery or ADOQuery1.FieldByName and AsString ... I will be grateful if you can help!

    3 answers 3

    For master-detail communication, you do not need to write DBGrid1CellClick handlers and the like. It is enough to configure the connection once.

     Q1.sql.text := 'select id, name from groups'; Q2.DataSource := DS1; Q2.sql.text := 'select id, fio from students where group_id = :id'; 

    Communication is established through the DataSource and parameters in the request. The names of the parameters in the detailed query are substituted by the name of the field in the main query, i.e. In the main field there was an id field, so the parameter id will be available in the slave

    This connection can be established both from the code (see above) and in object inspector without writing any code at all.

    About writing queries. Never try to substitute any values ​​(except integer can be) direct change of the query text. Always use parameters. Your data can and will break your program depending on the input.

    • Thank you very much! You just saved me!) - Denyska

    To quotes did not swear try quotedstr:

     'select* from groups where spec='+quotedstr(ADOQuery1.FieldByName('spec').AsString) 

      I used to work in Delphi too. There must be single quotes. Try the following expression:

        'select * from groups where spec='+#39+ADOQuery1.FieldByName('spec').AsString+#39 
      • I write an error in both the first and second cases, the compiler does not like ADOQuery1.FieldByName and AsString - Denyska