Database Application Using SQL Statement in Visual Basic 6

Friday, May 9, 2008

Pada tutorial kali ini saya akan menjelaskan bagaimana cara memanipulasi data menggunakan SQL Statement di Visual Basic. Tujuan tutorial ini adalah agar pembaca bisa memahami bagaimana cara menggunakan SQL Statement untuk memanipulasi data di Visual Basic oleh karena itu tutorial ini dibuat se-jelas dan se-sederhana mungkin tetapi tidak mengesampingkan pemahaman pokok tentang operasi DML dengan SQL Statement.



  • Buat sebuah File Database menggunakan Microsoft Access, simpan di c:\Sample dengan nama DBSample.mdb

  • Buat sebuah table dengan nama Products dengan struktur sebagai berikut :


    • ProductID Text(20)

    • Description Text(50)

    • Price number(Double)


  • Masuk ke Visual Basic, buat sebuah project Standard.EXE simpan di c:\Sample dengan nama SQLDML.vbp

  • Tambahkan tiga buah Textbox : txtPid, txtDesc, dan txtPrice

  • Tambahkan tiga buah Label : lblPid, lblDesc, dan lblPrice

  • Tambahkan tiga buah Button : cmdSave, cmdUpdate, cmdDelete, cmdSearch

  • Tambahkan lagi empat buah button untuk navigasi record : cmdFirst, cmdPrev, cmdNext, cmdLast



  • Application Interface


    Koneksi dengan Database MS Access



  • Masuk code editor, deklarasikan dua buah variabel untuk koneksi Database dan Recordset



  • Dim AccessConn As New ADODB.Connection

    Dim rsProduct As New ADODB.Recordset


  • Dobel klik Form1, tambahkan kode program berikut :



  • Set AccessConn = New ADODB.Connection

    AccessConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _

    "Data Source=" & App.Path & "\DBSample.mdb;Persist Security Info=False"


    'kode program App.Path akan menghasilkan path dari file .exe aplikasi ini

    'yaitu C:\Sample



    'buka koneksi

    AccessConn.Open



    SQLStr = "select * from products"



    Set rsProduct = New ADODB.Recordset

    rsProduct.Open SQLStr, AccessConn, adOpenDynamic, adLockOptimistic, adCmdText




    Menambah Data dengan SQL Insert Statement



  • Dobel klik button cmdSave, tambahkan kode program berikut :



  • Dim SQLStr As String

    'deklarasi variabel SQLStr

    'yang nantinya akan digunakan untuk perintah SQL



    SQLStr = "insert into products (productid,description,price) " & _

    "values('" & txtPid & "','" & txtDesc & "','" & txtPrice & "')"



    'Execute SQL Command

    AccessConn.Execute SQLStr, , cmdtypetext



    'tampilkan pesan

    MsgBox "Data telah tersimpan", vbInformation, "Informasi"


    'refresh dataset rsproduct agar record baru langsung terlihat

    rsProduct.Requery



    'clear textbox

    txtPid = ""

    txtDesc = ""

    txtPrice = ""

    txtPid.SetFocus



    Retrieving data in Visual Basic using SQL Select Statement

    Mencari Data dengan SQL Select Statement


    Karena SQL Select Statement menghasilkan/mengembalikan satu atau lebih record maka perlu dideklarasikan sebuah variabel recordset baru yaitu rsproductsCari untuk menampung recordset hasil query pencarian



  • Dobel klik button cmdSearch, tambahkan kode Program berikut :




  • Dim SQLStr As String

    Dim rsproductsCari As New ADODB.Recordset



    'mencari product berdasarkan productid

    SQLStr = "select * from products where productid = '" & txtPid & "'"



    Set rsproductsCari = New ADODB.Recordset

    rsproductsCari.Open SQLStr, AccessConn, adOpenDynamic, adLockOptimistic, adCmdText



    If Not rsproductsCari.BOF And Not rsproductsCari.EOF Then

    'jika data ada, tampilkan record pada textbox

         txtPid = rsproductsCari!productid

         txtDesc = rsproductsCari!Description

         txtPrice = rsproductsCari!price

    Else

    'jika tidak ada, tampilkan pesan

         MsgBox "Data tidak ditemukan", vbInformation, "Informasi"

         'clear textbox

         txtPid = ""

         txtDesc = ""

         txtPrice = ""

         txtPid.SetFocus

    End If


    Search Data in Visual Basic using SQL Select Statement


    Mengedit Data dengan SQL Update Statement


  • Dobel klik button cmdUpdate, tambahkan kode Program berikut :




  • Dim SQLStr As String



    SQLStr = "update products set description='" & txtDesc & "'," & _

    "price='" & txtPrice & "' where productid='" & txtPid & "'"



    Set rsProduct = AccessConn.Execute(SQLStr, , cmdtypetext)



    'refresh dataset rsproduct agar record baru langsung terlihat

    rsProduct.Requery



    'tampilkan pesan

    MsgBox "Perubahan telah tersimpan", vbInformation, "Informasi"



    'clear textbox

    txtPid = ""

    txtDesc = ""

    txtPrice = ""

    txtPid.SetFocus


    Update Data in Visual Basic using SQL Update Statement in Visual Basic

    Menghapus Data dengan SQL Delete Statement


  • Dobel klik button cmdDelete, tambahkan kode Program berikut :




  • Dim SQLStr As String

    Dim msgresult As Byte

    'deklarasi variabel msgresult

    'digunakan untuk menangkap result dari pesan konfirmasi delete



    msgresult = MsgBox("Hapus Data produk " & txtPid, vbYesNo + vbQuestion, "Confirm")



    If msgresult = vbYes Then

         SQLStr = "delete from products where productid='" & txtPid & "'"

         Set rsProduct = AccessConn.Execute(SQLStr, , cmdtypetext)

         'refresh dataset rsproduct agar record baru langsung terlihat

         rsProduct.Requery


         'clear textbox

         txtPid = ""

         txtDesc = ""

         txtPrice = ""

         txtPid.SetFocus

    End If


    Delete Data in Visual Basic using SQL Delete Statement


    Navigasi Record Dengan Kode Program


    Ini adalah kode Program untuk navigasi record ke posisi record pertama.


  • Dobel klik button cmdFirst, tambahkan kode Program berikut :



  • On Error Resume Next

    rsProduct.MoveFirst

         'menampilkan data

         txtPid = rsProduct!productid

         txtDesc = rsProduct!Description

         txtPrice = rsProduct!price





    Kode Program untuk navigasi record ke posisi record sebelumnya. jika record sudah di posisi record pertama, maka pointer tetap akan diarahkan ke record pertama. kamu bisa ganti kode Program rsproduct.MoveFirst dengan rsproduct.MoveLast agar navigasi kembali ke record terakhir setelah berada pada record pertama.


  • Dobel klik button cmdPrev, tambahkan kode Program berikut :



  • On Error Resume Next

    rsProduct.MovePrevious



    If rsProduct.BOF Then

         rsProduct.MoveFirst

    End If

         'menampilkan data

         txtPid = rsProduct!productid

         txtDesc = rsProduct!Description

         txtPrice = rsProduct!price



    Kode Program untuk navigasi record ke posisi record berikutnya. jika record sudah di posisi record terakhir, maka pointer tetap akan diarahkan ke record terakhir. kamu bisa ganti kode Program rsproduct.MoveLast dengan rsproduct.MoveFirst agar navigasi kembali ke record pertama setelah berada pada record terakhir.


  • Dobel klik button cmdNext, tambahkan kode Program berikut :



  • On Error Resume Next

    rsProduct.MoveNext



    If rsProduct.EOF Then

         rsProduct.MoveLast

    End If

         'menampilkan data

         txtPid = rsProduct!productid

         txtDesc = rsProduct!Description

         txtPrice = rsProduct!price



    Kode Program untuk navigasi record ke posisi record terakhir.



  • Dobel klik button cmdLast, tambahkan kode Program berikut :



  • On Error Resume Next

    rsProduct.MoveLast

         'menampilkan data

         txtPid = rsProduct!productid

         txtDesc = rsProduct!Description

         txtPrice = rsProduct!price



    Kamu bisa Download Source Code Aplikasi ini disini. Semoga bermanfaat

    3 comments:

    Anonymous said...

    Sorry nih Paduka21 request artikelnya baru bisa di publish sekarang maklum lagi banyak kerjaan. mudah-mudahan ga telat nih buat tugasnya.

    Unknown said...

    http://untoong.blogspot.com


    thx for tutorial

    agus suhardi said...

    artikelnya keren gan, tapi file downloadnya g' ada
    thanks artikelnya gan, sangat membatu

    Recent Comments

    Tags Cloud

    Blogumulus by Roy Tanck and Amanda Fazani