Sign in Join
Wiki copytemporary

TSqlite3 Master Detail Example: what changed

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

Title: TSqlite3 Master Detail Example

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