You are on page 1of 3

Visual Basic 6 code for connecting to a MySQL database using the MySQL ODBC driver.

This code snippet demonstrates how to connect to a MySQL database from a Windows based application written in Visual Basic 6. By using the MySQL ODBC driver and the Microsoft Remote Data Object it is quite easy to connect and retrieve records from a MySQL database server.

Download and install the MySQL ODBC driver. Set-up a MySQL username and password combination that will allow connections from any host. See MySQLs grant command. Start a new Visual Basic project and add the Microsoft Remote Data Object - Using the menus select Project | References and then select the Microsoft Remote Data Object from the list.

Sample Code
Private Sub cmdConnectMySQL_Click() Dim cnMySql As New rdoConnection Dim rdoQry As New rdoQuery Dim rdoRS As rdoResultset ' ' ' ' ' set up a remote data connection using the MySQL ODBC driver. change the connect string with your username, password, server name and the database you wish to connect to.

cnMySql.CursorDriver = rdUseOdbc cnMySql.Connect = "uid=YourUserName;pwd=YourPassword; server=YourServerName;" & _ "driver={MySQL ODBC 3.51 Driver}; database=YourDataBase;dsn=;" cnMySql.EstablishConnection ' set up a remote data object query ' specifying the SQL statement to run. With rdoQry .Name = "selectUsers" .SQL = "select * from user" .RowsetSize = 1 Set .ActiveConnection = cnMySql Set rdoRS = .OpenResultset( rdOpenKeyset, rdConcurRowVer) End With

' loop through the record set ' processing the records and fields. Do Until rdoRS.EOF With rdoRS ' your code to process the fields ' to access a field called username you would ' reference it like !username rdoRS.MoveNext End With

Loop

' close record set ' close connection to the database rdoRS.Close cnMySql.Close End Sub

CODE 2:
Dim Dim Dim Dim Dim Dim strDataBaseName As String strDBCursorType As String strDBLockType As String strDBOptions As String rs As ADODB.Recordset cn As ADODB.Connection

Private Sub Command1_Click() On Error GoTo Command1_Click_Error Dim b as Long strDBCursorType = adOpenDynamic 'CursorType strDBLockType = adLockOptimistic 'LockType strDBOptions = adCmdText 'Options Set cn = New ADODB.Connection Me.MousePointer = 11 cn.Open ConnectString() With cn .CommandTimeout = 0 .CursorLocation = adUseClient End With Set rs = New ADODB.Recordset 'Creates record set

strSQL = "<Your SQL Here>" rs.Open strSQL, cn, strDBCursorType, strDBLockType, strDBOptions if rs.Eof then Goto ExitSub else For b = 1 To rs.RecordCount '<do whatever you need to do with the data here> Next b end if ExitSub: rs.Close Set rs = Nothing cn.Close Set cn = Nothing On Error GoTo 0 Exit Sub Command1_Click_Error: MsgBox "Error " & Err.Number & " (" & Err.Description & ") & _ in procedure Command1_Click of Form " & Me.Name End Sub Private Function ConnectString() As String Dim strServerName as String Dim strDatabaseName as string Dim strUserName as string Dim strPassword as string 'Change to IP Address if not on local machine 'Make sure that you give permission to log into the 'server from this address 'See Adding New User Accounts to MySQL 'Make sure that you d/l and install the MySQL Connector/ODBC 3.51 Driver strServerName = "localhost" strDatabaseName = "DatabaseName" strUserName = "UserName" strPassword ="Password" ConnectString = "DRIVER={MySQL ODBC 3.51 Driver};" & _ "SERVER=" & strServerName & _ ";DATABASE=" & strDatabaseName & ";" & _ "USER=" & strUserName & _ ";PASSWORD=" & strPassword & _ ";OPTION=3;" End Function

You might also like