Hello sob… ketemu lagi nih sama saya. Untuk kali ini saya
akan membahas tentang “Gimana sih caranya
mengkoneksikan access ke VB dan buat ID-nya berjalan dengan otomatis?”
.
Oke kawand udah gak sabar ya…. Kita lansung saja.
Pertama kita buat database di access dan beri nama “id”. Simpan database-nya di computer anda dan buat folder baru dengan nama “ID”. Selanjutnya kita buat tabel
dan field dengan keterangan sebagai berikut :
--Tabel id dengan fieldnya :
-
id (Text),
primary key
Dan isilah data di Tabel id (minimal 3).
Kalau sudah, kita buka VB 6-nya dan buat Form
baru. Dan tambahkan Component ADODC dan DataGrid-nya. Dan atur posisinya
seperti berikut :
--Pengaturan
Properties :
Object
|
Properties
|
Nilai
|
Form1
|
StartUpPosition
|
2-CenterScreen
|
Label1
|
Caption
|
ID
|
Label2
|
Caption
|
Nama
|
Command1
|
Caption
|
Baru
|
Command2
|
Caption
|
Simpan
|
Command3
|
Caption
|
Ubah
|
Command4
|
Caption
|
Hapus
|
Timer1
|
Interval
|
500
|
Adodc1
|
Visible
|
False
|
--Sekarang kita kasih Coding di Form1 :
Dim lokbase As String
lokbase = App.Path & "\id.mdb"
On Error Resume Next
Adodc1.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & lokbase &
";Persist Security Info=False"
Adodc1.RecordSource = "select * from id"
Set DataGrid1.DataSource = Adodc1
--Dan Coding di Timer1 :
Dim z As Integer
z = 1
z = z + Adodc1.Recordset.RecordCount
Text1.Text = ("N" & z)
If (Text1.Text = Adodc1.Recordset!ID) Then
Text1.Text =
("A" & z)
If (Text1.Text = Adodc1.Recordset!ID) Then
Text1.Text =
("P" & z)
End If
End If
--Dan Coding di DataGrid1 (DataGrid_MouseDown)
:
Dim row As Long, col As Long
On Error Resume Next
row = DataGrid1.RowContaining(Y)
Text1.Text =
DataGrid1.Columns(0).CellValue(DataGrid1.RowBookmark(row))
Text2.Text =
DataGrid1.Columns(1).CellValue(DataGrid1.RowBookmark(row))
Timer1.Enabled = False
--Dan Coding di Baru (Command1) :
Timer1.Enabled = True
Text2.Text = ""
--Dan Coding di Simpan (Command2) :
If (Text1.Text <> "") And (Text2.Text
<> "") Then
With
Adodc1.Recordset
.AddNew
!ID =
Text1.Text
!nama =
Text2.Text
.Update
End With
Timer1.Enabled =
True
Text2.Text =
""
Else
MsgBox "Harap
Lengkapi Isian", vbCritical, "Lengkapi"
End If
--Dan Coding di Ubah (Command3) :
If (Text1.Text <> "") And (Text2.Text
<> "") Then
With
Adodc1.Recordset
!ID =
Text1.Text
!nama =
Text2.Text
.Update
End With
Timer1.Enabled =
True
Text2.Text =
""
Else
MsgBox "Harap
Lengkapi Isian", vbCritical, "Lengkapi"
End If
--Dan yang terakhir Coding di Hapus (Command4) :
Beep
If MsgBox("Anda Yakin Ingin Menghapus Data",
vbQuestion + vbYesNoCancel, "Hapus Data") = vbYes Then
With
Adodc1.Recordset
.Delete
End With
Timer1.Enabled =
True
Text2.Text =
""
Else
Exit Sub
End If
--Dan inilah Coding untuk keseluruhannya :
Private Sub Command1_Click()
Timer1.Enabled = True
Text2.Text = ""
End Sub
Private Sub Command2_Click()
If (Text1.Text <> "") And (Text2.Text
<> "") Then
With
Adodc1.Recordset
.AddNew
!ID =
Text1.Text
!nama =
Text2.Text
.Update
End With
Timer1.Enabled =
True
Text2.Text =
""
Else
MsgBox "Harap
Lengkapi Isian", vbCritical, "Lengkapi"
End If
End Sub
Private Sub Command3_Click()
If (Text1.Text <> "") And (Text2.Text
<> "") Then
With
Adodc1.Recordset
!ID =
Text1.Text
!nama =
Text2.Text
.Update
End With
Timer1.Enabled =
True
Text2.Text =
""
Else
MsgBox "Harap
Lengkapi Isian", vbCritical, "Lengkapi"
End If
End Sub
Private Sub Command4_Click()
Beep
If MsgBox("Anda Yakin Ingin Menghapus Data",
vbQuestion + vbYesNoCancel, "Hapus Data") = vbYes Then
With Adodc1.Recordset
.Delete
End With
Timer1.Enabled =
True
Text2.Text =
""
Else
Exit Sub
End If
End Sub
Private Sub DataGrid1_MouseDown(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Dim row As Long, col As Long
On Error Resume Next
row = DataGrid1.RowContaining(Y)
Text1.Text =
DataGrid1.Columns(0).CellValue(DataGrid1.RowBookmark(row))
Text2.Text =
DataGrid1.Columns(1).CellValue(DataGrid1.RowBookmark(row))
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
Dim lokbase As String
lokbase = App.Path & "\id.mdb"
On Error Resume Next
Adodc1.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & lokbase &
";Persist Security Info=False"
Adodc1.RecordSource = "select * from id"
Set DataGrid1.DataSource = Adodc1
End Sub
Private Sub Timer1_Timer()
Dim z As Integer
z = 1
z = z + Adodc1.Recordset.RecordCount
Text1.Text = ("N" & z)
If (Text1.Text = Adodc1.Recordset!ID) Then
Text1.Text =
("A" & z)
If (Text1.Text = Adodc1.Recordset!ID) Then
Text1.Text =
("P" & z)
End If
End If
End Sub
Kalau semuanya sudah selesai, kita simpan dulu Project
dan Form-nya.
Simpan di folder “ID” yang telah
kita buat tadi.
Ket : Project, Form dan database
Acccess-nya harus di simpan dalam satu folder. Karna kalau tidak di
simpan di satu folder, program tidak akan berjalan (Error).
Setelah tersimpan semua, kita buka lagi Project-nya dan jalankan (F5).
Dan inilah hasilnya :
Gimanba sob…. Bagus gak tuh…. Kalau kalian pengen yang sudah jadi, download saja programnya
di bawah ini :
-- ID BERJALAN
No comments:
Post a Comment
Komentar, pesan, kritik, dan saran...