Link to home
Start Free TrialLog in
Avatar of thomas-sherrouse
thomas-sherrouseFlag for United States of America

asked on

Run SQL Update Query From Excel with Parameter VBA

I've got a basic update query that I run from SQL Server Mgnt Studio and I'd like to incorporate it into Excel so that I can type in the parameter that is needed and run it out of Excel. I've been able to incorporate "Select" SQL queries and replace the parameters in the SQL Statement with "?" to create a parameter that is linked to a cell reference, but the same procedure isn't working for an "Update" Statement.

I researched all over and it says I'll probably need to incorporate a bit of VBA Code in order to do this, but I can't quite figure out how to do this.

Here's the Connection String I have that works when I run the "Select" Query:

---
DSN=AvXXnte;Description=Avionte;UID=thoXXX.shXXX;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=MAU832N;DATABASE=AvXXnte

Open in new window

---

The Command Text I would like to run is:

---
UPDATE       TimeEntryBatchError
SET                Approve = 1
FROM            TimeEntryBatch INNER JOIN
                         TimeEntryBatchError ON TimeEntryBatch.TimeEntryBatchGUID = TimeEntryBatchError.TimeEntryBatchGUID
WHERE        (TimeEntryBatch.BatchID = ?)

Open in new window

---

The "?" is normally the Parameter that is pulling the number from Cell "=Sheet1!$C$2"


Please let me know if anyone has any questions or if I can provide any other detail on how best to accomplish this task.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thomas-sherrouse

ASKER

I'm guessing I don't have the Driver needed installed to create a connection using ADODB.Connection.

I'm getting an error of:

Complie error:

User-define type not defined

"cn as New ADODB.Connection" is highlighted as the error.

I'll do some research on the solution but if there is an easy one please let me know .

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had a small error in the solution provided and I was able to troubleshoot and provide a solution to the issue.