Pada postingan kali ini, saya akan menuliskan salah satu contoh source code untuk membangun aplikasi berbasis client server menggunakan VB 6.0 dengan menggunakan koneksi MyODBC 3.51 driver. Source code yang akan kita bahas ini adalah source code yang berfungsi untuk menghubungkan program VB dengan
database MySQL, dapat digunakan pada program stand alone maupun yang berbasis client server. By the way, yang belum punya MyODBC 3.51 driver, silahkan download. . Letakkan source code ini di Module Class :
Public db As New ADODB.Connection
Public rs As New ADODB.Recordset
Public Function Koneksi()
Dim Respon As Integer
On Error GoTo pesan
Set db = New ADODB.Connection
db.CursorLocation = adUseClient
DoEvents
db.ConnectionTimeout = 10
nmdir = App.Path: nmf = nmdir & "\ip_adress.txt"
Open nmf For Input As #1
v_tmp = Input(LOF(1), 1)
vIp = Mid((v_tmp), 1, 15)
Close #1
db.ConnectionString = "driver={MySQL ODBC 3.51 Driver}; " & _
"SERVER=" & vIp & ";database=db_siswa; " & _
"UID=reg;PASSWORD=123;"
db.Open
Exit Function
pesan:
Respon = MsgBox("Gagal menghubungkan ke database", _
vbCritical + vbRetryCancel, "Connection Error")
If Respon = vbRetry Then
Resume
Else
End
End If
End Function
Keterangan
database MySQL, dapat digunakan pada program stand alone maupun yang berbasis client server. By the way, yang belum punya MyODBC 3.51 driver, silahkan download. . Letakkan source code ini di Module Class :
Public db As New ADODB.Connection
Public rs As New ADODB.Recordset
Public Function Koneksi()
Dim Respon As Integer
On Error GoTo pesan
Set db = New ADODB.Connection
db.CursorLocation = adUseClient
DoEvents
db.ConnectionTimeout = 10
nmdir = App.Path: nmf = nmdir & "\ip_adress.txt"
Open nmf For Input As #1
v_tmp = Input(LOF(1), 1)
vIp = Mid((v_tmp), 1, 15)
Close #1
db.ConnectionString = "driver={MySQL ODBC 3.51 Driver}; " & _
"SERVER=" & vIp & ";database=db_siswa; " & _
"UID=reg;PASSWORD=123;"
db.Open
Exit Function
pesan:
Respon = MsgBox("Gagal menghubungkan ke database", _
vbCritical + vbRetryCancel, "Connection Error")
If Respon = vbRetry Then
Resume
Else
End
End If
End Function
Keterangan
- ip_adress.txt adalah sebuah file notepad yang berfungsi sebagai parameter koneksi. ip_adress.txt berisi ip adress komputer server. Letakkan file ip_adress.txt ini dalam satu folder tempat module ini disimpan.
- db_siswa adalah nama databasenya
- reg dan 123 adalah username dan password yang digunakan untuk login ke Database di MySQL

