Update MySQL database using Excel VBA

zovche

Board Regular
Joined
Mar 21, 2013
Messages
125
Hi, I have easy one here, but can't get it done, I want to update MySQL DB via Visual basic based on cell value.
Table in MySQL has column ID and column Value, I would like to update that value based on A1 cell value in Sheet1. So far I have this code, but I receive an error message. Can you guide me where I did wrong or how to update it in other way.

Thank you.

Code:
Private Sub CommandButton1_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset




strCon = "Driver={MySQL ODBC 5.1 Driver};SERVER=doks;DATABASE=teke_brojeva;UID=***;PWD=***"


Set cn = CreateObject("ADODB.Connection")


cn.Open strCon
Set rs = CreateObject("adodb.recordset")


rs.Open "UPDATE ugovori_dt SET br_ugovora='" & (Cells(1, 1).Values) & "'", cn


rs.Close


End Sub
 
What happens if you remove the 2nd rs.Close?

Solution from ScottR works fine and resolved this issue. Thank you for help.


By the way, the code as posted is only going to put the value from F12 on 'Books' into the field br_ugovora ,it's not going to take the value from that cell, add 10 to it and then update with that.

If you wanted to do that then this,
Code:
 updSql = "UPDATE ugovori_dt SET br_ugovora='" & ThisWorkbook.Sheets("Books").Range("F12") & "'"[/code
would need to be move below this.
[code]
    Sheets("Books").Range("F12") = rs.Fields("br_ugovora") + 10

Oh, I guess I'm just too tired and missed this one. I am executing updSql below but I named that variable before that command so no wonder it worked messed up :D Have to admit it was funny to see how it worked every second time :) Now it works perfectly.

PS Is the SELECT query only ever returning one record?

Yes, there will be only one record, last saved/used number.
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Just overall biiig thank to everyone who participated and helped me out. Just learning new things and this should be great for my next needs of use of this. Hope it will help to others in my situation as well. Stay awesome everyone ;) Case Closed

Final code:

Code:
Sub connect()
    Dim strSql As String
    Dim updSql As String
    
Application.ScreenUpdating = False
    
    Set rs = CreateObject("ADODB.Recordset")
    
    strSql = "Select br_ugovora FROM ugovori_dt"
    
    Set cn = CreateObject("ADODB.Connection")
    cn.Open "Driver={MySQL ODBC 5.1 Driver};SERVER=***;DATABASE=****;UID=***;PWD=***"


    rs.Open strSql, cn, adOpenDynamic
    
    Sheets("Books").Range("F12") = rs.Fields("br_ugovora") + 10
    
    rs.Close
    Set rs = Nothing
    
    updSql = "UPDATE ugovori_dt SET br_ugovora='" & Sheets("Books").Range("F12") & "'"
    cn.Execute updSql
    
    cn.Close
    Set cn = Nothing


Application.ScreenUpdating = True


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,156
Members
452,385
Latest member
Dottj

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top