[VB 6] Cara Membuat MySQLConnectionKami.dll yang sederhana

Bagi banyak programmer VB 6 yang sudah mahir, mungkin membuat DLL koneksi database sudah hal yang lumrah dan biasa saja. Namun apakah hal itu juga berlaku pada pemula VB 6 ? hemmm tentu jawabannya relatif juga, ada yang bisa dan ada yang tidak, nah… bagi yang sudah bisa jangan menghina kesederhanaan coding DLL ini oke hehehehe, karena tujuan penulis membuat tulisan ini untuk membantu para pemula yang kesulitan dalam membuat koneksi database menggunakan DLL.

Oke, dari pada penulis berkata panjang lebar, lebih baik penulis langsung saja membahas bagaimana membuat MySQLConnectionKami.dll dengan VB 6

Langkah pertama kita buat dll-nya terlebih dahulu

1. Buka VB6

2. Buat Project DLL baru (File – ActiveX DLL – OK)

3. Beri nama Class1 dengan nama clsMySQLConnection

4. Ketik listing program berikut pada clsMySQLConnection

Option Explicit

Private strServer As String
Private strDatabase As String
Private strUID As String
Private strPWD As String
Private strPort As String
Private cnOpen As Boolean
Private cn As ADODB.Connection
Private DGError
Private cnString As String

‘open connection
Public Sub Open_Cn(ByVal nstrServer As String, ByVal nstrDatabase As String, ByVal nstrUID As String, ByVal nstrPWD As String, ByVal nstrPort As String)

On Error GoTo errhandler

strServer = nstrServer
strDatabase = nstrDatabase
strUID = nstrUID
strPWD = nstrPWD
strPort = nstrPort

Set cn = New ADODB.Connection

cn.CursorLocation = adUseClient
cnString = “Driver=MySQL ODBC 3.51 Driver;Server=” & strServer & “;UID=” & strUID & “;PWD=” & strPWD & “;Database=” & strDatabase & “;port=” & strPort

cn.Open cnString
cnOpen = True

Exit Sub

errhandler:

Call MsgBox(“Connection Error.”, vbOKOnly, “Error”)
strServer = “”

cnOpen = False

End Sub

‘re open connection
Public Sub Re_Open_Cn()

Call Close_Cn
Call Open_Cn(strServer, strDatabase, strUID, strPWD, strPort)

End Sub

‘close connection
Public Sub Close_Cn()

If cnOpen = True Then

cn.Close
Set cn = Nothing

cnOpen = False

End If

End Sub

‘ambil nama server
Public Property Get myStrServer() As String

myStrServer = strServer

End Property

‘memberi nama server
Public Property Let myStrServer(ByVal nstrServer As String)

strServer = nstrServer

End Property

‘ambil nama database
Public Property Get myStrDatabase() As String

myStrDatabase = strDatabase

End Property

‘memberi nama database
Public Property Let myStrDatabase(ByVal nstrDatabase As String)

strDatabase = nstrDatabase

End Property

‘ambil nama user
Public Property Get myStrUID() As String

myStrUID = strUID

End Property

‘memberi nama user
Public Property Let myStrUID(ByVal nstrUID As String)

strUID = nstrUID

End Property

‘memberi password
Public Property Let myStrWD(ByVal nstrPWD As String)

strPWD = nstrPWD

End Property

‘ambil port
Public Property Get myStrPort() As String

myStrPort = strPort

End Property

‘memberi port
Public Property Let myStrPort(ByVal nstrPort As String)

strPort = nstrPort

End Property

‘ambil connection
Public Property Get myStrConnection() As ADODB.Connection

Set myStrConnection = cn

End Property

5. Save Project dengan nama MySQLConnectionKami di foleder (Project MySQLConnection)

6. Make MySQLConnectionKami.dll dan simpan di folder (Project MySQLConnection\DLL)

7. Buat Form pemanggil, dengan mebuka VB 6 (File – New Project – VB Enterprise Edition Controls – OK)

8. Beri nama Form dengan frm_CobaMySQLConnection

9. Tambahkan 1 buah Listview dan Button

10. Beri nama Listview1 dengan lvAmbilData

11. Beri nama Button1 dengan nama cmd_AmbilData

12. Rekomendasikan MySQLConnectionKami.dll (Project – Reference – Browse File MySQLConnection.dll – OK)

13. Pada lvAmbilData rubah view-nya pada Properties dengan 3-lvwReport

14. Ketik listing program berikut ini pada Form frm_CobaMySQLConnection

Private ambilConnection As MySQLConnectionKami.clsMySQLConnection
Private rs As ADODB.Recordset

Private Sub cmd_AmbilData_Click()

On Error Resume Next

Dim i As Double
Dim j As Double
Dim k As Double

Set rs = New ADODB.Recordset
Set ambilConnection = New MySQLConnectionKami.clsMySQLConnection

ambilConnection.Open_cn “localhost”, “MySQL”, “root”, “badboyfuck”, “8080”
rs.Open “SELECT * FROM help_category h;”, ambilConnection.myStrConnection, adOpenDynamic

jmlhRecord = rs.RecordCount

If jmlhRecord > 0 Then

For i = 0 To rs.Fields.Count – 1

lvAmbilData.ColumnHeaders.Add , , rs.Fields(i).Name, 2000

Next i

For k = 0 To rs.RecordCount – 1

If IsNull(rs.Fields(0).Value) Then _
lvAmbilData.ListItems.Add , , “” Else lvAmbilData.ListItems.Add , , rs.Fields(0).Value

For j = 1 To rs.Fields.Count – 1

If IsNull(rs.Fields(1).Value) Then _
lvAmbilData.ListItems.Item(k + 1).ListSubItems.Add , , “” Else lvAmbilData.ListItems.Item(k + 1).ListSubItems.Add , , rs.Fields(j).Value

Next j

rs.MoveNext

Next k

End If

rs.Close
ambilConnection.Close_cn
Set rs = Nothing

End Sub

15. Jalankan program, semoga berhasil dan bermanfaat.