From VBA, the simplest data access library is ADO. Add a link to the Microsoft ActiveX Data Objects Library so you can use ADODB objects. *.
To execute the stored procedure (which in your case will add an entry to the table), you can do this:
... lazy way (creating SQL statements directly, without using Parameter objects, this is subject to SQL injection hacks):
Public Sub AddFoo _ ( _ strServer As String, _ strDatabase As String, _ strUsername As String, _ strPassword As String, _ lFooValue As Long _ ) ' Build the connection string Dim strConnectionString As String strConnectionString = "Driver={SQL Server}" _ & ";Server=" & strServer _ & ";Database=" & strDatabase _ & ";UID=" & strUsername _ & ";PWD=" & strPassword ' Create & open the connection Dim oConnection As Connection Set oConnection = New Connection oConnection.ConnectionString = strConnectionString oConnection.Open ' Build the SQL to execute the stored procedure Dim strSQL As String strSQL = "EXEC AddFoo " & lFooValue ' Call the stored procedure Dim oCommand As Command Set oCommand = New Command oCommand.CommandType = adCmdText oCommand.CommandText = strSQL oCommand.ActiveConnection = oConnection oCommand.Execute oConnection.Close End Sub
... or the correct way (which concerns the encoding of all parameters and, therefore, is not subject to SQL injection hacks - intentional or random):
Public Sub AddFoo _ ( _ strServer As String, _ strDatabase As String, _ strUsername As String, _ strPassword As String, _ lFooValue As Long _ ) ' Build the connection string Dim strConnectionString As String strConnectionString = "Driver={SQL Server}" _ & ";Server=" & strServer _ & ";Database=" & strDatabase _ & ";UID=" & strUsername _ & ";PWD=" & strPassword ' Create & open the connection Dim oConnection As Connection Set oConnection = New Connection oConnection.ConnectionString = strConnectionString oConnection.Open ' Build the SQL to execute the stored procedure Dim strSQL As String strSQL = "EXEC AddFoo " & lFooValue ' Create the command object Dim oCommand As Command Set oCommand = New Command oCommand.CommandType = adCmdStoredProc oCommand.CommandText = "AddFoo" ' Create the parameter Dim oParameter As Parameter Set oParameter = oCommand.CreateParameter("foo", adParamInteger, adParamInput) oParameter.Value = lFooValue oCommand.Parameters.Add oParameter ' Execute the command oCommand.ActiveConnection = oConnection oCommand.Execute oConnection.Close End Sub
source share