ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   btnUpdate_Click() Question in VBA (https://www.excelbanter.com/excel-programming/344442-btnupdate_click-question-vba.html)

marcmc

btnUpdate_Click() Question in VBA
 
I am a Csharp.NET developer with little or no exposure to VB.
I need to connect to a SQL database on a click event, read a number of cells
and insert/update/delete them from a SQL table.

Any guides where I can get help with the slightly different syntax.
Here is the code I want to transform:

Private Sub btnUpdate_Click()

Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim cmdSelect As New SqlClient.SqlCommand

Dim strSQL As String = "UPDATE mis_MISRE_employees SET Logged_In = 'Y'
WHERE uname = '" + UName + "' "
Dim cmdSelect As New SqlClient.SqlCommand
cmdSelect.CommandText = strSQL
cmdSelect.CommandType = CommandType.Text

Try
cn = New SqlClient.SqlConnection("user id=" & UNameLogXML &
";password=" & PasswordLogXML & ";database=" & DatabaseLogXML & ";server=" &
ServerLogXML)
cn.Open()
cmdSelect1.Connection = cn
cmdSelect1.ExecuteNonQuery()
Catch ex As Exception
sqlCnError = ("Error: Could not establish database connection")
End Try

cn.Close()
cn.Dispose()

End Sub


marcmc

btnUpdate_Click() Question in VBA
 
Got it as follows: don't forget to go Tools/References and add Microsoft
ActiveX 2.5 or .

Private Sub btnUpdate_Click()

Dim c As ADODB.Connection
Dim r As ADODB.Recordset
Set c = New ADODB.Connection
Dim rw As Integer

strCn = "Server=ServerName;Database=DbName;User Id=marc;password=mm"

c.Provider = "sqloledb"
c.Open strCn

rw = 2
Do While Len(Cells(rw, 8)) 0

'sq = "Insert into marc_temp_excel (comment) values ('Marc')"

Set r = c.Execute(sq)
rw = rw + 1
Loop

c.Close
MsgBox ("marc_temp_excel Table Successfully Updated.")
End Sub



All times are GMT +1. The time now is 06:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com