Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help!, Connection to MySQL database

Status
Not open for further replies.

KGB007

MIS
Jun 15, 2005
7
GB
Dear Experts,


Could you help me with VB?

I like to write a VB code to connect to MySQL database via myODBC in order to do some query on the database, but I don't know how.


Thank alot for future help.

KBG007


 
There are many ways but here is one example:

The first thing you need to do is set a reference to the microsoft ActiveX Data Object. You don't have to do this and you can create objects but you will not have the intellisense drop downs and the application itself is not as efficient.


Dim cnMyConn As ADODB.Connection
Dim rsMyRS As ADODB.Recordset
Dim strMySQL As String

'create the connection object
Set cnMyConn = CreateObject("adodb.connection")
'open the connection object using a DSN entry
'you could also just pass it your connection string

cnMyConn.Open "DSN=Test2000"

'set up your sql string
strsql = "Select * From tblTest"

'create you recordset object
Set rsMyRS = cnMyConn.Execute(strsql)

'you now have a recordset object and you can do what you want with it
'the following just loops through the recordset and prints out the two
'fields to the immediate window

With rsMyRS
Do While Not .EOF
Debug.Print .Fields("Test1")
Debug.Print .Fields("Test2")
.MoveNext
Loop
End With

'close the recordset object
rsMyRS.Close

'release the objects
Set rsMyRS = Nothing
Set cnMyConn = Nothing


This is just a basic example but should get you started with playing around with your data in SQL Server. Hope this helps you out and just holler if you have any other questions.....:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top