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
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
What's the error message?

PS Cells(1,1).Values should just be Cells(1,1).Value.
 
Upvote 0
It says "Operation is now allowed when the object is closed", referencing on pre-last row "rs.Close".

Thnx for Value correction
 
Upvote 0
Is the table being updated?

Oh, actually you're right, table is updated as I wanted. I thought because of the error that nothing happened. :) Ok, so now I should just find a way to close everything properly so I don't get that error. I'm guessing just deleting that part wouldn't be smartest idea :).
 
Upvote 0
Try closing the connection then setting it and the recordset to Nothing.
 
Upvote 0
Try closing the connection then setting it and the recordset to Nothing.

Hi,thnx for suggestion, I'm almost done but still I receive same error. In addition, I have code working (except for that error) but it works every second time. It suppose to pull the number in MySql table, adds 10 to it, returns value in excel and replaces old number in MySql with that new number, like a counter with addition of 10. But first time I run code it does that job, second time I run nothing happened, then third time I run and does it again as wanted. and so on. So every second time it works :) Can someone please check on my code and help me out why is that. Thank you so much.

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"
    updSql = "UPDATE ugovori_dt SET br_ugovora='" & ThisWorkbook.Sheets("Books").Range("F12") & "'"
    Set cn = CreateObject("ADODB.Connection")
    cn.Open "Driver={MySQL ODBC 5.1 Driver};SERVER=***;DATABASE=***;UID=***;PWD=***"

    rs.Open strSql, cn, adOpenStatic
    
    Sheets("Books").Range("F12") = rs.Fields("br_ugovora") + 10
    
    rs.Close

    rs.Open updSql, cn, adOpenStatic
    
    rs.Close 'where error shows
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
Application.ScreenUpdating = True
End Sub

 
Last edited:
Upvote 0
Why are you using a recordset to fire a SQL update command? Not able to confirm with testing but this

Code:
cn.execute updSql

replaces this

Code:
rs.Open updSql, cn, adOpenStatic
    
    rs.Close 'where error shows
    Set rs = Nothing
 
Upvote 0
What happens if you remove the 2nd rs.Close?

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

PS Is the SELECT query only ever returning one record?
 
Upvote 0
Why are you using a recordset to fire a SQL update command? Not able to confirm with testing but this

Code:
cn.execute updSql

replaces this

Code:
rs.Open updSql, cn, adOpenStatic
    
    rs.Close 'where error shows
    Set rs = Nothing

You're right, I'm new with MySql and VBA and I remember I read about this so no need for recordset to execute SQL update command, but I copied from code above and left it that way. Now it works just fine that part.
 
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