Database DAO

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Membuat database dengan menggunakan DAO (DATA)

Buatlah form seperti pada gambar dibawah ini :

Kemudian masukkan code dibawah ini:

Dim dbs As DAO.Database


Dim dbs2 As DAO.Database

Dim rstUser As DAO.Recordset


'---
Dim rstSimA As DAO.Recordset
Dim rstSimAU As DAO.Recordset
Dim rstSimB1 As DAO.Recordset
Dim rstSimB1U As DAO.Recordset
Dim rstSimB2 As DAO.Recordset
Dim rstSimB2U As DAO.Recordset
Dim rstSimC As DAO.Recordset
Dim rstSimD As DAO.Recordset
Public Sub koneksiDB()
On Error Resume Next
Set dbs = DBEngine.OpenDatabase(App.Path &
"\pendaftaran.mdb")
Set dbs2 = DBEngine.OpenDatabase(App.Path &
"\user.mdb")

Set rstUser = dbs2.OpenRecordset("tbuser", dbOpenTable)

Set rstSimA = dbs.OpenRecordset("tbsima", dbOpenTable)


Set rstSimAU = dbs.OpenRecordset("tbsimau",
dbOpenTable)
Set rstSimB1 = dbs.OpenRecordset("tbsimb1",
dbOpenTable)
Set rstSimB1U = dbs.OpenRecordset("tbsimb1u",
dbOpenTable)
Set rstSimB2 = dbs.OpenRecordset("tbsimb2",
dbOpenTable)
Set rstSimB2U = dbs.OpenRecordset("tbsimb2u",
dbOpenTable)
Set rstSimC = dbs.OpenRecordset("tbsimc", dbOpenTable)
Set rstSimD = dbs.OpenRecordset("tbsimd", dbOpenTable)

End Sub

Public Sub dbSIM(mySIM As Integer)


'On Error Resume Next
Dim jk As String

koneksiDB

If mySIM = 0 Then
With rstSimA
.Index = "idxsima"

.AddNew
!nopendaftaran = txtNoPendaftaran.Text
!kategori = cboKategori.Text
!noujian = txtNoUjian.Text

!namapendaftar = txtNamaPendaftar.Text
If (opJk1.Value = True) And (OpJK2.Value =
False) Then
jk = "1"
Else
jk = "2"
End If
!jk = jk
!tempatlahir = txtTempatLahir.Text
!tanggallahir = txtTanggalLahir.Text
!alamat = txtAlamat.Text
!tinggi = txtTinggi.Text
!pekerjaan = txtPekerjaan.Text
!notmp = txtTmpNoUjian.Text
!tanggalsekarang = txtTanggalSekarang.Text
.Update
End With
dbUser 0
GoTo simpanSukses
End If

simpanSukses:

MsgBox "Data berhasil disimpan", vbInformation,


"Help Center"
Unload Me

End Sub

Public Sub dbUser(myUser As Integer)


'On Error Resume Next
koneksiDB

If myUser = 0 Then
With rstUser
.Index = "idxuser"

.AddNew
!nopendaftaran = txtNoPendaftaran.Text
!kategori = cboKategori.Text
!no_ujian = txtNoUjian.Text

!nama = txtNamaPendaftar.Text
If (opJk1.Value = True) And (OpJK2.Value =
False) Then
jk = "1"
Else
jk = "2"
End If
!jk = jk
!tempatlahir = txtTempatLahir.Text
!tgllahir = txtTanggalLahir.Text
!alamat = txtAlamat.Text
!tinggi = txtTinggi.Text
!pekerjaan = txtPekerjaan.Text
!notmp = txtTmpNoPendaftaran.Text
.Update
End With
End If

End Sub

Public Sub buatNomorPendaftaran()


On Error Resume Next
Dim nomor As Integer
Dim digit As String
Dim teksNomor As String

textnomor = txtTmpNoPendaftaran.Text

koneksiDB

rstUser.Index = "idxuser"

If rstUser!notmp = 0 Then
nomor = 1
digit = "0000"
txtNoPendaftaran.Text = digit & nomor
txtTmpNoUjian.Text = nomor
Else
rstUser.MoveLast
nomor = Int(rstUser!notmp) + 1
teksNomor = nomor
MsgBox Len(teksNomor)
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
rstUser.MoveNext

txtNoPendaftaran.Text = digit & nomor


txtTmpNoPendaftaran.Text = nomor
End If
End Sub
Public Sub isiKategori()
cboKategori.Clear

With cboKategori
.AddItem "SIM A"
.AddItem "SIM A UMUM"
.AddItem "SIM BI"
.AddItem "SIM BI UMUM"
.AddItem "SIM BII"
.AddItem "SIM BII UMUM"
.AddItem "SIM C"
.AddItem "SIM D"

.ListIndex = 0
End With
End Sub

Private Sub cboKategori_Click()


On Error Resume Next
Dim nomor As Integer
Dim digit As String
Dim teksNomor As String

koneksiDB

If cboKategori.ListIndex = 0 Then
With rstSimA
.Index = "idxsima"

If !notmp = 0 Then
nomor = 1
If Len(!notmp) = 1 Then
digit = "0000"
ElseIf Len(!notmp) = 2 Then
digit = "000"
ElseIf Len(!notmp) = 3 Then
digit = "00"
ElseIf Len(!notmp) = 4 Then
digit = "0"
Else
Exit Sub
End If
txtNoUjian.Text = "A" & digit & nomor
txtTmpNoUjian.Text = nomor
Else
.MoveLast
nomor = Int(!notmp) + 1
teksNomor = nomor
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
.MoveNext

txtNoUjian.Text = "A" & digit & nomor


txtTmpNoUjian.Text = nomor
End If
End With
ElseIf cboKategori.ListIndex = 1 Then
With rstSimAU
.Index = "idxsimau"

If !notmp = "" Then


nomor = 1
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
txtNoUjian.Text = "AU" & digit & nomor
txtTmpNoUjian.Text = nomor
Else
.MoveLast
nomor = Int(!notmp) + 1
teksNomor = nomor
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
.MoveNext
txtNoUjian.Text = "AU" & digit & nomor
txtTmpNoUjian.Text = nomor
End If
End With
ElseIf cboKategori.ListIndex = 2 Then
With rstSimB1
.Index = "idxsimb1"

If !notmp = 0 Then
nomor = 1
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
txtNoUjian.Text = "BI" & digit & nomor
txtTmpNoUjian.Text = nomor
Else
.MoveLast
nomor = Int(!notmp) + 1
teksNomor = nomor
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
.MoveNext
txtNoUjian.Text = "BI" & digit & nomor
txtTmpNoUjian.Text = nomor
End If
End With
ElseIf cboKategori.ListIndex = 3 Then
With rstSimB1U
.Index = "idxsimb1u"

If !notmp = 0 Then
nomor = 1
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
txtNoUjian.Text = "BIU" & digit & nomor
txtTmpNoUjian.Text = nomor
Else
.MoveLast
nomor = Int(!notmp) + 1
teksNomor = nomor
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
.MoveNext

txtNoUjian.Text = "BIU" & digit & nomor


txtTmpNoUjian.Text = nomor
End If
End With
ElseIf cboKategori.ListIndex = 4 Then
With rstSimB2
.Index = "idxsimb2"
If !notmp = 0 Then
nomor = 1
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
txtNoUjian.Text = "B2" & digit & nomor
txtTmpNoUjian.Text = nomor
Else
.MoveLast
nomor = Int(!notmp) + 1
teksNomor = nomor
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
.MoveNext

txtNoUjian.Text = "B2" & digit & nomor


txtTmpNoUjian.Text = nomor
End If
End With
ElseIf cboKategori.ListIndex = 5 Then
With rstSimB2U
.Index = "idxsimb2u"

If !notmp = 0 Then
nomor = 1
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
txtNoUjian.Text = "B2U" & digit & nomor
txtTmpNoUjian.Text = nomor
Else
.MoveLast
nomor = Int(!notmp) + 1
teksNomor = nomor
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
.MoveNext
txtNoUjian.Text = "B2U" & digit & nomor
txtTmpNoUjian.Text = nomor
End If
End With
ElseIf cboKategori.ListIndex = 6 Then
With rstSimC
.Index = "idxsimc"

If !notmp = 0 Then
nomor = 1
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
txtNoUjian.Text = "C" & digit & nomor
txtTmpNoUjian.Text = nomor
Else
.MoveLast
nomor = Int(!notmp) + 1
teksNomor = nomor
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
.MoveNext
txtNoUjian.Text = "C" & digit & nomor
txtTmpNoUjian.Text = nomor
End If
End With
ElseIf cboKategori.ListIndex = 7 Then
With rstSimD
.Index = "idxsimD"

If !notmp = 0 Then
nomor = 1
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
txtNoUjian.Text = "D" & digit & nomor
txtTmpNoUjian.Text = nomor
Else
.MoveLast
nomor = Int(!notmp) + 1
teksNomor = nomor
If Len(teksNomor) = 1 Then
digit = "0000"
ElseIf Len(teksNomor) = 2 Then
digit = "000"
ElseIf Len(teksNomor) = 3 Then
digit = "00"
ElseIf Len(teksNomor) = 4 Then
digit = "0"
Else
Exit Sub
End If
.MoveNext
txtNoUjian.Text = "D" & digit & nomor
txtTmpNoUjian.Text = nomor
End If
End With

Else
Exit Sub
End If
End Sub

Public Sub setEnabledForm(status As Boolean)


On Error Resume Next
If status = False Then
cboKategori.Enabled = False
frameUser.Enabled = True
txtNamaPendaftar.SetFocus

cmdBaru.Enabled = False
cmdSimpan.Enabled = True

Else
cboKategori.Enabled = False
frameUser.Enabled = True
txtNamaPendaftar.SetFocus
End If
End Sub

Private Sub cmdBaru_Click()


setEnabledForm False
End Sub

Private Sub cmdCari_Click()


On Error Resume Next
koneksiDB

If cboKategori.ListIndex = 0 Then
With rstSimA
.Index = "idxsima"
.Seek "=", txtNamaPendaftar.Text
If .NoMatch = False Then
MsgBox "Nama sudah terdaftar" & vbCrLf & _
"====================" & vbCrLf
& _
"No.Pendaftaran : " &
!nopendaftaran & vbCrLf & _
"Kategori : " &
!kategori & vbCrLf & _
"No. Ujian : " &
!noujian & vbCrLf & _
"Tanggal : " &
!tanggalsekarang & vbCrLf & _
"====================",
vbInformation, "Help Center"
Exit Sub
Else
MsgBox "Tidak Ada"
txtTempatLahir.SetFocus
End If
End With
End If

End Sub

Private Sub cmdSimpan_Click()


'On Error Resume Next
koneksiDB
If cboKategori.ListIndex = 0 Then
dbSIM 0
End If
End Sub

Private Sub Form_Load()


isiKategori
buatNomorPendaftaran
End Sub

Private Sub txtAlamat_KeyPress(KeyAscii As Integer)


KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtTinggi.SetFocus
KeyAscii = 0
Beep
End If
End Sub
Private Sub txtNamaPendaftar_KeyPress(KeyAscii As
Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
If txtNamaPendaftar.Text = "" Then
MsgBox "Nama Masih kosong"
txtNamaPendaftar.SetFocus
Else
Call cmdCari_Click
KeyAscii = 0
Beep
End If
End If

End Sub

Private Sub txtPekerjaan_KeyPress(KeyAscii As Integer)


KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtTanggalSekarang.Text = Format(Date$, "dd-MM-
yyyy")

KeyAscii = 0
Beep
End If
End Sub

Private Sub txtTanggalLahir_KeyPress(KeyAscii As


Integer)
Dim tanggal, bulan, tahun As String

If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii =


vbKeyBack Then
ElseIf KeyAscii = 13 Then
tanggal = Mid(txtTanggalLahir.Text, 1, 2)
bulan = Mid(txtTanggalLahir.Text, 3, 2)
tahun = Mid(txtTanggalLahir.Text, 5, 4)
If Mid(txtTanggalLahir.Text, 1, 3) <> "-" And
Mid(txtTanggalLahir.Text, 3, 2) <> "-" Then
txtTanggalLahir.Text = tanggal & "-" & bulan &
"-" & tahun
Else
Beep
End If
txtAlamat.SetFocus
Else
KeyAscii = 0
Beep
End If

End Sub

Private Sub txtTempatLahir_KeyPress(KeyAscii As


Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtTanggalLahir.SetFocus
KeyAscii = 0
Beep
End If
End Sub

Private Sub txtTinggi_KeyPress(KeyAscii As Integer)


If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii =
vbKeyBack Then
ElseIf KeyAscii = 13 Then
txtPekerjaan.SetFocus
Else
KeyAscii = 0
Beep
End If

End Sub

You might also like