Koneksi VB dengan database acces dengan ADODC



  • Pertama buatlah database dari M.Acces, dan beri nama coba.mdb
  • Kemudian buatlah tabel dengan nama tabel anggota dengan 3 field yaitu id sebagai primary key, nama dan alamat dengan tipe data sama yaitu text.
  • Lalu isikan beberapa data pada table tersebut (3 sudah cukup).
  • Selanjutnya buka Microsoft Visual Basic lalu plih standart.exe.
  • Tekan ctrl+t untuk menambah komponen Datagrid dan Adodc.
  • Setelah menekan ctrl+t pilih komponen Microsoft ADO Data Control 6.0 (OLEDB) dan Microsoft Datagrid Control 6.0 (OLEDB) kemudian apply dan ok.
  • Setelah itu tambahkanlah 3 label, 3 textbox, 4 command, 1 adodc dan 1 datagrid pada form.
  • Atur posisi komponen tadi seperti gambar dibawah ini :
  • Kemudian ketikkan cooding berikut pada form_load
Dim LokasiDatabase As String
LokasiDatabase = App.Path & "\coba.mdb"
On Error Resume NextAdodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & LokasiDatabase & ";Persist Security Info=False"
Adodc1.RecordSource = "select * from anggota"
Set DataGrid1.DataSource = Adodc1
  • Kemudian ketikkan cooding berikut pada command1 (Tambah) :
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
  • Kemudian ketikkan cooding berikut pada command2 (Simpan) :
If (Text1.Text <> "") And (Text2.Text <> "") And (Text3.Text <> "") Then
Adodc1.Recordset.AddNew
Adodc1.Recordset!id = Text1
Adodc1.Recordset!nama = Text2
Adodc1.Recordset!alamat = Text3
Adodc1.Recordset.Update
Else
MsgBox "Mohon Diisi dengan Lengkap", vbCritical, "Peringatan"
End If
  • Kemudian ketikkan cooding berikut pada command3 (Edit) :
If (Text1.Text <> "") And (Text2.Text <> "") And (Text3.Text <> "") Then
With Adodc1.Recordset
.Fields(0) = Text1.Text
.Fields(1) = Text2.Text
.Fields(2) = Text3.Text
End With
Else
MsgBox "Mohon Diisi dengan Lengkap", vbCritical, "Peringatan"
End If
  • Kemudian ketikkan cooding berikut pada command4 (Hapus) :
If MsgBox("Apa anda yakin ???", vbSystemModal + vbYesNo + vbQuestion, "Kepastian") = vbYes Then
Adodc1.Recordset.Delete
End If
  • Setelah semua selesai, cobalah running (F5) program anda. VB sudah terhubung dengan database Acces dan anda bisa menambah, mengedit, dan menghapus data melalui VB.
  • Nb : Data Microsoft Acces (coba.mdb) dan Form serta Project VB diletakkan dalam satu folder yang sama.
Terima kasih, Semoga bermanfaat.


Responses

0 Respones to "Koneksi VB dengan database acces dengan ADODC"

Posting Komentar

Return to top of page Copyright © 2011 | Platinum Theme Converted into Blogger Template by Hack Tutors