I am trying to "connect two forms with mysql" I can't explain it very well.
I want to show users first and last name in other form when I click on him ( Click on his ID or ID Održ or first name or last name.)
How can I do that with MySQL? I am beginner in delphi and MySQL.
CodePudding user response:
This is purely related to Delphi and not the DBMS (MySQL or others). What you are trying to achieve is showing data from your main form's dataset on a second form for details (shown as modal).
You need to be more specific about your implementation :
- The location of the dataset : on your main form or on an independent DataModule ?
- Also, you didn't specify how the values of selected record will be used in the detail form : In labels, variables, other DB-aware controls ?
- Is there a bidirectionnal relationship (Editing on the detail form will edit the record on the main form) ?
Here are the possible steps for your case : Suppose your dataset control is named ''Dataset'', your main form is ''Form1'', and the detail form is ''Form2''.
First : If your dataset is on your main form, to avoid circular dependency you have to declare :
- The detail form's unit in the Implementation uses a section of the main form's unit.
- The main form's unit in the Interface uses a section of the detail form's unit.
If your dataset is on an independent "DataModule", then you have to declare :
- The detail form's unit in the Implementation uses a section of the main form's unit.
- The DataModule's unit in the Interface uses a section of units of both forms.
Second : Go to the DBGrid "OnCellClick" event (since you only want to click on the cell), write a code to check the column's FieldName if it is ''Id'' or ''Last Name'' or whatever you want, then show the detail form (ShowModal)
procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
//Add whatever fields you want
If (Column.FieldName = 'Id') or (Column.FieldName = 'Last Name') then
Form2.ShowModal;
end;
Third : In the detail form, there are two possible Implementations :
- If you have DB-aware controls (TDBEdit, TDBLabel, ...), then you can set their ''DataSource'' and DataField'' properties to get and exchange data with the dataset, for example ''Form1.Dataset'' or ''DataModule1.Dataset''. And the data will be refreshed everytime the detail form is shown.
- If you want to use the values of the selected records in variables or a custom logic (like changing a TLabel's caption to something like ''User is %s with id %u'', or as foreign key for filtering another dataset), then you have to get and set those values in the detail form ''OnShow'' event, like in this example :
procedure TForm2.FormShow(Sender: TObject);
begin
Label1.Caption := Format('Details of %s %s - Id : %u', [Form1.Dataset.FirstNameField.AsString, Form1.Dataset.LastNameField.AsString, Form1.Dataset.IdField.AsInteger]);
end;
Or like this :
procedure TForm2.FormShow(Sender: TObject);
var
FirstName, LastName: string;
Id: Integer;
begin
FirstName := Form1.Dataset.FirstNameField.AsString;
LastName := Form1.Dataset.LastNameField.AsString;
Id := Form1.Dataset.IdField.AsInteger;
end;
Fourth : There is a known issue, the second form ''OnShow" event is fired only on the first call to ''ShowModal'' after form creation, and not in subsequent calls. As a workaround, you have to either release or hide the detail form when you close it. You can do this through the detail form ''OnClose'' event, by setting a value for the close action (''caFree'' or ''caHide'').
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caHide;
end;
Fifth : If you want to update the current record from the detail form, you have to put a control (like a button) for validation in the detail form with an "OnClick" event handler that will commit the changes to the dataset and close the detail form. There is also the form's ''ModalResult'' property that, when assigned a non-zero value, will close the modal form and return that value to the function ''ShowModal'' called in the main form :
- If you use this property, then the validation button on the detail form must assign the returned value, and on the main form you have to check the returned value and receive updated values as variables from the detail.
- But you can also avoid using it in order to keep the update logic on the detail form.
If you call the Close method in a modal form, then "ModalResult" is automatically set to "mrCancel". Here is an example for updating the record without need for ''ModalResult''property (if your dataset is on a DataModule, then replace "Form1" with the name of the DataModule) :
procedure TForm2.BtnSaveClick(Sender: TObject);
begin
try
Form1.Dataset.Edit;
try
Form1.Dataset.FirstNameField.AsString := EditFirstName.Caption;
Form1.Dataset.LastNameField.AsString := EditLastName.Caption;
Form1.Dataset.Post;
except
Form1.Dataset.Cancel;
end;
finally
Close;
end;
end;