Connect to mySQL Database with Visual Basic (1 Viewer)

charlesandrews

Registered User.
Local time
Today, 00:09
Joined
Dec 28, 2003
Messages
27
Hi SJ,

Glasgow is very bright today eh?

SJ, I was wondering if you could point me in the right direction. I have been using MS Access and Visual Basic for access for the past two years and have managed via the help of this forum to create cool functional databases.
However, I have recently installed MySQL 4.0.25 and Visual Basic 6.0 Professional and I would like to be able to switch away from access.

I have installed MySQL in my C Drive and I access mysql by typing: mysql -u root mysql. I am then presented with the prompt for SQL and I have been able to create databases on my c drive. I would like to create an application in Visual Basic that uses a MYSQL database for data and I am totally stuck at that point.
I am having great difficulty understanding and configuring ODBC and I have also read that databases can connect via ADO in VB6. I am lost lol.

Do you know if there is any step-by-step instructions as I feel a total idiot.

Kind Regards
 

Mile-O

Back once again...
Local time
Today, 00:09
Joined
Dec 10, 2002
Messages
11,316
I've not done it myself but you'll find the connection information you need at ConnectionStrings.com.

Yes, databases can be connected to using ADO.

I found this code, by Mark Gambo, on vbForums:

Code:
Dim strDataBaseName As String
Dim strDBCursorType As String
Dim strDBLockType As String
Dim strDBOptions As String
Dim rs As ADODB.Recordset
Dim 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
 

charlesandrews

Registered User.
Local time
Today, 00:09
Joined
Dec 28, 2003
Messages
27
Thanx SJ,

I will try that when I go home. Do I just include that code in the VB form?
 

Mile-O

Back once again...
Local time
Today, 00:09
Joined
Dec 10, 2002
Messages
11,316
You could so.

Personally, I'd be inclined to make a Class to deal with connections.
 

Dreamweaver

Well-known member
Local time
Today, 00:09
Joined
Nov 28, 2005
Messages
2,466
Thanks I've been trying to workout a sulution for this for weeks.

mick
 

tomtan29

New member
Local time
Today, 00:09
Joined
May 16, 2007
Messages
2
Hi ppl

Using the code provided in an access 2003 form with a command button i get the following error.

error 458.(variable uses an Automation Type not supported in visual basic)

can someone help with this???

Thanks a lot
 

MikeRG

New member
Local time
Today, 00:09
Joined
Dec 7, 2009
Messages
2
Hi all, - My first post here,

Using VB6, ADO

I know this is an old thread but I need help with this same subject.. I have searched all over and don't seem to be able to find an answer.

I have all the code in place to access the On-Line MySQL database but am getting an error ;

Connection Failed;[HY000][MySQL[ODBC 5.1 Driver]Access denied for user:'myusername@server_ip_address'(Using Password: YES)

I am assuming that this is caused by not having set privileges/permissions on the server.
I have read that this is done using the MySQL Grant command.

Thats my problem, I don't know how or where to set the privileges using Grant. I notice from a demo that it is done from a MySQL> Promp. Where do I find that interface.
I have asked the Server Tech-Support but they are not being very helpfull.

Can someone talk me through this please..

Thanks for reading this
~Mike~
 

MikeRG

New member
Local time
Today, 00:09
Joined
Dec 7, 2009
Messages
2
I've not done it myself but you'll find the connection information you need at ConnectionStrings.com.

Yes, databases can be connected to using ADO.

I found this code, by Mark Gambo, on vbForums:

Code:
Dim strDataBaseName As String
Dim strDBCursorType As String
Dim strDBLockType As String
Dim strDBOptions As String
Dim rs As ADODB.Recordset
Dim 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

I Copied and pasted this straight into a new project, added my DB name, password etc. and it worked. (With one error);-
change
Code:
For b = 1 To rs.RecordCount
to
Code:
For b = 0 To rs.RecordCount -1
The ConnectFunction() is particularly usefull as a stand alone function. Just create a ConnectClose function or sub to close the connection after use.

After studying the code, it all becomes a lot clearer.
A good starting point for using VB6 to access remote mySQL databases.

Thanks Mile-O, solved my connection problem completely.
~Mike~
 

Users who are viewing this thread

Top Bottom