Kemudian ubah name seperti dibawah ini :
- Pertama kita buat Sub Aktif ( fungsi ketika kita pilih add maka semuanya aktif)
Sub aktif()
nokode.Enabled = True
tgl1.Enabled = True
tgl2.Enabled = True
tcari.Enabled = True
End Sub
nokode.Enabled = True
tgl1.Enabled = True
tgl2.Enabled = True
tcari.Enabled = True
End Sub
- Buat Sub nonaktif ( fungsi ketika form dijalankan semuanya dalam kondisi nonaktif, kecuali add)
Sub nonaktif()
nokode.Enabled = False
tgl1.Enabled = False
tgl2.Enabled = False
tcari.Enabled = False
cmdsave.Enabled = False
cmdcancel.Enabled = False
cmdedit.Enabled = False
cmdupdate.Enabled = False
cmddelete.Enabled = False
End Sub
nokode.Enabled = False
tgl1.Enabled = False
tgl2.Enabled = False
tcari.Enabled = False
cmdsave.Enabled = False
cmdcancel.Enabled = False
cmdedit.Enabled = False
cmdupdate.Enabled = False
cmddelete.Enabled = False
End Sub
- Buat sub bersih
Sub bersih()
tgl1.Text = ""
tgl2.Text = ""
tcari.Text = ""
nokode.Text = ""
End Sub
tgl1.Text = ""
tgl2.Text = ""
tcari.Text = ""
nokode.Text = ""
End Sub
- Buat form-load
Private Sub Form_Load()
Call nonaktif
cmdadd.Enabled = True
End Sub
Call nonaktif
cmdadd.Enabled = True
End Sub
- buat sub tampil :
Sub tampil()
Call koneksi
rstanggaltest.Open "select*from tanggaltest", KON
Set grid.DataSource = rstanggaltest
End Sub
Call koneksi
rstanggaltest.Open "select*from tanggaltest", KON
Set grid.DataSource = rstanggaltest
End Sub
Silahkan copy kode listing berikut dan pastekan sesuai name nya :
- cmdadd:
Private Sub cmdadd_Click()
Call koneksi
Call bersih
Call aktif
rstanggaltest.Open "select*from tanggaltest order by no_kode desc", KON
With rstanggaltest
If .BOF And .EOF Then
nokode.Text = "T" + "001"
Else
nokode.Text = "T" + Right(Str(Val(Right(.Fields("no_kode"), 3)) + 1001), 3)
End If
End With
tgl1.SetFocus
cmdsave.Enabled = True
cmdcancel.Enabled = True
cmddelete.Enabled = False
End Sub
Call koneksi
Call bersih
Call aktif
rstanggaltest.Open "select*from tanggaltest order by no_kode desc", KON
With rstanggaltest
If .BOF And .EOF Then
nokode.Text = "T" + "001"
Else
nokode.Text = "T" + Right(Str(Val(Right(.Fields("no_kode"), 3)) + 1001), 3)
End If
End With
tgl1.SetFocus
cmdsave.Enabled = True
cmdcancel.Enabled = True
cmddelete.Enabled = False
End Sub
- cmdsave
Private Sub cmdsave_Click()
Call koneksi
rstanggaltest.Open "insert into tanggaltest values('" & nokode & "','" & tgl1 & "','" & tgl2 & "')", KON
MsgBox "Data Sudah Tersimpan", vbInformation
Call tampil
Call nonaktif
End Sub
Call koneksi
rstanggaltest.Open "insert into tanggaltest values('" & nokode & "','" & tgl1 & "','" & tgl2 & "')", KON
MsgBox "Data Sudah Tersimpan", vbInformation
Call tampil
Call nonaktif
End Sub
-cmdcancel :
Private Sub cmdcancel_Click()
bersih
tgl1.SetFocus
Call nonaktif
End Sub
bersih
tgl1.SetFocus
Call nonaktif
End Sub
- cmdedit :
Private Sub cmdedit_Click()
Call koneksi
rstanggaltest.Open "select*from tanggaltest where no_kode='" & tcari.Text & "'", KON
With rstanggaltest
nokode.Text = .Fields("no_kode")
tgl1.Text = .Fields("tanggaltest1")
tgl2.Text = .Fields("tanggaltest2")
End With
nokode.Enabled = False
tgl1.SetFocus
cmdupdate.Enabled = True
cmddelete.Enabled = True
cmdcancel.Enabled = False
End Sub
Call koneksi
rstanggaltest.Open "select*from tanggaltest where no_kode='" & tcari.Text & "'", KON
With rstanggaltest
nokode.Text = .Fields("no_kode")
tgl1.Text = .Fields("tanggaltest1")
tgl2.Text = .Fields("tanggaltest2")
End With
nokode.Enabled = False
tgl1.SetFocus
cmdupdate.Enabled = True
cmddelete.Enabled = True
cmdcancel.Enabled = False
End Sub
- cmdupdate :
Private Sub cmdupdate_Click()
Call koneksi
rstanggaltest.Open "update tanggaltest set tanggaltest1='" & tgl1 & "',tanggaltest2='" & tgl2 & "' where no_kode='" & nokode.Text & "'", KON
MsgBox "Data Berhasil di Update", vbInformation, "Info"
bersih
Call tampil
cmdupdate.Enabled = False
cmdedit.Enabled = False
cmdadd.Enabled = True
End Sub
Call koneksi
rstanggaltest.Open "update tanggaltest set tanggaltest1='" & tgl1 & "',tanggaltest2='" & tgl2 & "' where no_kode='" & nokode.Text & "'", KON
MsgBox "Data Berhasil di Update", vbInformation, "Info"
bersih
Call tampil
cmdupdate.Enabled = False
cmdedit.Enabled = False
cmdadd.Enabled = True
End Sub
- cmddelete :
Private Sub cmddelete_Click()
Call koneksi
a = MsgBox("Yakin Ingin Hapus Data ini?", vbQuestion + vbYesNo, "tanya")
If a = vbYes Then
rstanggaltest.Open "delete from tanggaltest where no_kode='" & tcari.Text & "'", KON
bersih
tcari.Text = ""
End If
End Sub
Call koneksi
a = MsgBox("Yakin Ingin Hapus Data ini?", vbQuestion + vbYesNo, "tanya")
If a = vbYes Then
rstanggaltest.Open "delete from tanggaltest where no_kode='" & tcari.Text & "'", KON
bersih
tcari.Text = ""
End If
End Sub
- bmenu :
Private Sub Command1_Click()
fmenu.Show
End Sub
fmenu.Show
End Sub
- exit :
Private Sub EXIT_Click()
a = MsgBox("Yakin Keluar", vbCritical + vbYesNo, "INFO")
If a = vbYes Then
End
End If
End Sub
a = MsgBox("Yakin Keluar", vbCritical + vbYesNo, "INFO")
If a = vbYes Then
End
End If
End Sub
- tcari (ubah declarations nya ke "change"):
Private Sub tcari_Change()
Call koneksi
rstanggaltest.Open "select*from tanggaltest where no_kode like '" & tcari.Text & "'", KON
If Not tcari.Text = "" Then
Set grid.DataSource = rstanggaltest
Else
Call tampil
End If
cmdedit.Enabled = True
cmdadd.Enabled = False
cmdsave.Enabled = False
End Sub
Call koneksi
rstanggaltest.Open "select*from tanggaltest where no_kode like '" & tcari.Text & "'", KON
If Not tcari.Text = "" Then
Set grid.DataSource = rstanggaltest
Else
Call tampil
End If
cmdedit.Enabled = True
cmdadd.Enabled = False
cmdsave.Enabled = False
End Sub
0 Komentar