Disini kita akan
mencoba membuat kode otomatis, misal pada kasus penggajian. Terdapat
tiga jenis gaji, yaitu : Bulanan, Harian, Borongan. Sedangkan kode akan
menyesuaiakan jenis gaji yang di pilih.
Bingung ??? hmm, langsung saja berikut langkah-langkahnya :
- Buat database db_gaji, tabel t_gaji dengan field id_gaji.
- Buat Module untuk koneksi ke database dan ketikkan syntax berikut ini.
Public db As New ADODB.Connection
Public ra As New ADODB.Recordset
Public sql As String
Dim koneksi As String
Dim db_nama As String
Dim db_pass As String
Dim db_user As String
Dim db_server As String
Dim db_root As String
Sub buka_koneksi()
On Error GoTo buat_error
db_nama = "db_gaji"
db_pass = ""
db_user = "root"
db_server = "localhost"
db_root = "3306"
db.ConnectionString = "DRIVER={MYSQL ODBC 3.51 DRIVER};SERVER=" & db_server & ";DATABASE=" & db_nama & ";UID=" & db_user & ";PWD=" & db_pass & ";PORT=" & db_port & ";"
db.Open
On Error GoTo 0
Exit Sub
buat_error:
MsgBox "Ada kesalahan server !!", vbInformation, "Cek Server"
End Sub
- Buat Form dengan desain seperti ini.
Kontrol
|
Name
|
Caption / Text / List
|
Label1
|
Label1
|
Data Gaji
|
Label2
|
Label2
|
Jenis Gaji
|
Label3
|
Label3
|
Id Gaji
|
Combo1
|
cboJenisGaji
|
Dikosongkan
|
Text1
|
txtIdGaji
|
Dikosongkan
|
Command1
|
cmdClose
|
Close
|
- Ketikkan syntax berikut ini.
Private Sub cboJenisGaji_Click()
BuatKode
End Sub
Private Sub cmdClose_Click()
End
End Sub
Private Sub Form_Load()
buka_koneksi
End Sub
Sub BuatKode()
If cboJenisGaji.Text = "Bulanan" Then
kode = "BLN-"
ElseIf cboJenisGaji.Text = "Harian" Then
kode = "HRN-"
ElseIf cboJenisGaji.Text = "Borongan" Then
kode = "BRG-"
End If
sql = "SELECT * FROM t_gaji WHERE left(id_gaji, 4)='" & kode & "'"
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open sql, db, adOpenDynamic, adLockOptimistic
If rs.RecordCount > 0 Then
rs.MoveLast
No = Right(rs!id_gaji, 3) + 1
Else
No = 1
End If
rs.Close
txtIdGaji.Text = kode & Right(1000 + No, 3)
End Sub
- Tekan F5 untuk menjalankan.
No comments:
Write komentar