mySQL connect function

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
I am looking for a way to connect to mySQL with a function so it can be reused passing different credentials each time it is called. Only thing is that I can't seem to use the connection after I run the SQLconnect function. I'm sure it's something easy but I don't know what to do. Can anyone help??


This is my attempt, but keep getting the error "object required"
Code:
Sub testUpdate()
Dim sqlstr As String

SQL = SQLconnect("127.0.0.1", "test", "root", "xxxxxxxx")

sqlstr = "UPDATE " & table1 & " SET " & field2 & " = 'thisGuy' WHERE " & field1 & " = '1'"

SQL.Execute sqlstr

End Sub

Public Function SQLconnect(server_name As String, database_name As String, user_id As String, password As String)

Dim conn As New ADODB.Connection

' Establish connection to the database
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=16427"

End Function
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You aren't returning anything from the function, you should return the connection.

Something like this perhaps, though I haven't had a chance to check it.
Code:
Sub testUpdate()
Dim sqlstr As String
    Set Sql = SQLconnect("127.0.0.1", "test", "root", "xxxxxxxx")
    sqlstr = "UPDATE " & table1 & " SET " & field2 & " = 'thisGuy' WHERE " & field1 & " = '1'"
    Sql.Execute sqlstr
End Sub
 
Public Function SQLconnect(server_name As String, database_name As String, user_id As String, password As String) As ADODB.Connection
Dim conn As New ADODB.Connection
' Establish connection to the database
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=16427"
Set SQLconnect = conn
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,922
Messages
6,175,406
Members
452,640
Latest member
steveridge

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