How to connect vb to mysql

This is one of the questions comes to in my mind when I first tried to work with visual basic 6. 0 (vb6) and backend as mysql database.

Actually its required to install the mysql odbc connector. Which you can find on the mysql website. Ok here is the link for the mysql odbc connector

http://dev.mysql.com/downloads/connector/odbc/5.1.html

Just select the appropriate driver for instance windows and I always select windows

- Windows MSI Installer (x86)

Download by clicking on the link/mirror available on the right site of the above text.

After downloading the odbc connector for mysql double click on your machine to install it by following the screen instructions(installation wizard).

Your mysql odbc connector is installed and ready to use.

How to  check that your driver is installed properly.

Go to Control Panel

Go to Administrative Tools

Double click on Data Source(ODBC) icon

Click on Drivers Tab and Check that “MySQL ODBC  5.1 Driver” is available in the list of all ODBC drivers.

Then you can use the following simple code in visual basic 6.0 (vb6) to connect to the database.

Beginning of  vb code


Dim strConnectionString As String

Dim tconnection As ADODB.Connection

Dim trecord As ADODB.Recordset


Dim slct As String


  strConnectionString = “DRIVER={MYSQL ODBC 5.1  DRIVER};SERVER=localhost;

DATABASE=yourdatabasename;

PORT=3306;

UID=username;

PWD=password;OPTION=3″
   Set tconnection = New ADODB.Connection
   tconnection.ConnectionString = strConnectionString
   tconnection.Open

   slct = “select * from tablename”
      
  Set trecord = New ADODB.Recordset
  trecord.Open slct, tconnection

 

End of vb code

 

It is done, but don’t forget to create database, user, tables in the mysql.

 

To do that following this link.

One thought on “How to connect vb to mysql

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>