How to Update SQL Sever Recordset from VBA Excel - It does not work, but errors are Not given

actjfc

Active Member
Joined
Jun 28, 2003
Messages
416
Excel Friends,

I have been trying to develop a recordset update VBA code for a customer SQL server database. I could develop a "rs.addNew" record easily. However, I do not know SQL/VBA enough to make the update code below work for me. This Sub is called after another sub that actually makes it certain the Customer is in the database. It does not give me any errors, but it does not update the record with the data in Cust_Data.Cells(I + 1,2).Value. It has been a kind of trial and too many errors for me. So it might have some unnecessary code.

Any help is highly appreciated.

Thanks!

Code:
Sub SQL_Customer_Update()

Dim Unique_ID As String
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

Dim sqlcn As String
sqlcn = "Driver={XXX YYYYY};Server=abcdxyz.xyz.acme.com,5555; Database=dummy;"
cn.Open sqlcn, "EmpID", "PW"

Dim rs As New ADODB.Recordset
Set rs = New ADODB.Recordset

With rs

        .ActiveConnection = cn
        Unique_ID = Bill.Range("B5")
        .Open "SELECT * FROM CUSTOMER_DATABASE WHERE Cust_ID = '" & Unique_ID & "'"
                                  
           Dim I As Integer
           For I = 0 To 20
                rs.Fields(I).Value = Cust_Data.Cells(I + 1,2).Value
           Next I
        
        .Update
        .Close

End With

cn.Close ‘it is needed?

Set rs = Nothing
Set cn = Nothing

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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