Marinas Harbour Tools

  • Increase font size
  • Default font size
  • Decrease font size

DBF database Edit sample using Grid

E-mail Print PDF

This a sample about howto create a database  Dbf edition program

Este es un Ejemplo de como crear un programa de edicion de base de datos DBF

 

Here we use a grid to display data , and the sample show how to create a grid , add items and delete it .

Aqui usamos un grid para presentar los datos , y el ejemplo muestra como crear el grid , agregar Items y borrarlos.

 

 

 When the program starts ask tous if we like to start using a fresh database ( if we have deleted records or make a mistake )

Cuando el programa se inicia nos pregunta su queremos arrancar con una base de datos nueva ( por si hemos borrado registros o hemos cometido algun error )

 

 

In the next printscreen we se the use of a modal window to edit or create data

En el proximo pantallazo vemos el uso de una ventana modal para editar o cargar datos

 

 

 

Using Marinas-Ide DBF internal Tool to edit DBF

Usando la utilidad interna de Marinas para editar la DBF

 

 

Here you can see the source code

Aqui pueden ver el codigo fuente

 

Download Complete source Code with .MIP project file   Clients.rar

 

Sample code clients.prg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include "marinas-gui.ch"
 
 
#define DEMOTITLE "Multiplatform Demo System"
 
REQUEST HB_LANG_ESWIN
REQUEST HB_CODEPAGE_ESWIN
 
 
Function Main ()
 
HB_CDPSELECT('ESWIN')
HB_LANGSELECT('ESWIN')
 
REQUEST DBFCDX , DBFFPT
RDDSETDEFAULT( "DBFCDX" )
 
 
        SET ENTERTAB .T.        //  Replace  Minigui    SET NAVIGATION EXTENDED  
 
        lNewRecord := .F.
 
        ANNOUNCE RDDSYS
        REQUEST DBFCDX , DBFFPT
        RDDSETDEFAULT( "DBFCDX" )
        SET FILECASE LOWER
        SET DIRCASE LOWER
        SET DELETE ON
        SET DECIMALS TO 3
        SET FIXED ON
        SET DATE BRITISH
        SET DATE FORMAT TO "dd/mm/yyyy"
        SET SCOREBOARD OFF
        SET AUTOPEN OFF
 
 
// Main Window Creation
 
CREATE WINDOW d_window
      ROW 0
      COL 0
      WIDTH 800
      HEIGHT 600
      CAPTION DEMOTITLE
      ONINIT  {|| mg_Set( "d_window" , "Style_Grid_1" , "value" , 1 ) }
      ONRELEASE  {|| closebase() }
      ICON "resource/d_test.png"
      MAIN .T.
 
 
 
      mainmenu()
 
      mtoolbar()
 
      mstatusbar()
 
      creategrid()
 
      createbase()
 
      openbase()
 
      refresh()
 
      buttons()
 
 
   END WINDOW
 
   mg_Do( "d_window" , "center" )
 
   mg_Do( "d_window" , "activate" )
 
 
Return .T.
 
//Main menu creation function
 
Function mainmenu()
 
   CREATE MAIN MENU OF d_window
 
      CREATE POPUP "&ABM"
 
         CREATE ITEM "New Record"
            ONCLICK new()
            ITEMNAME item1
            FONTNAME "mg_monospace"
            PICTURE "resource/d_test.png"
         END ITEM
 
         CREATE ITEM "Edit Record"
            ONCLICK edit()
            FONTNAME "mg_monospace"
            PICTURE "resource/d_test.png"
         END ITEM
 
         SEPARATOR
 
         CREATE ITEM "Delete Record"
            ONCLICK DeleteItem()
            FONTNAME "mg_monospace"           
            CHECKED .T.
         END ITEM
 
         CREATE POPUP "More..."
 
            CREATE ITEM "SubItem 1"
               ONCLICK mg_MsgInfo( "SubItem Clicked" )
            END ITEM
 
            CREATE ITEM "SubItem 2"
               ONCLICK mg_MsgInfo( "SubItem Clicked" )
               PICTURE "resource/d_test.png"
            END ITEM
 
            CREATE ITEM "SubItem 3"
               ONCLICK mg_MsgInfo( "SubItem Clicked" )
            END ITEM
 
            CREATE POPUP "More..."
 
               CREATE ITEM "SubItem of SubItem 4"
                  ONCLICK mg_MsgInfo( "SubItem Clicked" )
                  PICTURE "resource/d_test.png"
               END ITEM
 
               CREATE POPUP "More..."
 
                  CREATE ITEM "SubItem 1"
                     ONCLICK mg_MsgInfo( "SubItem Clicked" )
                  END ITEM
 
                  CREATE ITEM "SubItem 2"
                     ONCLICK mg_MsgInfo( "SubItem Clicked" )
                     PICTURE "resource/d_test.png"
                  END ITEM
 
                  CREATE ITEM "SubItem 3"
                     ONCLICK mg_MsgInfo( "SubItem Clicked" )
                  END ITEM
 
                  CREATE POPUP "More..."
 
                     CREATE ITEM "SubItem of SubItem 4"
                        ONCLICK mg_MsgInfo( "SubItem Clicked" )
                        PICTURE "resource/d_test.png"
                     END ITEM
 
                  END POPUP
 
                  CREATE ITEM "SubItem 5"
                     ONCLICK mg_MsgInfo( "SubItem Clicked" )
                  END ITEM
 
                  SEPARATOR
 
                  CREATE ITEM "SubItem 6"
                     ONCLICK mg_MsgInfo( "SubItem2 Clicked" )
                  END ITEM
 
               END POPUP
 
               SEPARATOR
 
            END POPUP
 
            CREATE ITEM "SubItem 5"
               ONCLICK mg_MsgInfo( "SubItem Clicked" )
            END ITEM
 
            SEPARATOR
 
            CREATE ITEM "SubItem 6"
               ONCLICK mg_MsgInfo( "SubItem2 Clicked" )
            END ITEM
 
         END POPUP
 
         SEPARATOR
 
         CREATE ITEM "Item 5"
            ONCLICK mg_MsgInfo( "Click from Item 5" )
         END ITEM
 
         SEPARATOR
 
         CREATE ITEM "Exit"
            ONCLICK mg_Do( mg_GetLastActivatedWindowName() , "release" )
            PICTURE "resource/d_test_stop.png"
         END ITEM
 
      END POPUP
 
      CREATE POPUP "M&enu 2"
 
         CREATE ITEM "Item 1"
            ONCLICK mg_MsgInfo( "Click from Item 1" )
            PICTURE "resource/d_test.png"
         END ITEM
 
         CREATE ITEM "Item 2"
            ONCLICK mg_MsgInfo( "Click from Item 2" )
         END ITEM
 
         CREATE ITEM "Item 3 (CheckMark)"
            ONCLICK mg_MsgInfo( "Click from Item 3" )
         END ITEM
 
         CREATE POPUP "More..."
 
            CREATE ITEM "SubItem 1"
               ONCLICK mg_MsgInfo( "SubItem Clicked" )
            END ITEM
 
            CREATE ITEM "SubItem 2"
               ONCLICK mg_MsgInfo( "SubItem Clicked" )
               PICTURE "resource/d_test.png"
            END ITEM
 
            CREATE ITEM "SubItem 3"
               ONCLICK mg_MsgInfo( "SubItem Clicked" )
            END ITEM
 
            CREATE POPUP "More..."
 
               CREATE ITEM "SubItem of SubItem 4"
                  ONCLICK mg_MsgInfo( "SubItem Clicked" )
                  PICTURE "resource/d_test.png"
               END ITEM
 
            END POPUP
 
            CREATE ITEM "SubItem 5"
 
   ONCLICK mg_MsgInfo( "SubItem Clicked" )
            END ITEM
 
            SEPARATOR
 
            CREATE POPUP "More2..."
 
               CREATE ITEM "SubItem of SubItem 4"
                  ONCLICK mg_MsgInfo( "SubItem Clicked" )
                  PICTURE "resource/d_test.png"
               END ITEM
 
            END POPUP
 
            CREATE ITEM "SubItem 6"
               ONCLICK mg_MsgInfo( "SubItem2 Clicked" )
            END ITEM
 
         END POPUP
 
         SEPARATOR
 
         CREATE ITEM "Item 5"
            ONCLICK mg_MsgInfo( "Click from Item 5" )
            PICTURE "resource/d_test.png"
         END ITEM
 
      END POPUP
 
      CREATE POPUP "&Help"
 
         CREATE ITEM "About"
            ONCLICK { || mg_MsgInfo("Marinas Version : "+mg_Version() ) }
            PICTURE "resource/d_test.png"
         END ITEM
 
      END POPUP
 
   END MENU
 
Return .T.
 
// Toolbar creation function
 
Function mtoolbar()
 
   CREATE TOOLBAR myTollbar
 
      TOOLTIP "ToolBar ToolTip"
 
 
      CREATE TOOLBUTTON ToolBar_Button_1
         CAPTION "Exit Demo"
         TOOLTIP "Button Exit Tooltip"
         ONCLICK mg_Do( "d_window" , "release" )
         PICTURE "resource/d_test_stop.png"
 
      END TOOLBUTTON
 
      TOOLBARSEPARATOR
 
      CREATE TOOLBUTTON ToolBar_Button_2
         CAPTION "Data Entry"
         TOOLTIP "Start Modal Window"
         ONCLICK  new()
         PICTURE "resource/d_window_modal.png"
      END TOOLBUTTON
 
      TOOLBARSEPARATOR
 
      CREATE TOOLBUTTON ToolBar_Button_4
         CAPTION "About"
         TOOLTIP "Button About Tooltip"
         ONCLICK mg_MsgInfo( mg_Version() )
         PICTURE "resource/d_test.png"
      END TOOLBUTTON
 
      TOOLBARSEPARATOR
 
   END TOOLBAR
 
Return .T.
 
// StatusBar creation function
 
Function mstatusbar()
 
   CREATE STATUSBAR
      CAPTION "Marinas-Gui StatusBar Ready !!!"
      TOOLTIP "StatusBar ToolTip"
   END STATUSBAR
 
Return .T.
 
// Edit window ( modal )
 
 
 
// Save new or edited data  , depends on lNewRecord  variable
 
Function SaveData()
 
if (lNewRecord)
 
    clients->(Dbappend())
 
endif
 
clients->(DbRlock())
 
clients->codigo := val(mg_Get("Modal_1","text_1","value"))
clients->Nombre := mg_Get("Modal_1","text_2","value")
clients->Telefono := mg_Get("Modal_1","text_3","value")
clients->Direccion := mg_Get("Modal_1","text_4","value")
clients->Ciudad := mg_Get("Modal_1","text_5","value")
clients->Fecha := CTOD(mg_Get("Modal_1","text_6","value"))
 
clients->(DBcommit())
clients->(DbRunlock())
 
refresh()
 
mg_Do("Modal_1","Release")
 
return .T.
 
 
// Funcion que crea el Grid para el despligue de los datos de la base clientes
 
Function CreateGrid()
 
      CREATE GRID Style_Grid_1
             ROW    20
             COL    40
             WIDTH  680
             HEIGHT 300
             VALUE  1
             COLUMNWIDTHALL {30,60,150,90,120,120,90}
             COLUMNALIGNALL {"Qt_AlignRight","CENTER","CENTER","CENTER","CENTER","CENTER","CENTER"}
             BACKCOLOR {255,223,255}
             SELECTIONBACKCOLOR {255,0,0}
             COLUMNHEADERALL  {'Rec','Code','Name','Telephone ','Adress','City','Start Date'}
      END GRID
 
 
 
return .T.
 
// Main window edit button's creation
 
function buttons()
 
 
     CREATE BUTTON Style_Button_4
            ROW    450
            COL    540
            WIDTH  60
            HEIGHT 28
            CAPTION "New"
            ONCLICK new()
      END BUTTON
 
     CREATE BUTTON Style_Button_5
            ROW    450
            COL    620
            WIDTH  60
            HEIGHT 28
            CAPTION "Edit"
            ONCLICK Edit()
      END BUTTON
 
      CREATE BUTTON Style_Button_6
            ROW    450
            COL    700
            WIDTH  60
            HEIGHT 28
            CAPTION "Delete"
            ONCLICK DeleteItem()
      END BUTTON
 
return .T.
 
// Database Creation if not exist
 
Function createbase()
   local aStruct := { ;
       { "codigo", "N", 5, 0 }, ;
       { "nombre", "C", 30, 0 }, ;
       { "telefono", "C", 13, 0 }, ;
       { "direccion", "C", 30, 0 }, ;
       { "ciudad", "C", 30, 0 }, ;
       { "fecha", "D", 8, 0 } ;
   }
 
   local aData := { ;
       {10001,"Bruno Luciani","0054349588585","Colon 2323","Ramallo",date()}, ;
       {10002,"Carozo de Quilmes","0054382390849","España 321","Quilmes",date()}, ;
       {10003,"Massimo Belgrano","003489384988","Via benneto 456","Roma",date()}, ;
       {10004,"Francesco Perillo","004568989899","Via Romana 8989","Florence",date()};
     }
 
if file("clients.dbf")
 
  if mg_msgyesno("Do you like to start using a fresh database ?")
     delete file ("clients.dbf")
     delete file ("clients.cdx")
  endif
 
endif
 
if !file("clients.dbf")
 
  dbCreate("clients", aStruct )
 
  use clients
 
  for x:=1 to 4
  clients->(dbappend())
    clients->codigo:=aData[x,1]
    clients->nombre:=aData[x,2]
    clients->telefono:=aData[x,3]
    clients->direccion:=aData[x,4]
    clients->ciudad:=aData[x,5]
    clients->fecha:=aData[x,6]
  clients->(dbcommit())
  next
 
  close clients
 
endif
 
return .T.
 
/////////////////
 
Function new()
lNewRecord:= .T.
load()
refresh()
return
 
 
// Data edit function
function edit()
lNewRecord := .F.
load()
refresh()
 
return
 
 
 
 
Function load()
 
local item_grid:=array(0)
 
if !(lNewRecord)
 
   registro:=mg_Get("d_window","Style_Grid_1","value")
 
   if registro=0
        mg_msginfo("You need to select an item to edit","Error Editing !")
        return
   endif
 
   item_grid:=item_grid:=mg_Get( "d_window" , "Style_Grid_1" , "item" , registro )
 
   go item_grid[1]
 
 
endif
 
 
 
   CREATE WINDOW Modal_1
      ROW 0 ; COL 0
      WIDTH 400 ; HEIGHT 300
      CAPTION "Data Entry "+" - " + Time()
      BACKCOLOR { 219 , 244 , 208 }
      MODAL .T.
 
      CREATE KEY Plus
         ACTION {|| mg_Do( cWin , "Style_Button_1" , "ONCLICK" ) }
      END KEY
 
      CREATE FRAMEBOX Style_Frame_1
             ROW    5
             COL    10
             WIDTH  375
             HEIGHT 275
             CAPTION "Client"
      END FRAMEBOX
 
 
 
     CREATE BUTTON Style_Button_1
            ROW    230
            COL    310
            WIDTH  60
            HEIGHT 28
            CAPTION "Save"
            ONCLICK SaveData()
      END BUTTON
 
* Aca van los textbox
      CREATE LABEL Label_1
             ROW    20
             COL    20
             VALUE "Code:"
             AUTOSIZE .T.
             TRANSPARENT .F.
             TOOLTIP "Label Tooltip"
      END LABEL
 
 
      CREATE TEXTBOX Text_1
             ROW    20
             COL    80
             WIDTH  100
             HEIGHT 24
             ALIGN Qt_AlignRight
             TOOLTIP "Client Number"
             UPPERCASE  .T.
             MAXLENGTH 5
*             BACKCOLOR {0,0,0,}
*             VALUE  ""
      END TEXTBOX
 
 
      CREATE LABEL Label_2
             ROW    50
             COL    20
             VALUE "Name:"
             AUTOSIZE .T.
             TRANSPARENT .F.
             TOOLTIP "Label Tooltip"
      END LABEL
 
 
      CREATE TEXTBOX Text_2
             ROW    50
             COL    80
             WIDTH  200
             HEIGHT 24
             ALIGN Qt_AlignLeft
             TOOLTIP "Client Name"
             UPPERCASE  .T.
             MAXLENGTH 30
*             BACKCOLOR {0,0,0,}
*             VALUE  ""
      END TEXTBOX
 
      CREATE LABEL Label_3
             ROW    80
             COL    20
             VALUE "Telephone:"
             AUTOSIZE .T.
             TRANSPARENT .F.
             TOOLTIP "Label Tooltip"
      END LABEL
 
 
 
      CREATE TEXTBOX Text_3
             ROW    80
             COL    80
             WIDTH  150
             HEIGHT 24
             ALIGN Qt_AlignLeft
             TOOLTIP "Client Telephone"
             UPPERCASE  .T.
             MAXLENGTH 13
*             BACKCOLOR {0,0,0,}
*             VALUE  ""
      END TEXTBOX
 
      CREATE LABEL Label_4
             ROW    110
             COL    20
             VALUE "Adress:"
             AUTOSIZE .T.
             TRANSPARENT .F.
             TOOLTIP "Label Tooltip"
      END LABEL
 
 
      CREATE TEXTBOX Text_4
             ROW    110
             COL    80
             WIDTH  300
             HEIGHT 24
             ALIGN Qt_AlignLeft
             TOOLTIP "Client Adress"
             UPPERCASE  .T.
             MAXLENGTH 30
*             BACKCOLOR {0,0,0,}
*             VALUE  ""
      END TEXTBOX
 
      CREATE LABEL Label_5
             ROW    140
             COL    20
             VALUE "City:"
             AUTOSIZE .T.
             TRANSPARENT .F.
             TOOLTIP "Label Tooltip"
      END LABEL
 
 
      CREATE TEXTBOX Text_5
             ROW    140
             COL    80
             WIDTH  300
             HEIGHT 24
             ALIGN Qt_AlignLeft
             TOOLTIP "Client city"
             UPPERCASE  .T.
             MAXLENGTH 30
*             BACKCOLOR {0,0,0,}
*             VALUE  ""
      END TEXTBOX
 
      CREATE LABEL Label_6
             ROW    170
             COL    20
             VALUE "Fecha Ing.:"
             AUTOSIZE .T.
             TRANSPARENT .F.
             TOOLTIP "Label Tooltip"
      END LABEL
 
 
      CREATE TEXTBOX Text_6
             ROW    170
             COL    80
             WIDTH  100
             HEIGHT 24
             ALIGN Qt_AlignRight
             TOOLTIP "Client start date "
             UPPERCASE  .T.
             MAXLENGTH 8
*            BACKCOLOR {0,0,0,}
             VALUE  dtoc(date())
      END TEXTBOX
 
if !(lNewRecord)
 
   mg_Set("Modal_1","text_1","value",str(Clients->Codigo))
   mg_Set("Modal_1","text_2","value",clients->Nombre)
   mg_Set("Modal_1","text_3","value",clients->Telefono)
   mg_Set("Modal_1","text_4","value",clients->Direccion)
   mg_Set("Modal_1","text_5","value",clients->Ciudad)
   mg_Set("Modal_1","text_6","value",Dtoc(clients->Fecha))
 
endif
 
  mg_Do("Modal_1","text_1","setfocus")
 
   END WINDOW
 
   mg_Do( "Modal_1" , "center" )
 
   mg_Do( "Modal_1" , "activate" )
 
 
 
 
 
return .T.
 
 
// Open base function and index check
 
function openbase()
use clients alias clients
if !FILE("clients.cdx")
   index on Nombre tag nom to clients.cdx
endif
 
 
 
return .T.
 
// close database on main window release
 
function closebase()
close clients
mg_MsgInfo( "Base Closed" , "Base")
return .T.
 
// Grid Data Refresh
 
function refresh()
local data:={}
mg_Do("d_window","Style_Grid_1","deleteallitems")
clients->(dbgotop())
DO while .NOT. clients->(eof())
data:={recno(),str(clients->codigo),clients->nombre,clients->telefono,clients->direccion,clients->ciudad,dtoc(clients->fecha)}
mg_Do("d_window","Style_Grid_1","additem",data)
clients->(Dbskip())
enddo
 
return .T.
 
// Item delete from Database function
 
function DeleteItem()
local item_grid:=array(0)
registro:=mg_Get("d_window","Style_Grid_1","value")
*mg_msginfo(str(registro))
if registro=0
        mg_msginfo("You need to select an Item to delete","Error deleting !")
        return
endif
 
select clients
 
item_grid:=mg_Get( "d_window" , "Style_Grid_1" , "item" , registro )
mg_msginfo("You try to delete Client "+hb_osnewline()+"Client: "+item_grid[3])
go item_grid[1]
clients->(dbrlock())
delete
clients->(dbunlock())
*mg_Do("d_window","Style_Grid_1","deleteitem",registro)
refresh()
return
 
Last Updated on Wednesday, 23 March 2011 11:35  

Links

 


 

Statistics

Members : 1
Content : 11
Web Links : 1
Content View Hits : 47822