I want to write a sql query which take an input from cell of an excel sheet.
For example if there are 3 columns like EmpID,EmpName,Salary,Address in the database and I would like to connect with the sql server database and update the database using a button in the excel sheet.
I wrote a code like this
Dim rst As ADODB.Recordset
Dim Cnxn As ADODB.Connection
Dim strCnxn As String
Dim strSQL As String
' Open connection
Set Cnxn = New ADODB.Connection
strCnxn = "Provider=SQLOLEDB;" & _
"Data Source=ABC\SQLEXPRESS;" & _
"Initial Catalog=Trail;" & _
"Integrated Security=SSPI;" & _
"Application Name=MyExcelFile"
Cnxn.Open strCnxn
Set rst = New ADODB.Recordset
Dim Ename As Variant
Ename = Worksheets("DBSheet").Cells(2, 2).Value
Dim Eno As Variant
Eno = Int(Worksheets("DBSheet").Cells(2, 1).Value)
strSQL = "UPDATE Employee SET EmpName='Micro' WHERE EmpID=1"
rst.Open strSQL, Cnxn, adOpenDynamic, adLockOptimistic, adCmdText
Cnxn.Close
Set rst = Nothing
Set Cnxn = Nothing
Here in the above code I Wrote a Update query like SET EmpName='Micro' WHERE EmpID=1, here i mentioned the EmpName and EmpID manually rather than that I would like to populate the update query with the cell values of an excel sheet like
strSQL = "UPDATE Employee SET EmpName=Worksheets("AB').Cells(2,2).Value WHERE EmpID=Worksheets("AB').Cells(2,1).Value"
But this is not working because , the EmpName and EmpID values are coming in the format of ""(double quotes)to the query but sqlquery would not accept any thing in that format in order to execute it should in the format of ''(single quotes).
So If any one know how to populate cell values of an excel sheet to the sql query please suggest me how to do that.