VB Menggunakan Database MySQL

Buat koneksi database menggunakan driver ODBC, Ketik source code berikut :

Public Adoconn As ADODB.Connection
Public RsLogin As ADODB.Recordset

Private Sub CmdAdd_Click()
TxtId_IT.Enabled = True
Call TombolTdkAktif
CmdAdd.Enabled = False
Call KosongForm
End Sub

Private Sub CmdClose_Click()
Unload Me
End Sub

Private Sub CmdDelete_Click()
Dim VSQL As String
Dim Konfirmasi As String
If TxtId_IT.Text <> “” Then
Konfirmasi = MsgBox(“Ingin Di Hapus?”, vbYesNo + vbCritical, “Konfirmasi”)
If Konfirmasi = vbYes Then
RsIT.Delete
RsIT.Update
RsIT.MoveFirst
Call TombolTdkAktif
TxtId_IT.Enabled = True
Call KosongForm
DataGrid1.Refresh
TxtId_IT.SetFocus
TxtId_IT.SelLength = 8
End If
End If
End Sub

Private Sub cmdEdit_Click()
If CmdEdit.Caption = “Edit” Then
CmdSave.Enabled = False
CmdAdd.Enabled = False
CmdDelete.Enabled = False
CmdEdit.Caption = “Save Edit”
ElseIf CmdEdit.Caption = “Save Edit” Then
On Error Resume Next
RsIT.MoveFirst
While Not RsIT.EOF
Call Simpan
RsIT.Update
RsIT.MoveNext
Wend
On Error GoTo 0
CmdEdit.Caption = “Edit”
DataGrid1.Refresh
End If
End Sub

Private Sub CmdReset_Click()
TxtId_IT.Enabled = True
Call KosongForm
Call TombolTdkAktif
TxtId_IT.SetFocus
TxtId_IT.SelLength = 8
End Sub

Private Sub CmdSave_Click()
Set RsIT = New ADODB.Recordset
Dim X As Integer
RsIT.CursorLocation = adUseClient
RsIT.Open “SELECT * FROM `it_internal` WHERE `id_it` = ‘” & TxtId_IT.Text & “‘ “, Adoconn, adOpenDynamic, adLockOptimistic
If Not RsIT.EOF Then
X = MsgBox(“ID IT Sudah Ada, Ingin Di Edit?”, vbYesNo)
If X = vbYes Then
Call Tampil
RsIT.Update
TxtId_IT.SelLength = 8
TxtId_IT.SetFocus
Else
Exit Sub
End If
Else
RsIT.AddNew
Call Simpan
RsIT.Update
KosongForm
CmdAdd.Enabled = True
CmdReset.Enabled = True
DataGrid1.Refresh
TxtId_IT.SetFocus
TxtId_IT.SelLength = 8
End If
End Sub

Private Sub Form_Load()

Set Adoconn = New ADODB.Connection
Adoconn.CursorLocation = adUseClient
Adoconn.Open “Sesuaikan dengan Driver ODBC”

Call TombolTdkAktif
TxtId_IT.SelLength = 8

Set RsIT = New ADODB.Recordset
RsIT.Open “SELECT * FROM `it_internal`”, Adoconn, adOpenDynamic, adLockPessimistic

End Sub

Private Sub KosongForm()
TxtId_IT.Text = “”
TxtNama.Text = “”
TxtJabatan.Text = “”
CboStatus.Text = “”
CboUserGrp.Text = “”
End Sub

Private Sub Tampil()
TxtId_IT.Text = RsIT!id_it
TxtNama.Text = RsIT!nama_it
TxtJabatan.Text = RsIT!jabatan
CboStatus.Text = RsIT!Status
CboUserGrp.Text = RsIT!user_groups
End Sub

Private Sub Simpan()
RsIT!id_it = TxtId_IT.Text
RsIT!nama_it = TxtNama.Text
RsIT!jabatan = TxtJabatan.Text
RsIT!Status = CboStatus.Text
RsIT!user_groups = CboUserGrp.Text
End Sub

Private Sub TombolTdkAktif()
CmdEdit.Enabled = False
CmdDelete.Enabled = False
CmdReset.Enabled = False
CmdSave.Enabled = False
End Sub

Private Sub TxtId_IT_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

Private Sub TxtId_IT_LostFocus()
Dim X As Integer
Dim Konfirmasi As String
Set RsIT = New ADODB.Recordset
RsIT.CursorLocation = adUseClient
If TxtId_IT.Text = “” Then
X = MsgBox(“ID IT Tidak Boleh Kosong”, vbOKOnly, “Konfirmasi”)
End If
RsIT.Open “SELECT * FROM `it_internal` WHERE `id_it` = ‘” & TxtId_IT.Text & “‘ “, Adoconn, adOpenDynamic, adLockOptimistic
If Trim(TxtId_IT.Text) = “” Then Exit Sub
If Not RsIT.EOF Then
MsgBox “ID IT Sudah Ada”, vbOKOnly
Tampil
Call TombolAktif
CmdSave.Enabled = False
CmdAdd.Enabled = True
Else
MsgBox “ID IT Belum Terdaftar”, vbOKOnly
TxtNama.SetFocus
CmdAdd.Enabled = False
CmdSave.Enabled = True
CmdDelete.Enabled = False
CmdEdit.Enabled = False
CmdReset.Enabled = True
End If
End Sub

Private Sub TxtNama_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

Private Sub TombolAktif()
CmdEdit.Enabled = True
CmdDelete.Enabled = True
CmdReset.Enabled = True
CmdSave.Enabled = True
End Sub

Satu Balasan ke VB Menggunakan Database MySQL

  1. hery mengatakan:

    trims astry, numpang copy paste lagi mo nyusun ta,sklian salam kenal

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: