We have a SQL Server 2008 database that has stored procedures to handle reads/writes/etc. These procedures are used by a variety of applications internally.
The need ha
I ended up going with this approach using AD authentication. I used the example in this post for inspiration: http://www.eggheadcafe.com/community/sql-server/13/10141669/using-excel-to-update-data-on-ms-sql-tables.aspx
Note that these functions live in different areas of the Excel Workbook (Objects, Modules, This Workbook), but here's a quick reference.
I also have each of the columns that are FK validating against the tables they reference.
Here are some code samples:
Public aCon As New ADODB.Connection
Public aCmd As New ADODB.Command
Private Sub Workbook_Open()
Application.EnableEvents = False
PopulateSheet
Application.EnableEvents = True
End Sub
Sub Connect()
Dim sConn As String
sConn = "Provider=SQLOLEDB;Trusted_Connection=Yes;Server=[SVR];Database=[DB]"
With aCon
.ConnectionString = sConn
.CursorLocation = adUseClient
.Open
End With
BuildProcs
End Sub
Sub BuildProcs()
With aCmd
.ActiveConnection = aCon
.CommandType = adCmdStoredProc
.CommandText = "[SPROC]"
.Parameters.Append .CreateParameter("@in_EmployeeID", adInteger, adParamInput)
End With
End Sub
Sub PopulateSheet()
Dim n As Integer, r As Long
Dim aCmdFetchEmployees As New ADODB.Command
Dim aRstEmployees As New ADODB.Recordset
If aCon.State = adStateClosed Then Connect
With aCmdFetchEmployees
.ActiveConnection = aCon
.CommandType = adCmdStoredProc
.CommandText = "[SPROC]"
Set aRstEmployees = .Execute
End With
r = aRstEmployees.RecordCount
Worksheets(1).Activate
Application.ScreenUpdating = False
Cells(2, 1).CopyFromRecordset aRstEmployees
For n = 1 To aRstEmployees.Fields.Count
Cells(1, n) = aRstEmployees(n - 1).Name
Cells(1, n).EntireColumn.AutoFit
Next
Cells(1).EntireColumn.Hidden = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If aCon.State = adStateClosed Then Connect
With aCmd
.Parameters(0) = Cells(Target.Row, 1)
.Parameters(1) = Cells(Target.Row, 4)
.Parameters(2) = Cells(Target.Row, 5)
.Parameters(3) = Cells(Target.Row, 6)
.Parameters(4) = Cells(Target.Row, 7)
.Parameters(5) = Cells(Target.Row, 8)
.Parameters(6) = Cells(Target.Row, 10)
.Parameters(7) = Cells(Target.Row, 11)
.Parameters(8) = Cells(Target.Row, 12)
.Parameters(9) = Cells(Target.Row, 13)
.Parameters(10) = Cells(Target.Row, 14)
.Parameters(11) = Cells(Target.Row, 15)
.Parameters(12) = Cells(Target.Row, 16)
.Execute , , adExecuteNoRecords
End With
End Sub
I would recommend that you create a simple Webpart that does the edit. You will have an easier time finding someone who can do C# to a database than vba. Coding a very rough webpart to any coding in this manner is pretty easy.
There are also plenty of coding samples for C# and webparts available.
You also have the benefit of keeping the code on the server without potentially exposing username/password combinations in clear text within an office document.
This approach also enables you to smoothly improve the "quality" of the solution if it becomes more mission critical over time.
It does sound that you are following a scenario covered nicely by this example
Hi you can start with this.
Create Macro Button on your excel file. Click New and then add this code.
Sub Button1_Click()
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stSQL As String
Dim SNfound As String
'Your sqlserver 2008 connection string
Const stADO As String = "Provider=SQLOLEDB.1;" & _
"" & _
"Initial Catalog=YOurDB;" & _
"Data Source=YourServer;UID=yourusername;PWD=yourpassword;"
'SELECT FROM STORED PROCEDURE
' eg: select SN from SNTable where SN=@SN
stSQL = "exec usp_SelectSN '" & "TESTSN" & "'"
Set cnt = New ADODB.Connection
With cnt
.CursorLocation = adUseClient
.Open stADO
.CommandTimeout = 0
Set rst = .Execute(stSQL)
End With
If rst.RecordCount = 0 Then
'NO SN FOUND ON YOUR DB
Else
'RECORDS FOUND SHOW Retrieve SN from DB to message box
SNfound = rst.Fields("SN")
MsgBox ("Found:" & SNfound)
End If
Set rst = Nothing
Set cnt = Nothing
End Sub
Regards