Please help.
How to connect vb6 to mysql.
What are the codes?
Thanks.!

Recommended Answers

All 6 Replies

using odbc..
download the odbc connector here

after installing odbc connector then u can access mysql.

open vb6 project
add a button,
add references (Project->References), Find and Mark Microsoft ActiveX Data Objects 2.5 Library

then add this codes below :

Public Conn As New ADODB.Connection

Private Sub Command1_Click()
Dim ConnString As String
Dim db_name As String
Dim db_server As String
Dim db_port As String
Dim db_user As String
Dim db_pass As String
' error traping
On Error GoTo buat_koneksi_Error
' fill the variable
db_name = "MySQL"
db_server = "localhost" '
db_port = "3306"    'default port is 3306.
db_user = "root"    'default user name or depend on your user name on mysql.
db_pass = "adinda"  'depend on your password on mysql.
'/Create connection string
ConnString = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=" & db_server & ";DATABASE=" & db_name & ";UID=" & db_user & ";PWD=" & db_pass & ";PORT=" & db_port & ";OPTION=3"
'/Open Connection
With Conn
    .ConnectionString = ConnString
    .Open
End With

MsgBox "Connected"
'___________________________________________________________
On Error GoTo 0
Exit Sub
 
buat_koneksi_Error:
    MsgBox "Error, Please check if server is running!", vbInformation, "Check Server"
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Conn.State = adStateOpen Or Conn.State = adStateConnecting Then
   Conn.Close
   Set Conn = Nothing
End If
End Sub
commented: Well executed. +6

can u sample me one simple program? i really just dont know how to deal with mysql using vb6.

What you think post #3 is ?

If you are familiar with vb6 coding for database, mysql is no different from other DBMS.

Ok..i'm modified codes a bit..just for load data..as simple as u want..
like debasisdas said..if u familiar with vb6 database coding then mysql is not different with another dbms..

1 listview named listaddress, 1 button named btnView

Public Conn As New ADODB.Connection
Public rs As New ADODB.Recordset

Public Sub AturListView(LSV As ListView, ParamArray lstview())
    Dim i, width
    LSV.View = lvwReport
    width = LSV.width - 80
    With LSV.ColumnHeaders
        .Clear
        For i = 0 To UBound(lstview) - 1 Step 2
        .Add , , lstview(i), (lstview(i + 1) * width) / 100
        Next i
    End With
End Sub

Public Sub ShowData(LSV As ListView)
    Call AturListView(LSV, "Address Id", 12, "First Name", 18, _
    "Last Name", 18, "Phone Number", 20, "Email", 32)
            
    'Set rs = New ADODB.Recordset
    rs.Open "SELECT * FROM AddressData", _
        Conn, adOpenDynamic, adLockBatchOptimistic
    
    LSV.ListItems.Clear
    
    While Not rs.EOF
        Set View = LSV.ListItems.Add
        View.Text = rs!IdAddress
        View.SubItems(1) = rs!First_Name
        View.SubItems(2) = rs!Last_Name
        View.SubItems(3) = rs!Phone_Num
        View.SubItems(4) = rs!Email
        rs.MoveNext
    Wend
    rs.Close
    
End Sub

Public Sub Connect()
Dim ConnString As String
Dim db_name As String
Dim db_server As String
Dim db_port As String
Dim db_user As String
Dim db_pass As String
' error traping
On Error GoTo buat_koneksi_Error
' fill the variable
db_name = "AddressBook"
db_server = "localhost" '
db_port = "3306"    'default port is 3306
db_user = "root"    'default user name.
db_pass = "adinda"  ' depend on your password on mysql
'/Create connection string
ConnString = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=" & db_server & ";DATABASE=" & db_name & ";UID=" & db_user & ";PWD=" & db_pass & ";PORT=" & db_port & ";OPTION=3"
'/Open Connection
With Conn
    .ConnectionString = ConnString
    .Open
End With

    '___________________________________________________________
On Error GoTo 0
Exit Sub
 
buat_koneksi_Error:
    MsgBox "Err Desc : " & Err.Description & Chr(13) & "Err Source : " & Err.Source, vbInformation, "Error"
End Sub
Private Sub btnView_Click()
Call Connect
Call ShowData(ListAddress)
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Conn.State = adStateOpen Or Conn.State = adStateConnecting Then
   Conn.Close
   Set Conn = Nothing
End If
End Sub
commented: Owesome +4

Hiii
I have tried all these steps but still i m getting error like this "Run-time error '3709' The connection cannot be....."

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.