btnUpdate_Click() Question in VBA

M

marcmc

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
 
M

marcmc

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top