Sign in Join
Wiki copytemporary

TSqlite3 Master Detail Example/fr: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TSqlite3 Master Detail Example/fr

OldNewLine
1+Exemple de relation Maître/Détail pour TSqlite3.
2+
3+## A propos de la démo
4+
5+Cette démo est un exemple fonctionnel de comment utiliser le composant DataSet TSqlite3 de [[SQLite/fr|SQLite]] dans une relation Maître/Détail.
6+
7+> **Note:** Ce composant ne fait pas partie des composants de base de données généraux de la SQLDB. SQLDB fournit TSQLite3Connection qui correspond à d'autres connecteurs de bases de données. Il est fortement recommandé de regarder dans TSQLite3Connection du fait de la facilité de changement vers une autre base de données ; utilisez seulement TSQLite3 si cela est réellement demandé.
8+
9+Voir [[MasterDetail/fr|MasterDetail]] pour des instructions sur comment implémenter des relations Maître/Détail en utilisant des composants SQLdb standards (p.ex. sqlite3).
10+
11+# Auteur
12+
13+David Stewart .. davesimplewear at yahoo dot com\
14+NdT : Merci à Dave Stewart pour cet article.
15+
16+# Composants utilisés
17+
18+- TSqlite3
19+- Standard Lazarus database components
20+
21+# License
22+
23+- Vous êtes libres de l'utiliser comme vous le voulez.
24+
25+# Téléchargement
26+
27+L'exemple TSQLite3 peut être téléchargé depuis [L'emplacement de téléchargement Lazarus-CCR de SF](http://sourceforge.net/projects/lazarus-ccr/files/Demos%20and%20examples/). Aussi depuis [Freeware de David](http://www.users.on.net/~dave.stewart/index.html).
28+
29+## Captures d'écran
30+
31+![](/wiki-images/b/b8/Screenshot-Master_Detail_Example_-_SQLLite3.png)
32+
33+![](/wiki-images/8/82/Screenshot-Master_Detail_Example_-_SQLLite3-1.png)
34+
35+![](/wiki-images/7/71/Screenshot-Master_Detail_Example_-_SQLLite3-2.png)
36+
37+![](/wiki-images/1/14/Screenshot-Select_Customer_Name.png)
38+
39+![](/wiki-images/2/2e/Screenshot-Select_Item_Number.png)
40+
41+Cette capture d'écran montre la configuration de la table pour le Mapître/Détail.
42+
43+![](/wiki-images/e/e6/Screenshot.png)
44+
45+## Code du programme d'exemple
46+
47+``` pascal
48+unit uMain;
49+
50+{$mode objfpc}{$H+}
51+
52+interface
53+
54+uses
55+ Classes, SysUtils, db, sqlite3ds, FileUtil, LResources, Forms, Controls,
56+ Graphics, Dialogs, ComCtrls, ExtCtrls, Menus, DbCtrls, StdCtrls, DBGrids;
57+
58+type
59+
60+ { TfMain }
61+
62+ TfMain = class(TForm)
63+ btnSelCust: TButton;
64+ btnAddSale: TButton;
65+ btnSaveEntry: TButton;
66+ btnDelEntry: TButton;
67+ DBNavigator3: TDBNavigator;
68+ dsCust: TDatasource;
69+ dsSales: TDatasource;
70+ dsStock: TDatasource;
71+ DBEdit1: TDBEdit;
72+ DBEdit10: TDBEdit;
73+ DBEdit12: TDBEdit;
74+ DBEdit13: TDBEdit;
75+ DBEdit14: TDBEdit;
76+ DBEdit15: TDBEdit;
77+ DBEdit3: TDBEdit;
78+ DBEdit4: TDBEdit;
79+ DBEdit5: TDBEdit;
80+ DBEdit6: TDBEdit;
81+ DBEdit7: TDBEdit;
82+ DBEdit8: TDBEdit;
83+ DBEdit9: TDBEdit;
84+ dgSales: TDBGrid;
85+ DBNavigator1: TDBNavigator;
86+ DBNavigator2: TDBNavigator;
87+ Label1: TLabel;
88+ Label10: TLabel;
89+ Label11: TLabel;
90+ Label2: TLabel;
91+ Label3: TLabel;
92+ Label4: TLabel;
93+ Label5: TLabel;
94+ Label7: TLabel;
95+ Label8: TLabel;
96+ Label9: TLabel;
97+ miClose: TMenuItem;
98+ miFile: TMenuItem;
99+ mmMain: TMainMenu;
100+ nbMain: TNotebook;
101+ Panel1: TPanel;
102+ pnlSales: TPanel;
103+ pnlStock: TPanel;
104+ pnlCustomer: TPanel;
105+ pnlSelectCust: TPanel;
106+ pStock: TPage;
107+ pCustomer: TPage;
108+ pSales: TPage;
109+ sbMain: TStatusBar;
110+ TCustAddr: TStringField;
111+ TCustCustName: TStringField;
112+ TCustcustState: TStringField;
113+ TCustID: TAutoIncField;
114+ TCustpostCode: TStringField;
115+ TCustSuburb: TStringField;
116+ TSalescustID: TLongintField;
117+ TSalesID: TAutoIncField;
118+ TSalesitem: TStringField;
119+ TSalesitemNum: TStringField;
120+ TSalesprice: TFloatField;
121+ TSalessaleDate: TDateField;
122+ TSalesshipDate: TDateField;
123+ TStock: TSqlite3Dataset;
124+ TSales: TSqlite3Dataset;
125+ TCust: TSqlite3Dataset;
126+ TStockID: TAutoIncField;
127+ TStockitem: TStringField;
128+ TStockitemNum: TStringField;
129+ TStockprice: TFloatField;
130+ procedure btnAddSaleClick(Sender: TObject);
131+ procedure btnDelEntryClick(Sender: TObject);
132+ procedure btnSaveEntryClick(Sender: TObject);
133+ procedure btnSelCustClick(Sender: TObject);
134+ procedure dgSalesEditButtonClick(Sender: TObject);
135+ procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
136+ procedure FormCreate(Sender: TObject);
137+ procedure FormDestroy(Sender: TObject);
138+ procedure FormShow(Sender: TObject);
139+ procedure miCloseClick(Sender: TObject);
140+ private
141+ { private declarations }
142+ public
143+ { public declarations }
144+ end;
145+
146+var
147+ fMain: TfMain;
148+
149+implementation
150+uses uCust, uSales;
151+{ TfMain }
152+
153+procedure TfMain.miCloseClick(Sender: TObject);
154+begin
155+ Close;
156+end;
157+
158+procedure TfMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
159+begin
160+ CanClose := MessageDlg('Are You Sure ?',mtConfirmation,[mbYes,mbNo],0)=mrYes;
161+end;
162+
163+procedure TfMain.FormCreate(Sender: TObject);
164+var
165+n:integer;
166+c:TComponent;
167+FName:string;
168+begin
169+ fName := ExtractFilePath(ParamStr(0)) +'data/md.db3';
170+for n := 0 to ComponentCount -1 do
171+ begin
172+ c := Components[n];
173+ if c is TSqlite3Dataset then
174+ TSqlite3Dataset(c).FileName:= fName;
175+ end;
176+ for n := 0 to ComponentCount -1 do
177+ begin
178+ c := Components[n];
179+ if c is TSqlite3Dataset then
180+ TSqlite3Dataset(c).Open;
181+ end;
182+end;
183+
184+procedure TfMain.FormDestroy(Sender: TObject);
185+var
186+n:integer;
187+c:TComponent;
188+begin
189+ for n := 0 to ComponentCount -1 do
190+ begin
191+ c := Components[n];
192+ if c is TSqlite3Dataset then
193+ TSqlite3Dataset(c).Close;
194+ end;
195+end;
196+
197+procedure TfMain.FormShow(Sender: TObject);
198+begin
199+ nbMain.PageIndex:=0;
200+end;
201+
202+procedure TfMain.dgSalesEditButtonClick(Sender: TObject);
203+begin
204+ if SearchDlg.ShowModalParts =mrOk then
205+ begin
206+ TSales.Edit;
207+ TSalesItemNum.Value := SearchDlg.PartNum;
208+ TSalesItem.Value := TStockitem.Value;
209+ TSalesPRICE.Value:= TStockPrice.Value;
210+ end;
211+end;
212+
213+procedure TfMain.btnSelCustClick(Sender: TObject);
214+begin
215+ custDlg.CustName := TCustCUSTNAME.Value ;
216+ if CustDlg.ShowModalCust =mrOk then
217+ begin
218+ TCust.Edit;
219+ TCustCUSTNAME.Value := custDlg.CustName;
220+ end;
221+end;
222+
223+procedure TfMain.btnAddSaleClick(Sender: TObject);
224+begin
225+ TSales.Append;
226+end;
227+
228+procedure TfMain.btnDelEntryClick(Sender: TObject);
229+begin
230+ TSales.Delete;
231+end;
232+
233+procedure TfMain.btnSaveEntryClick(Sender: TObject);
234+begin
235+ TSales.ApplyUpdates;
236+end;
237+
238+initialization
239+ {$I uMain.lrs}
240+
241+end.
242+```
243+
244+uMain.lfm (edité)
245+
246+``` pascal
247+object fMain: TfMain
248+ Left = 395
249+ Height = 332
250+ Top = 218
251+ Width = 534
252+ ActiveControl = nbMain
253+ BorderIcons = [biSystemMenu, biMinimize]
254+ BorderStyle = bsSingle
255+ Caption = 'Master Detail Example - SQLLite3'
256+ ClientHeight = 305
257+ ClientWidth = 534
258+ Font.Height = -13
259+ Font.Name = 'Sans'
260+ Menu = mmMain
261+ OnCloseQuery = FormCloseQuery
262+ OnCreate = FormCreate
263+ OnDestroy = FormDestroy
264+ OnShow = FormShow
265+ Position = poScreenCenter
266+ LCLVersion = '0.9.27'
267+ object sbMain: TStatusBar
268+ Left = 0
269+ Height = 19
270+ Top = 286
271+ Width = 534
272+ AutoHint = True
273+ Panels = <>
274+ end
275+ object nbMain: TNotebook
276+ Left = 0
277+ Height = 286
278+ Top = 0
279+ Width = 534
280+ Align = alClient
281+ PageIndex = 0
282+ TabOrder = 1
283+ object pSales: TPage
284+ Caption = 'Sales'
285+ ClientWidth = 532
286+ ClientHeight = 259
287+ object Label3: TLabel
288+ Left = 0
289+ Height = 18
290+ Top = 241
291+ Width = 532
292+ Align = alBottom
293+ Caption = 'Select Customer First, then click in item number field to select item, then save'
294+ ParentColor = False
295+ end
296+ object pnlSelectCust: TPanel
297+ Left = 15
298+ Height = 216
299+ Top = 14
300+ Width = 232
301+ BevelInner = bvLowered
302+ BevelWidth = 2
303+ ClientHeight = 216
304+ ClientWidth = 232
305+ TabOrder = 0
306+ object Label1: TLabel
307+ Left = 4
308+ Height = 18
309+ Top = 4
310+ Width = 224
311+ Align = alTop
312+ Alignment = taCenter
313+ Caption = 'Customer Detail'
314+ Font.Height = -13
315+ Font.Name = 'Sans'
316+ Font.Style = [fsBold, fsItalic]
317+ ParentColor = False
318+ ParentFont = False
319+ end
320+ object Label2: TLabel
321+ Left = 24
322+ Height = 18
323+ Top = 31
324+ Width = 73
325+ Caption = 'First Name'
326+ Font.Height = -13
327+ Font.Name = 'Sans'
328+ Font.Style = [fsItalic]
329+ ParentColor = False
330+ ParentFont = False
331+ end
332+ object DBEdit1: TDBEdit
333+ Left = 24
334+ Height = 23
335+ Hint = 'Cust name'
336+ Top = 48
337+ Width = 184
338+ DataField = 'CustName'
339+ DataSource = dsCust
340+ ReadOnly = True
341+ MaxLength = 8192
342+ ParentShowHint = False
343+ ShowHint = True
344+ TabOrder = 0
345+ end
346+ object DBEdit3: TDBEdit
347+ Left = 24
348+ Height = 23
349+ Hint = 'Address'
350+ Top = 72
351+ Width = 184
352+ DataField = 'Addr'
353+ DataSource = dsCust
354+ ReadOnly = True
355+ MaxLength = 8192
356+ ParentShowHint = False
357+ ShowHint = True
358+ TabOrder = 1
359+ end
360+ object DBEdit4: TDBEdit
361+ Left = 24
362+ Height = 23
363+ Hint = 'Suburb/Town'
364+ Top = 96
365+ Width = 184
366+ DataField = 'Suburb'
367+ DataSource = dsCust
368+ ReadOnly = True
369+ MaxLength = 8192
370+ ParentShowHint = False
371+ ShowHint = True
372+ TabOrder = 2
373+ end
374+ object DBEdit5: TDBEdit
375+ Left = 24
376+ Height = 23
377+ Hint = 'Postal Code'
378+ Top = 120
379+ Width = 80
380+ DataField = 'postCode'
381+ DataSource = dsCust
382+ ReadOnly = True
383+ MaxLength = 8192
384+ ParentShowHint = False
385+ ShowHint = True
386+ TabOrder = 3
387+ end
388+ object DBEdit6: TDBEdit
389+ Left = 128
390+ Height = 23
391+ Hint = 'State'
392+ Top = 119
393+ Width = 80
394+ DataField = 'custState'
395+ DataSource = dsCust
396+ ReadOnly = True
397+ CharCase = ecUppercase
398+ MaxLength = 8192
399+ ParentShowHint = False
400+ ShowHint = True
401+ TabOrder = 4
402+ end
403+ object btnSelCust: TButton
404+ Left = 24
405+ Height = 25
406+ Hint = 'Click to select a Customer'
407+ Top = 176
408+ Width = 184
409+ Caption = 'Select Customer'
410+ OnClick = btnSelCustClick
411+ ParentShowHint = False
412+ ShowHint = True
413+ TabOrder = 5
414+ end
415+ object DBNavigator3: TDBNavigator
416+ Left = 30
417+ Height = 22
418+ Top = 150
419+ Width = 170
420+ BevelOuter = bvNone
421+ ClientHeight = 22
422+ ClientWidth = 170
423+ DataSource = dsCust
424+ TabOrder = 6
425+ VisibleButtons = [nbFirst, nbPrior, nbNext, nbLast]
426+ end
427+ end
428+ object pnlSales: TPanel
429+ Left = 255
430+ Height = 216
431+ Top = 14
432+ Width = 260
433+ BevelInner = bvLowered
434+ BevelWidth = 2
435+ ClientHeight = 216
436+ ClientWidth = 260
437+ TabOrder = 1
438+ object Label4: TLabel
439+ Left = 4
440+ Height = 18
441+ Top = 4
442+ Width = 252
443+ Align = alTop
444+ Alignment = taCenter
445+ Caption = 'Sales Detail'
446+ Font.Height = -13
447+ Font.Name = 'Sans'
448+ Font.Style = [fsBold, fsItalic]
449+ ParentColor = False
450+ ParentFont = False
451+ end
452+ object btnAddSale: TButton
453+ Left = 16
454+ Height = 25
455+ Hint = 'Add Another Sale'
456+ Top = 176
457+ Width = 72
458+ Caption = 'Add Sale'
459+ OnClick = btnAddSaleClick
460+ ParentShowHint = False
461+ ShowHint = True
462+ TabOrder = 0
463+ end
464+ object Panel1: TPanel
465+ Left = 4
466+ Height = 13
467+ Top = 22
468+ Width = 252
469+ Align = alTop
470+ BevelOuter = bvNone
471+ TabOrder = 1
472+ end
473+ object dgSales: TDBGrid
474+ Left = 4
475+ Height = 125
476+ Hint = 'Click Item Number Button to Select Item'
477+ Top = 35
478+ Width = 252
479+ Align = alTop
480+ Columns = <
481+ item
482+ ButtonStyle = cbsEllipsis
483+ Title.Caption = 'item Number'
484+ Width = 100
485+ FieldName = 'itemNum'
486+ end
487+ item
488+ Width = 150
489+ FieldName = 'item'
490+ end
491+ item
492+ FieldName = 'price'
493+ end
494+ item
495+ Title.Caption = 'sale Date'
496+ FieldName = 'saleDate'
497+ end
498+ item
499+ Title.Caption = 'ship Date'
500+ FieldName = 'shipDate'
501+ end>
502+ DataSource = dsSales
503+ ShowHint = True
504+ TabOrder = 2
505+ TitleFont.Height = -13
506+ TitleFont.Name = 'Sans'
507+ OnEditButtonClick = dgSalesEditButtonClick
508+ end
509+ object btnSaveEntry: TButton
510+ Left = 95
511+ Height = 25
512+ Hint = 'Save Entry'
513+ Top = 176
514+ Width = 51
515+ Caption = 'Save'
516+ OnClick = btnSaveEntryClick
517+ ParentShowHint = False
518+ ShowHint = True
519+ TabOrder = 3
520+ end
521+ object btnDelEntry: TButton
522+ Left = 168
523+ Height = 25
524+ Hint = 'Delete Entry'
525+ Top = 176
526+ Width = 75
527+ Caption = 'Delete'
528+ OnClick = btnDelEntryClick
529+ ParentShowHint = False
530+ ShowHint = True
531+ TabOrder = 4
532+ end
533+ end
534+ end
535+ object pCustomer: TPage
536+ Caption = 'Customer'
537+ ClientWidth = 532
538+ ClientHeight = 259
539+ object pnlCustomer: TPanel
540+ Left = 95
541+ Height = 194
542+ Top = 30
543+ Width = 339
544+ BevelInner = bvLowered
545+ BevelWidth = 2
546+ ClientHeight = 194
547+ ClientWidth = 339
548+ TabOrder = 0
549+ object Label5: TLabel
550+ Left = 72
551+ Height = 18
552+ Top = 31
553+ Width = 73
554+ Caption = 'First Name'
555+ Font.Height = -13
556+ Font.Name = 'Sans'
557+ Font.Style = [fsItalic]
558+ ParentColor = False
559+ ParentFont = False
560+ end
561+ object Label7: TLabel
562+ Left = 4
563+ Height = 18
564+ Top = 4
565+ Width = 331
566+ Align = alTop
567+ Alignment = taCenter
568+ Caption = 'Customer Entry'
569+ Font.Height = -13
570+ Font.Name = 'Sans'
571+ Font.Style = [fsBold, fsItalic]
572+ ParentColor = False
573+ ParentFont = False
574+ end
575+ object DBEdit10: TDBEdit
576+ Left = 72
577+ Height = 23
578+ Hint = 'Cust name'
579+ Top = 48
580+ Width = 184
581+ DataField = 'CustName'
582+ DataSource = dsCust
583+ MaxLength = 8192
584+ ParentShowHint = False
585+ ShowHint = True
586+ TabOrder = 0
587+ end
588+ object DBEdit12: TDBEdit
589+ Left = 72
590+ Height = 23
591+ Hint = 'Address'
592+ Top = 72
593+ Width = 184
594+ DataField = 'Addr'
595+ DataSource = dsCust
596+ MaxLength = 8192
597+ ParentShowHint = False
598+ ShowHint = True
599+ TabOrder = 1
600+ end
601+ object DBEdit13: TDBEdit
602+ Left = 72
603+ Height = 23
604+ Hint = 'Suburb/Town'
605+ Top = 96
606+ Width = 184
607+ DataField = 'Suburb'
608+ DataSource = dsCust
609+ MaxLength = 8192
610+ ParentShowHint = False
611+ ShowHint = True
612+ TabOrder = 2
613+ end
614+ object DBEdit14: TDBEdit
615+ Left = 72
616+ Height = 23
617+ Hint = 'Postal Code'
618+ Top = 120
619+ Width = 80
620+ DataField = 'postCode'
621+ DataSource = dsCust
622+ MaxLength = 8192
623+ ParentShowHint = False
624+ ShowHint = True
625+ TabOrder = 3
626+ end
627+ object DBEdit15: TDBEdit
628+ Left = 176
629+ Height = 23
630+ Hint = 'State'
631+ Top = 119
632+ Width = 80
633+ DataField = 'custState'
634+ DataSource = dsCust
635+ CharCase = ecUppercase
636+ MaxLength = 8192
637+ ParentShowHint = False
638+ ShowHint = True
639+ TabOrder = 4
640+ end
641+ object DBNavigator2: TDBNavigator
642+ Left = 45
643+ Height = 25
644+ Top = 154
645+ Width = 241
646+ BevelOuter = bvNone
647+ ClientHeight = 25
648+ ClientWidth = 241
649+ DataSource = dsCust
650+ TabOrder = 5
651+ end
652+ end
653+ end
654+ object pStock: TPage
655+ Caption = 'Stock'
656+ ClientWidth = 532
657+ ClientHeight = 259
658+ object pnlStock: TPanel
659+ Left = 95
660+ Height = 218
661+ Top = 22
662+ Width = 339
663+ BevelInner = bvLowered
664+ BevelWidth = 2
665+ ClientHeight = 218
666+ ClientWidth = 339
667+ TabOrder = 0
668+ object Label8: TLabel
669+ Left = 4
670+ Height = 18
671+ Top = 4
672+ Width = 331
673+ Align = alTop
674+ Alignment = taCenter
675+ Caption = 'Stock Entry'
676+ Font.Height = -13
677+ Font.Name = 'Sans'
678+ Font.Style = [fsBold, fsItalic]
679+ ParentColor = False
680+ ParentFont = False
681+ end
682+ object Label9: TLabel
683+ Left = 75
684+ Height = 18
685+ Top = 24
686+ Width = 83
687+ Caption = 'Part Number'
688+ Font.Height = -13
689+ Font.Name = 'Sans'
690+ Font.Style = [fsItalic]
691+ ParentColor = False
692+ ParentFont = False
693+ end
694+ object Label10: TLabel
695+ Left = 75
696+ Height = 18
697+ Top = 72
698+ Width = 75
699+ Caption = 'Description'
700+ Font.Height = -13
701+ Font.Name = 'Sans'
702+ Font.Style = [fsItalic]
703+ ParentColor = False
704+ ParentFont = False
705+ end
706+ object Label11: TLabel
707+ Left = 75
708+ Height = 18
709+ Top = 128
710+ Width = 33
711+ Caption = 'Price'
712+ Font.Height = -13
713+ Font.Name = 'Sans'
714+ Font.Style = [fsItalic]
715+ ParentColor = False
716+ ParentFont = False
717+ end
718+ object DBEdit7: TDBEdit
719+ Left = 75
720+ Height = 23
721+ Top = 40
722+ Width = 181
723+ DataField = 'itemNum'
724+ DataSource = dsStock
725+ MaxLength = 8192
726+ ParentShowHint = False
727+ ShowHint = True
728+ TabOrder = 0
729+ end
730+ object DBEdit8: TDBEdit
731+ Left = 75
732+ Height = 23
733+ Top = 89
734+ Width = 181
735+ DataField = 'item'
736+ DataSource = dsStock
737+ MaxLength = 8192
738+ ParentShowHint = False
739+ ShowHint = True
740+ TabOrder = 1
741+ end
742+ object DBEdit9: TDBEdit
743+ Left = 75
744+ Height = 23
745+ Top = 145
746+ Width = 181
747+ DataField = 'price'
748+ DataSource = dsStock
749+ ParentShowHint = False
750+ ShowHint = True
751+ TabOrder = 2
752+ end
753+ object DBNavigator1: TDBNavigator
754+ Left = 48
755+ Height = 25
756+ Top = 183
757+ Width = 241
758+ BevelOuter = bvNone
759+ ClientHeight = 25
760+ ClientWidth = 241
761+ DataSource = dsStock
762+ ParentShowHint = False
763+ ShowHint = True
764+ TabOrder = 3
765+ end
766+ end
767+ end
768+ end
769+ object mmMain: TMainMenu
770+ left = 16
771+ top = 277
772+ object miFile: TMenuItem
773+ Caption = '&File'
774+ object miClose: TMenuItem
775+ Caption = '&Close'
776+ GlyphShowMode = gsmAlways
777+ Hint = 'Close Application'
778+ OnClick = miCloseClick
779+ end
780+ end
781+ end
782+ object dsCust: TDatasource
783+ DataSet = TCust
784+ left = 16
785+ top = 53
786+ end
787+ object dsSales: TDatasource
788+ DataSet = TSales
789+ left = 16
790+ top = 141
791+ end
792+ object dsStock: TDatasource
793+ DataSet = TStock
794+ left = 16
795+ top = 214
796+ end
797+ object TCust: TSqlite3Dataset
798+ AutoIncrementKey = True
799+ Options = []
800+ PrimaryKey = 'ID'
801+ SaveOnClose = True
802+ SaveOnRefetch = True
803+ SQL = 'Select * from cust;'
804+ TableName = 'cust'
805+ FieldDefs = <
806+ item
807+ Name = 'ID'
808+ DataType = ftAutoInc
809+ Precision = -1
810+ Size = 0
811+ end
812+ item
813+ Name = 'CustName'
814+ DataType = ftString
815+ Precision = -1
816+ Size = 8192
817+ end
818+ item
819+ Name = 'Addr'
820+ DataType = ftString
821+ Precision = -1
822+ Size = 8192
823+ end
824+ item
825+ Name = 'Suburb'
826+ DataType = ftString
827+ Precision = -1
828+ Size = 8192
829+ end
830+ item
831+ Name = 'postCode'
832+ DataType = ftString
833+ Precision = -1
834+ Size = 8192
835+ end
836+ item
837+ Name = 'custState'
838+ DataType = ftString
839+ Precision = -1
840+ Size = 8192
841+ end>
842+ left = 66
843+ top = 53
844+ object TCustID: TAutoIncField
845+ DisplayWidth = 10
846+ FieldKind = fkData
847+ FieldName = 'ID'
848+ Index = 0
849+ LookupCache = False
850+ ProviderFlags = [pfInUpdate, pfInWhere]
851+ ReadOnly = False
852+ Required = False
853+ end
854+ object TCustCustName: TStringField
855+ DisplayWidth = 8192
856+ FieldKind = fkData
857+ FieldName = 'CustName'
858+ Index = 1
859+ LookupCache = False
860+ ProviderFlags = [pfInUpdate, pfInWhere]
861+ ReadOnly = False
862+ Required = False
863+ Size = 8192
864+ end
865+ object TCustAddr: TStringField
866+ DisplayWidth = 8192
867+ FieldKind = fkData
868+ FieldName = 'Addr'
869+ Index = 2
870+ LookupCache = False
871+ ProviderFlags = [pfInUpdate, pfInWhere]
872+ ReadOnly = False
873+ Required = False
874+ Size = 8192
875+ end
876+ object TCustSuburb: TStringField
877+ DisplayWidth = 8192
878+ FieldKind = fkData
879+ FieldName = 'Suburb'
880+ Index = 3
881+ LookupCache = False
882+ ProviderFlags = [pfInUpdate, pfInWhere]
883+ ReadOnly = False
884+ Required = False
885+ Size = 8192
886+ end
887+ object TCustpostCode: TStringField
888+ DisplayWidth = 8192
889+ FieldKind = fkData
890+ FieldName = 'postCode'
891+ Index = 4
892+ LookupCache = False
893+ ProviderFlags = [pfInUpdate, pfInWhere]
894+ ReadOnly = False
895+ Required = False
896+ Size = 8192
897+ end
898+ object TCustcustState: TStringField
899+ DisplayWidth = 8192
900+ FieldKind = fkData
901+ FieldName = 'custState'
902+ Index = 5
903+ LookupCache = False
904+ ProviderFlags = [pfInUpdate, pfInWhere]
905+ ReadOnly = False
906+ Required = False
907+ Size = 8192
908+ end
909+ end
910+ object TSales: TSqlite3Dataset
911+ AutoIncrementKey = True
912+ IndexFieldNames = 'CustID'
913+ Options = []
914+ PrimaryKey = 'ID'
915+ SaveOnClose = True
916+ SaveOnRefetch = True
917+ SQL = 'Select * from sales;'
918+ TableName = 'sales'
919+ MasterSource = dsCust
920+ MasterFields = 'ID'
921+ FieldDefs = <
922+ item
923+ Name = 'itemNum'
924+ DataType = ftString
925+ Precision = -1
926+ Size = 8192
927+ end
928+ item
929+ Name = 'ID'
930+ DataType = ftAutoInc
931+ Precision = -1
932+ Size = 0
933+ end
934+ item
935+ Name = 'custID'
936+ DataType = ftInteger
937+ Precision = -1
938+ Size = 0
939+ end
940+ item
941+ Name = 'saleDate'
942+ DataType = ftDate
943+ Precision = -1
944+ Size = 0
945+ end
946+ item
947+ Name = 'shipDate'
948+ DataType = ftDate
949+ Precision = -1
950+ Size = 0
951+ end
952+ item
953+ Name = 'item'
954+ DataType = ftString
955+ Precision = -1
956+ Size = 8192
957+ end
958+ item
959+ Name = 'price'
960+ DataType = ftFloat
961+ Precision = -1
962+ Size = 0
963+ end>
964+ left = 66
965+ top = 141
966+ object TSalesitemNum: TStringField
967+ DisplayWidth = 8192
968+ FieldKind = fkData
969+ FieldName = 'itemNum'
970+ Index = 0
971+ LookupCache = False
972+ ProviderFlags = [pfInUpdate, pfInWhere]
973+ ReadOnly = False
974+ Required = False
975+ Size = 8192
976+ end
977+ object TSalesID: TAutoIncField
978+ DisplayWidth = 10
979+ FieldKind = fkData
980+ FieldName = 'ID'
981+ Index = 1
982+ LookupCache = False
983+ ProviderFlags = [pfInUpdate, pfInWhere]
984+ ReadOnly = False
985+ Required = False
986+ end
987+ object TSalescustID: TLongintField
988+ DisplayWidth = 10
989+ FieldKind = fkData
990+ FieldName = 'custID'
991+ Index = 2
992+ LookupCache = False
993+ ProviderFlags = [pfInUpdate, pfInWhere]
994+ ReadOnly = False
995+ Required = False
996+ end
997+ object TSalessaleDate: TDateField
998+ DisplayWidth = 10
999+ FieldKind = fkData
1000+ FieldName = 'saleDate'
1001+ Index = 3
1002+ LookupCache = False
1003+ ProviderFlags = [pfInUpdate, pfInWhere]
1004+ ReadOnly = False
1005+ Required = False
1006+ end
1007+ object TSalesshipDate: TDateField
1008+ DisplayWidth = 10
1009+ FieldKind = fkData
1010+ FieldName = 'shipDate'
1011+ Index = 4
1012+ LookupCache = False
1013+ ProviderFlags = [pfInUpdate, pfInWhere]
1014+ ReadOnly = False
1015+ Required = False
1016+ end
1017+ object TSalesitem: TStringField
1018+ DisplayWidth = 8192
1019+ FieldKind = fkData
1020+ FieldName = 'item'
1021+ Index = 5
1022+ LookupCache = False
1023+ ProviderFlags = [pfInUpdate, pfInWhere]
1024+ ReadOnly = False
1025+ Required = False
1026+ Size = 8192
1027+ end
1028+ object TSalesprice: TFloatField
1029+ DisplayWidth = 10
1030+ FieldKind = fkData
1031+ FieldName = 'price'
1032+ Index = 6
1033+ LookupCache = False
1034+ ProviderFlags = [pfInUpdate, pfInWhere]
1035+ ReadOnly = False
1036+ Required = False
1037+ Currency = True
1038+ MaxValue = 0
1039+ MinValue = 0
1040+ Precision = -1
1041+ end
1042+ end
1043+ object TStock: TSqlite3Dataset
1044+ AutoIncrementKey = True
1045+ Options = []
1046+ PrimaryKey = 'ID'
1047+ SaveOnClose = True
1048+ SaveOnRefetch = True
1049+ SQL = 'Select * from stock;'
1050+ TableName = 'stock'
1051+ FieldDefs = <
1052+ item
1053+ Name = 'ID'
1054+ DataType = ftAutoInc
1055+ Precision = -1
1056+ Size = 0
1057+ end
1058+ item
1059+ Name = 'item'
1060+ DataType = ftString
1061+ Precision = -1
1062+ Size = 8192
1063+ end
1064+ item
1065+ Name = 'price'
1066+ DataType = ftFloat
1067+ Precision = -1
1068+ Size = 0
1069+ end
1070+ item
1071+ Name = 'itemNum'
1072+ DataType = ftString
1073+ Precision = -1
1074+ Size = 8192
1075+ end>
1076+ left = 66
1077+ top = 214
1078+ object TStockID: TAutoIncField
1079+ DisplayWidth = 10
1080+ FieldKind = fkData
1081+ FieldName = 'ID'
1082+ Index = 0
1083+ LookupCache = False
1084+ ProviderFlags = [pfInUpdate, pfInWhere]
1085+ ReadOnly = False
1086+ Required = False
1087+ end
1088+ object TStockitem: TStringField
1089+ DisplayWidth = 8192
1090+ FieldKind = fkData
1091+ FieldName = 'item'
1092+ Index = 1
1093+ LookupCache = False
1094+ ProviderFlags = [pfInUpdate, pfInWhere]
1095+ ReadOnly = False
1096+ Required = False
1097+ Size = 8192
1098+ end
1099+ object TStockprice: TFloatField
1100+ DisplayWidth = 10
1101+ FieldKind = fkData
1102+ FieldName = 'price'
1103+ Index = 2
1104+ LookupCache = False
1105+ ProviderFlags = [pfInUpdate, pfInWhere]
1106+ ReadOnly = False
1107+ Required = False
1108+ Currency = True
1109+ MaxValue = 0
1110+ MinValue = 0
1111+ Precision = -1
1112+ end
1113+ object TStockitemNum: TStringField
1114+ DisplayWidth = 8192
1115+ FieldKind = fkData
1116+ FieldName = 'itemNum'
1117+ Index = 3
1118+ LookupCache = False
1119+ ProviderFlags = [pfInUpdate, pfInWhere]
1120+ ReadOnly = False
1121+ Required = False
1122+ Size = 8192
1123+ end
1124+ end
1125+end
1126+```
1127+
1128+``` pascal
1129+unit uCust;
1130+
1131+{$mode objfpc}{$H+}
1132+
1133+interface
1134+
1135+uses
1136+ Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
1137+ StdCtrls, ExtCtrls, Buttons, DBGrids,DB;
1138+
1139+type
1140+
1141+ { TcustDlg }
1142+
1143+ TcustDlg = class(TForm)
1144+ cancelBtn: TButton;
1145+ dgCust: TDBGrid;
1146+ edSearch: TEdit;
1147+ Label1: TLabel;
1148+ okBtn: TButton;
1149+ pnlCust: TPanel;
1150+ sbSearch: TSpeedButton;
1151+ procedure dgCustDblClick(Sender: TObject);
1152+ procedure edSearchChange(Sender: TObject);
1153+ procedure sbSearchClick(Sender: TObject);
1154+ private
1155+ function GetCust: String;
1156+ procedure SetCust(const AValue: String);
1157+ { private declarations }
1158+ public
1159+ { public declarations }
1160+ property CustName: String Read GetCust Write SetCust;
1161+ function ShowModalCust:integer;
1162+ end;
1163+
1164+var
1165+ custDlg: TcustDlg;
1166+
1167+implementation
1168+uses uMain;
1169+{ TcustDlg }
1170+
1171+procedure TcustDlg.edSearchChange(Sender: TObject);
1172+begin
1173+ sbSearch.Enabled:=edSearch.Text<>'';
1174+end;
1175+
1176+procedure TcustDlg.dgCustDblClick(Sender: TObject);
1177+begin
1178+ ModalResult := mrOk;
1179+end;
1180+
1181+procedure TcustDlg.sbSearchClick(Sender: TObject);
1182+begin
1183+ if not fMain.TCust.Locate('CustName', edSearch.Text,[loCaseInsensitive, loPartialKey])
1184+ then
1185+ MessageDlg('No matching record found.', mtInformation, [mbOK], 0);
1186+ edSearch.Color:=clRed;
1187+end;
1188+
1189+function TcustDlg.GetCust: String;
1190+begin
1191+ Result := fMain.TCustCustName.Value;
1192+end;
1193+
1194+procedure TcustDlg.SetCust(const AValue: String);
1195+begin
1196+ fMain.TCust.Locate('CustName',AValue,[loPartialKey,loCaseInsensitive]);
1197+end;
1198+
1199+function TcustDlg.ShowModalCust: integer;
1200+begin
1201+ Caption:='Select Customer Name';
1202+ Result := ShowModal;
1203+end;
1204+
1205+initialization
1206+ {$I uCust.lrs}
1207+
1208+end.
1209+```
1210+
1211+``` pascal
1212+unit uSales;
1213+
1214+{$mode objfpc}{$H+}
1215+
1216+interface
1217+
1218+uses
1219+ Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
1220+ StdCtrls, ExtCtrls, Buttons, DBGrids, DB;
1221+
1222+type
1223+
1224+ { TsearchDlg }
1225+
1226+ TsearchDlg = class(TForm)
1227+ cancelBtn: TButton;
1228+ dgParts: TDBGrid;
1229+ edSearch: TEdit;
1230+ Label1: TLabel;
1231+ okBtn: TButton;
1232+ pnlParts: TPanel;
1233+ sbSearch: TSpeedButton;
1234+ procedure dgPartsDblClick(Sender: TObject);
1235+ procedure edSearchChange(Sender: TObject);
1236+ procedure sbSearchClick(Sender: TObject);
1237+ private
1238+ function GetPartNum: String;
1239+ procedure SetPartNum(const AValue: String);
1240+ { private declarations }
1241+ public
1242+ { public declarations }
1243+ property PartNum:String Read GetPartNum Write SetPartNum;
1244+ function ShowModalParts: Integer;
1245+ end;
1246+
1247+var
1248+ searchDlg: TsearchDlg;
1249+
1250+implementation
1251+uses uMain;
1252+{ TsearchDlg }
1253+
1254+procedure TsearchDlg.edSearchChange(Sender: TObject);
1255+begin
1256+ sbSearch.Enabled:=edSearch.Text<>'';
1257+end;
1258+
1259+procedure TsearchDlg.dgPartsDblClick(Sender: TObject);
1260+begin
1261+ ModalResult := mrOk;
1262+end;
1263+
1264+procedure TsearchDlg.sbSearchClick(Sender: TObject);
1265+begin
1266+ if not fMain.TStock.Locate('itemNum', edSearch.Text,[loCaseInsensitive, loPartialKey])
1267+ then
1268+ MessageDlg('No matching record found.', mtInformation, [mbOK], 0);
1269+ edSearch.Color:=clRed;
1270+end;
1271+
1272+function TsearchDlg.GetPartNum: String;
1273+begin
1274+ Result := fMain.TStockitemNum.Value;
1275+end;
1276+
1277+procedure TsearchDlg.SetPartNum(const AValue: String);
1278+begin
1279+ fMain.TStock.Locate('itemNum',AValue,[loPartialKey,loCaseInsensitive]);
1280+end;
1281+
1282+function TsearchDlg.ShowModalParts: Integer;
1283+begin
1284+ Caption:='Select Item Number';
1285+ Result := ShowModal;
1286+end;
1287+
1288+initialization
1289+ {$I uSales.lrs}
1290+
1291+end.
1292+```
1293+
1294+---
1295+*From the Free Pascal wiki, page "TSqlite3 Master Detail Example/fr", as it stood on 2020-03-02 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history