Current location: Hot Scripts Forums » Programming Languages » Visual Basic » how to connect vb 6 with mysql


how to connect vb 6 with mysql

Reply
  #1 (permalink)  
Old 07-26-06, 05:01 AM
dzol dzol is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
how to connect vb 6 with mysql

i'm working on a vb project that need to has a database with mysql.
how do i want to connect it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-26-06, 01:00 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,167
Thanks: 3
Thanked 8 Times in 8 Posts
Download MySQL ODBC Driver:

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

Here is a sample code:

Code:
Private Sub cmdConnect_Click()
Dim cn As New rdoConnection
Dim qry As New rdoQuery
Dim rs As rdoResultset
 
cn.CursorDriver = rdUseOdbc
cn.Connect = "uid=UserName;pwd=Password;server=localhost;driver={MySQL ODBC 3.51 Driver};database=DatabaseName;dsn=;"

cn.EstablishConnection
 
With qry
	.Name = "queryName"
	.SQL = "select * from tblName"
	.RowsetSize = 1
	Set .ActiveConnection = cn
	Set rs = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
End With
	
 loop through the record set processing the
 records and fields.
Do Until rs.EOF
	With rs
	  ' your code using rs!fieldname
	  rs.MoveNext
	End With
Loop
rs.Close
cn.Close
End Sub
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-28-06, 12:14 AM
dzol dzol is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
another problem

thanks for ur help.
this is my interface:

this is the table using mysql:
mysql> select * from a;
+-------------+---------------+-------------------------------------+
| englishword | malayword | explanation |
+-------------+---------------+-------------------------------------+
| mouse | tetikus | benda yang digunakan untuk mengelik |
| keyboard | papan kekunci | benda yang digunakan untuk menaip |
+-------------+---------------+-------------------------------------+

and this far, this is the command:

Option Explicit

Private Sub Command1_Click()

Dim cnMySql As New rdoConnection
Dim rdoQry As New rdoQuery
Dim rdoRS As rdoResultset
' set up remote data connection using the
' MySQL ODBC driver

cnMySql.CursorDriver = rdUseOdbc
cnMySql.Connect = "uid=root;pwd=1234;server=localhost;" & _
"driver={MySQL ODBC 3.51 Driver};database=kamus;dsn='';"
cnMySql.EstablishConnection

With rdoQry
.Name = "malayword"
.SQL = "select * from a"
.RowsetSize = 1
Set .ActiveConnection = cnMySql
Set rdoRS = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
End With

Text2.Text = ""
Text3.Text = ""

With rdoRS
Text2.Text = Text2.Text & !malayword
Text3.Text = Text3.Text & !explanation
End With

rdoRS.Close
cnMySql.Close

End Sub

how do i want make if user type as example;
he type mouse in the first textbox, when he click the commandbutton then the output will be tetikus(2nd textbox) and explanation(3rd textbox) as in the table a.
Attached Images
File Type: gif 3.GIF (3.0 KB, 2155 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-28-06, 02:02 AM
duesi's Avatar
duesi duesi is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 225
Thanks: 0
Thanked 0 Times in 0 Posts
At the moment, your code selects all entries from the table, but thats not what you want.

The event handler of your button mus change the query like this:

Code:
sql = "SELECT malayword, explanation FROM a WHERE englishword = '" & Text1.Text & "'"
Maybe you rename your controls, to make it more obvious what tehy mean (Text1 could be txt_english...), also your table could use a different name "a" is not really self-descriptive

Happy Coding!
__________________
Duesi

"One of the great skills in using any language is knowing what not to use, what not to say" (Ron Jeffries)

http://www.swissbytes.de
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 07-28-06, 03:49 AM
dzol dzol is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for the code, i'll try it out.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 07-30-06, 10:37 PM
dzol dzol is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
error

i have two error in my coding, please someone tell me
what's wrong.

here the code:
Code:
Option Explicit
Dim TextMessage1 As String
Dim TextMessage2 As String
Dim cnMySql As New rdoConnection
Dim rdoQry As New rdoQuery
Dim rdoRS As rdoResultset

Private Sub ConvertMalay_Click()

'if user not type anything
If EnglishWordTxt.Text = "" Then
TextMessage1 = MsgBox("Please type the word first", vbOKOnly, "Caution")

'if user type a word that's in the database
Else
If EnglishWordTxt.Text = EnglishWordTxt.Text Then

'set up remote data connection using the MySQL ODBC driver
cnMySql.CursorDriver = rdUseOdbc
cnMySql.Connect = "uid=root;pwd=1234;server=localhost;" & _
    "driver={MySQL ODBC 3.51 Driver};database=kamus;dsn='';"
cnMySql.EstablishConnection

With rdoQry
    .Name = "malayword"
    .SQL = "SELECT malayword, explanation FROM a WHERE englishword = '" & EnglishWordTxt.Text & "'"
    .RowsetSize = 1
    Set .ActiveConnection = cnMySql
    Set rdoRS = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
End With
    
MalayWordTxt.Text = ""
ExplanationTxt.Text = ""

    With rdoRS
    MalayWordTxt.Text = MalayWordTxt.Text & !malayword
    ExplanationTxt.Text = ExplanationTxt.Text & !explanation
    End With
    
rdoRS.Close
cnMySql.Close

'if user type a word that's not in the database
Else
TextMessage2 = MsgBox("Sorry, your word is not in this dictionary", vbOKOnly, "Caution")
End If
End If
End Sub

Private Sub Quit_Click()
End
End Sub
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 07-30-06, 10:47 PM
dzol dzol is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
when i type another word that's in the mysql database
error at
Code:
.Name = "malayword"
when i type anyword that's not in the database
error at
Code:
MalayWordTxt.Text = MalayWordTxt.Text & !malayword
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 07-31-06, 02:46 AM
duesi's Avatar
duesi duesi is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 225
Thanks: 0
Thanked 0 Times in 0 Posts
I think you have 2 Problems.
First a Syntacical one:

Code:
Private Sub ConvertMalay_Click()

'if user not type anything
If EnglishWordTxt.Text = "" Then
	TextMessage1 = MsgBox("Please type the word first", vbOKOnly, "Caution")

	'if user type a word that's in the database
Else If EnglishWordTxt.Text = EnglishWordTxt.Text Then

	'set up remote data connection using the MySQL ODBC driver
	cnMySql.CursorDriver = rdUseOdbc
	cnMySql.Connect = "uid=root;pwd=1234;server=localhost;" & _
    	"driver={MySQL ODBC 3.51 Driver};database=kamus;dsn='';"
	cnMySql.EstablishConnection

	With rdoQry
	    .Name = "malayword"
	    .SQL = "SELECT malayword, explanation FROM a WHERE englishword = '" & EnglishWordTxt.Text & "'"
	    .RowsetSize = 1
	    Set .ActiveConnection = cnMySql
	    Set rdoRS = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
	End With
    
	MalayWordTxt.Text = ""
	ExplanationTxt.Text = ""
	
	    With rdoRS
	    MalayWordTxt.Text = MalayWordTxt.Text & !malayword
	    ExplanationTxt.Text = ExplanationTxt.Text & !explanation
	    End With
	    
	rdoRS.Close
	cnMySql.Close

'if user type a word that's not in the database
Else
	TextMessage2 = MsgBox("Sorry, your word is not in this dictionary", vbOKOnly, "Caution")
End If
End If '<<<< To much
End Sub
There is a superfluous "End IF", if I am not mistaken.
notive how I reformateed your code, I find indentation very important to see what goes together.

Your second problem is a logical one: Your If statement does not do what you want it to do.
At the time when the if statement gets evaluated, you do not yet know if the word is in your dictionary or not, because you have not yet looked (that happens INSIDE your if statement).

Therefore, this might come closer:

Code:
Private Sub ConvertMalay_Click()

'if user not type anything
If EnglishWordTxt.Text = "" Then
	TextMessage1 = MsgBox("Please type the word first", vbOKOnly, "Caution")

'Lets see if user type a word that's in the database
Else If EnglishWordTxt.Text = EnglishWordTxt.Text Then

	'set up remote data connection using the MySQL ODBC driver
	cnMySql.CursorDriver = rdUseOdbc
	cnMySql.Connect = "uid=root;pwd=1234;server=localhost;" & _
    	"driver={MySQL ODBC 3.51 Driver};database=kamus;dsn='';"
	cnMySql.EstablishConnection

	With rdoQry
	    .Name = "malayword"
	    .SQL = "SELECT malayword, explanation FROM a WHERE englishword = '" & EnglishWordTxt.Text & "'"
	    .RowsetSize = 1
	    Set .ActiveConnection = cnMySql
	    Set rdoRS = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
	End With
    
	MalayWordTxt.Text = ""
	ExplanationTxt.Text = ""
	
	    With rdoRS
	    MalayWordTxt.Text = MalayWordTxt.Text & !malayword
	    ExplanationTxt.Text = ExplanationTxt.Text & !explanation
	    End With
	    
	rdoRS.Close
	cnMySql.Close

	If ExplanationTxt.Text = "" Then
		TextMessage2 = MsgBox("Sorry, your word is not in this dictionary", vbOKOnly, "Caution")
	End If
	
End If
 
End Sub
__________________
Duesi

"One of the great skills in using any language is knowing what not to use, what not to say" (Ron Jeffries)

http://www.swissbytes.de
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 07-31-06, 10:17 PM
dzol dzol is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for your answer but...

when i use the code that u give, error at
Code:
Else If EnglishWordTxt.Text = EnglishWordTxt.Text Then
when change to this
Code:
Private Sub ConvertMalay_Click()

'if user not type anything
If EnglishWordTxt.Text = "" Then
    TextMessage1 = MsgBox("Please type the word first", vbOKOnly, "Caution")

'Lets see if user type a word that's in the database
Else
If EnglishWordTxt.Text = EnglishWordTxt.Text Then

    'set up remote data connection using the MySQL ODBC driver
    cnMySql.CursorDriver = rdUseOdbc
    cnMySql.Connect = "uid=root;pwd=1234;server=localhost;" & _
        "driver={MySQL ODBC 3.51 Driver};database=kamus;dsn='';"
    cnMySql.EstablishConnection

    With rdoQry
        .Name = "malayword"
        .SQL = "SELECT malayword, explanation FROM a WHERE englishword = '" & EnglishWordTxt.Text & "'"
        .RowsetSize = 1
        Set .ActiveConnection = cnMySql
        Set rdoRS = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
    End With
    
    MalayWordTxt.Text = ""
    ExplanationTxt.Text = ""
    
        With rdoRS
        MalayWordTxt.Text = MalayWordTxt.Text & !malayword
        ExplanationTxt.Text = ExplanationTxt.Text & !explanation
        End With
        
    rdoRS.Close
    cnMySql.Close

    If ExplanationTxt.Text = "" Then
        TextMessage2 = MsgBox("Sorry, your word is not in this dictionary", vbOKOnly, "Caution")
    End If
    
End If
End If
End Sub
error at,
Code:
cnMySql.EstablishConnection
can u tell me what's wrong
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 08-01-06, 02:25 AM
duesi's Avatar
duesi duesi is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 225
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, you can leave out the line

Code:
If EnglishWordTxt.Text = EnglishWordTxt.Text Then
Because it is no restriction (a variable is always the same as itself, insn't it?)
But what error did you get (I see no syntactical problem).

Happy Coding!
__________________
Duesi

"One of the great skills in using any language is knowing what not to use, what not to say" (Ron Jeffries)

http://www.swissbytes.de
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
can't connect to MySQL all of a sudden, PHP My Admin still works nassau PHP 6 03-10-06 02:24 PM
Connect to MySQL and create variables ronlar ASP.NET 2 08-19-05 12:34 PM
PHP scripts unable to connect to DB after MySQL 4.1.10 upgrade Jason L PHP 3 03-08-05 01:32 AM
Vb -MySQl Performance Evaluation Feed Back. please! Romeo PHP 0 11-06-04 03:38 PM
Can't connect to mysql in include file when included in forums Forse PHP 2 07-12-04 08:39 PM


All times are GMT -5. The time now is 07:17 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.