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!
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