it01y2, on 23 May 2007 - 07:19 AM, said:
I'm am trying to create a script so that visual basic 6 can interact with mysql
Any ideas?
Answer:
Create a new project. Add a button called Command1.
Paste the code below into the project.
Edit any text between < and > to the appropriate values.
Also, go to the dropdown menu Project/References - and select "Microsoft ActiveX Data Objects 2.0 Library" and click OK.
Download and install - "mysql-connector-odbc-3.51.27-win32.msi"
When you run the project, it will display a message box displaying the content of each record's selected field in turn.
Use the commented out loop bit to restrict the amount of results or to avoid unwanted loops.
It's pretty basic but it works.
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 9
Dim strSQL As String
Dim X As Long
strDBCursorType = adOpenDynamic 'CursorType
strDBLockType = adLockOptimistic 'LockType
strDBOptions = adCmdText 'Options
Set cn = New ADODB.Connection
Me.MousePointer = 11 'sets pointer to hourglass
cn.Open ConnectString()
With cn
.CommandTimeout = 0
.CursorLocation = adUseClient
End With
Set rs = New ADODB.Recordset 'Creates record set
strSQL = "select * from <your table>"
rs.Open strSQL, cn, strDBCursorType, strDBLockType, strDBOptions
If Not rs.BOF Then
rs.MoveFirst
End If
If rs.EOF Then
MsgBox "No data found"
Else
For X = 1 To rs.RecordCount
MsgBox rs.Fields("<your field>").Value
rs.MoveNext
'If X > 100 Then GoTo 10 'use this to avoid getting stuck in loops during development
Next X
rs.Close
Me.MousePointer = 0 'sets pointer to normal
End If
GoTo 10
9
MsgBox "Error - I'm out of here!"
10
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
strServerName = "localhost"
strDatabaseName = "<your database name>"
strUserName = "<your username>"
strPassword = "<Your password>"
ConnectString = "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=" & strServerName & _
";DATABASE=" & strDatabaseName & ";" & _
"USER=" & strUserName & _
";PASSWORD=" & strPassword & _
";OPTION=3;"
End Function