VBA: execute sql queries

Hello experts,

Please find bellow and attached a xlsm file that Use to update data in one of the database.

2015-08-25-16-22-21-Microsoft-Excel-.png
I was wondering if there is a way to create a Sub in VBA in which I define the user information (username, password) server and database and directly run the various queries defined in Column B and if possible display the sql logs in msgbox or in an external file.

Thank you again for your help.
LVL 1
LD16Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
HI,

pls try

Sub ConnectDB()

    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String

    sConnString = "Provider=SQLOLEDB;Data Source=localhost;User ID=abcd;Password=abcd;"

    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    conn.Open sConnString
    For Each c In Range(Range("B2"), Range("B" & Cells.Rows.Count).End(xlUp))
        Set rs = conn.Execute(c.Text)
    Next
End Sub

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Thank you for this code.

Could you please let me know where should I report the server name and the database name?

Thank you again for your help.
Rgonzo1971Commented:
What type of db?
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

LD16Author Commented:
Sql  server db
Rgonzo1971Commented:
then try and replace the names with the ones you need

strCon = "Provider=SQLOLEDB; " & _  
        "Data Source="your ServerName"; " & _  
        "Initial Catalog=database name;" & _  
        "User ID=uid; Password=pwd; Trusted_Connection=yes"
LD16Author Commented:
Perfect it works, I will retest for other queries tomorrow.
LD16Author Commented:
Sorry for the delay. Tested and it works!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.