Membuat Form Pegawai

Pada kesempatan kali ini saya coba kan berbagi pengetahuan cara membuat form pegawai dengan visual basic.Buat dulu design form seperti gambar di samping..
Setelah itu masukkan listing di bawah ini..
Untuk nama teks,Command button,Modul,database,combobox,sesuaikan dengan nama yang anda buat. Pada posting kali ini koneksi data base nya tidak menggunakan adodc melainkan menggunakan modul dengan listing.Caranya Ketik Project-->Pilih Add Modul.
Kemudian Ketikkan listing dibawah ini:


Public dbado As New ADODB.Connection
Public RsPeg As New ADODB.Recordset

Public Sub conn()
Set dbado = New ADODB.Connection
dbado.Open "Provider=MSDASQL.1;Persist Security Info=False;Data Source=penggajian_swb"
dbado.CursorLocation = adUseClient
'dbado.Open
End Sub
keterangan :dbado = Variable untuk nama  Koneksi (pemberian nama terserah)
RsPeg = Variable Recordset tabel Pegawai (pemberian nama terserah)
Sub Conn = Procedure Koneksi (pemberian nama terserah)
penggajian_swb= Nama Database (Sesuaikan dengan nama data Base)


Simpan modul dalam satu folder dengan project anda.


Kemudian ketikkan listing dibawah ini pada form anda:

Public isi As Boolean
Sub tampil()
Text1.Text = RsPeg!nip
Text2.Text = RsPeg!napeg
Text3.Text = RsPeg!alamat
Text4.Text = RsPeg!kota
Text5.Text = RsPeg!notelp
Combo1.Text = RsPeg!pendidikan
End Sub
Sub bersih()
For Each x In Me
If TypeOf x Is TextBox Then
   x.Text = ""
End If
Next
Combo1.Text = ""
End Sub
Sub nonaktif()
For Each x In Me
If TypeOf x Is TextBox Then
   x.Enabled = False
End If
Next
Combo1.Enabled = False
End Sub

Sub aktif()
For Each x In Me
If TypeOf x Is TextBox Then
   x.Enabled = True
End If
Next
Combo1.Enabled = True
End Sub

 
Sub awal()
Dim strsql As String
strsql = "select * from pegawai order by nip"
Set RsPeg = dbado.Execute(strsql, , adCmdText)
Call conn
Set Grid1.DataSource = RsPeg
bersih
nonaktif
End Sub

Private Sub Command1_Click()
awal
isi = True
aktif
bersih
Text1.SetFocus
End Sub

Private Sub Command2_Click()
Dim simpan As String
Dim ubah As String
If Text1 = "" Then
    MsgBox "Nip harus diisi !", vbExclamation, "Konfirmasi"
    Text1.SetFocus
End If
If isi = True Then
    simpan = "insert into pegawai() values('" & Text1 & "','" & Text2 & "','" & Text3 & "','" & Text4 & "','" & Text5 & "','" & Combo1 & "')"
    Set RsPeg = dbado.Execute(simpan, , adCmdText)
Else
    ubah = "update pegawai set napeg='" & Text2 & "',alamat='" & Text3 & "',kota='" & Text4 & "',notelp='" & Text5 & "',pendidikan='" & Combo1 & "' where nip='" & Text1 & "'"
    Set RsPeg = dbado.Execute(ubah, , adCmdText)
End If
awal
MsgBox "data telah tersimpan"
bersih
nonaktif
End Sub

 
Private Sub Command3_Click()
Dim cari As String
Dim hapus As String
x = InputBox("masukan NIP yang dicari", "cari kode")
cari = "select *from pegawai where nip='" & x & "'"
hapus = "delete from pegawai where nip='" & x & "'"
Set RsPeg = dbado.Execute(cari, , adCmdText)
With RsPeg
    If .BOF And .EOF Then
        MsgBox "Kode yg dicari tdk ada", vbCritical, "Kesalahan"
    Else
        tampil
        h = MsgBox("Bener mo dihapus?", vbYesNo + vbQuestion, "Konformasi")
        If h = vbYes Then
            Set RsPeg = dbado.Execute(hapus, , adCmdText)
        End If
        Grid1.Refresh
        awal
    End If
End With
End Sub
Private Sub Command4_Click()
Dim cari As String
x = InputBox("masukan NIP yang dicari", "cari kode")
cari = "select *from pegawai where nip='" & x & "'"
Set RsPeg = dbado.Execute(cari, , adCmdText)
With RsPeg
    If .BOF And .EOF Then
        MsgBox "Kode yg dicari tdk ada", vbCritical, "Kesalahan"
        Grid1.Refresh
        awal
    Else
        tampil
    End If
End With
isi = False
aktif
Text1.Enabled = False
End Sub

Private Sub Command5_Click()
awal
End Sub
Private Sub Command6_Click()
Unload Me
End Sub
Private Sub Command7_Click()
Dim cetak As String
cetak = "SELECT * from pegawai order by nip"
Set RsPeg = dbado.Execute(cetak, , adCmdText)
Crpeg.ReportFileName = App.Path & "\crpeg.rpt"
Crpeg.Destination = crptToWindow
Crpeg.WindowState = crptMaximized
Crpeg.WindowTitle = "Cetak Data Pegawai"
Crpeg.RetrieveDataFiles
Crpeg.Action = 1
End Sub

 
Private Sub Form_Activate()
awal
Combo1.AddItem ("SMA")
Combo1.AddItem ("D3")
Combo1.AddItem ("S1")
End Sub



Private Sub Form_Load()
Call conn
'Set RsPeg = New ADODB.Recordset
'RsPeg.Open "select * from pegawai", dbado, adOpenDynamic, adLockOptimistic
'Set Grid1.DataSource = RsPeg
awal
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Dim cari As String
cari = "select * from pegawai where nip='" & Text1.Text & "'"
Set RsPeg = dbado.Execute(cari, , adCmdText)
With RsPeg
    If .BOF And .EOF Then
        t = MsgBox("Kode " & Text1 & " belum ada, mo tambah data lagi?", vbYesNo + vbQuestion, "Konfirmasi")
        If t = vbYes Then
            'isipeg = True
            Call aktif
            Text2.SetFocus
            Grid1.Refresh
            'cmdsimpan.Enabled = True
            'cmdbatal.Enabled = True
        Else
            Call awal
        End If
    Else
        MsgBox "Kode " & Text1 & " sudah ada", 64, "Ketemu"
        tampil
        Text1.Enabled = False
        'Text1.BackColor = &H80000000
        'cmdhapus.Enabled = True
        'cmdtambah.Enabled = True
        'cmdubah.Enabled = True
        'cmdubah.SetFocus
        'cmdsimpan.Enabled = False
    End If
End With
End If
End Sub
Selamat mencoba dan semoga berhasil...