Putting Your MySQL Data into Datagrid with ADODC

I believe the following information I post here is not too easy to be found while you are searching in internet. I’ve been wasted for almost 2 days to making VB6’s Datagrid control get connected to some of MySQL database. Some of this information are also taken from some books I read. I’m sure some of the other beginner like me would face this problem also, so I feel this is important to share with all of you.

So, let’s begin. When you try to connect your Datagrid control (from Microsoft Datagrid 6.0 (OLEDB)), please make sure you have already put ADODC to your project reference. If don’t, you can add some ADODC control at your form, then run the project for the first time (later on you can delete this ADODC control; it’s only for initial running to make the ADODC’s reference be included with your program).

Please note, you need to install MySQL ODBC connector, but please make sure you don’t use the version 3.51, because ADODC 6.0 cannot run with this version. Please install the version 5.1 or newer. For you who dont’ have and install it (the MySQL ODBC Connector), you can get it from the following link : http://dev.mysql.com/downloads/connector/odbc/5.1.html

Now, let’s start our VB6. In coding windows, please type the following codes :

Option Explicit
Dim con As ADODB.Connection
Dim tbITSupport As ADODB.Recordset
Private Sub Form_Load()
    Set con = New ADODB.Connection
    con.CursorLocation = adUseClient
    con.ConnectionString = “Driver={MySQL ODBC 5.1 Driver};” & _
                        “SERVER=;PWD=;UID=;” & _
                        “PORT=3306;DATABASE=IT;”
    con.Open
    Set tbITSupport = New ADODB.Recordset
    tbITSupport.LockType = adLockOptimistic
    tbITSupport.CursorType = adOpenDynamic
    tbITSupport.Open “Select noform,tgl,jam,departemen,extension from itsupport”, _
                    con, , , adCmdText
    Set DataGrid1.DataSource = tbITSupport
   
End Sub

———————–

Note : I took some columns from my project (my table name in MySQL is ITSupport).

Any comment or want to add some information to help me sharing the topic more completely? So, please drop your comment. I would really appreciate it. Thank you all.

Advertisements