Membuat CRUD pada Visual Foxpro

 


Berikut adalah cara membuat proses CRUD ( Create , Read, Update,  Delete ) pada Visual Foxpro :

1. Buat table seperti gambar dibawah ini : tblharga.dbf yang terdiri dari fields :

kode ( kode barang ), nama ( nama barang ) dan harga


2. Design Form seperti gambar dibawah :



Isikan : 1 groupbox, 3 label, 3 textbox ( txtkode,  txtnama, txtharga),  1 grid, 5 command  (cmdsave, cmdedit, cmddelete, cmdcancel, cmdexit )

3.  Tambahkan script pada masing-masing object control yang terkait :

3.1. Object Form

Object Form1 Procedure Init

SET TALK OFF
SET DELETE on
SET EXCLUSIVE on
SET EXACT ON
IF !USED("tblharga")
SELECT 1 
USE tblharga EXCLUSIVE
ENDIF
thisform.txtkode.Value =''
thisform.txtnama.Value =''
thisform.txtharga.Value =0  
thisform.txtnama.Enabled = .F. 
thisform.txtharga.Enabled = .F.   
thisform.cmdsave.Enabled = .F.
thisform.cmdedit.Enabled = .F. 
thisform.cmddelete.Enabled = .F.     
thisform.txtkode.SetFocus  

Object Form1 procedure Load

PUBLIC baru
baru =0


3.2. Object Command Save

Object cmdsave procedure click

 IF EMPTY(thisform.txtkode.Value)

   =MESSAGEBOX("Kode Barang belum diisi",0+64,"Info")

   RETURN

ENDIF

IF EMPTY(thisform.txtnama.Value)

   =MESSAGEBOX("Nama Barang belum diisi",0+64,"Info")

   RETURN

ENDIF

IF EMPTY(thisform.txtharga.Value)

   =MESSAGEBOX("Harga Barang belum diisi",0+64,"Info")

   RETURN

ENDIF

SELECT tblharga

SET ORDER To tag kode

kdb=ALLTRIM(thisform.txtkode.Value)

nmb=ALLTRIM(thisform.txtnama.Value)

hb= thisform.txtharga.Value

IF baru=0

  INSERT INTO tblharga(kode,nama,harga) values(kdb,nmb,hb)

  ELSE

  UPDATE tblharga set nama=nmb,harga=hb WHERE kode=kdb

ENDIF

  thisform.cmdcancel.Click


3.3. Object Command Edit

Object cmdedit procedure click

 this.Enabled = .F. 

thisform.cmdsave.Enabled = .T.  

baru = 1

thisform.txtkode.Enabled = .F. 

thisform.txtnama.Enabled = .T. 

thisform.txtharga.Enabled = .T. 

thisform.txtnama.SetFocus 

 

3.4.  Object Command Delete

Object cmddelete procedure click

 IF MESSAGEBOX("Data ini mau dihapus?",4+32,"Konfirmasi")=7

thisform.cmdcancel.Click

ENDIF

SELECT tblharga

SET ORDER TO tag kode

DELETE

thisform.cmdcancel.Click 


3.5.  Object Command Cancel

Object cmdcancel procedure click

 thisform.txtkode.Value =''

thisform.txtnama.Value =''

thisform.txtharga.Value =0

thisform.txtnama.Enabled = .F. 

thisform.txtharga.Enabled = .F. 

thisform.cmdsave.Enabled = .F.

thisform.cmdedit.Enabled = .F. 

thisform.cmddelete.Enabled = .F.    

thisform.Refresh

thisform.txtkode.Enabled = .T.  

thisform.txtkode.SetFocus   

 

 3.6.  Object Command Exit

 Object cmdexit procedure click

thisform.Release


Layout, silahkan lakukan proses CRUD