0

I need some assistance in writing the following code. I wrote my comments where I am stuck:

Sub UploadDataUsingProc()

    Call CheckFields
    Call Variables.SetVariables
    Call Variables.VariableValues
    Dim sqlCommand As ADODB.Command
    Dim TargetServer As ADODB.Connection
    Set TargetServer = New ADODB.Connection
    TargetServer.CommandTimeout = 99999

    TargetServer.Open ( _
    "driver=" & driver & ";" & _
    "server=" & server & _
    ";database=" & databasename & ";" & _
    ";UID=" & username & _
    ";PWD=" & password & ";" & _
    "Encrypt=YES;")

    ' Set the stored procedure
    Set sqlCommand = New ADODB.Command
    sqlCommand.ActiveConnection = TargetServer
    sqlCommand.CommandType = adCmdStoredProc 'Set sqlCommand to be a stored procedure
    sqlCommand.CommandText = "Proc_ReportMonthlySubmission"

    ' This is where I am not sure how do code further... Following Excel fields needs to be
    ' inserted into a SQL Server table using a stored procedure called "Proc_ReportMonthlySubmission"
    '
    ' Worksheets("Keys").Range("D8").Value
    ' Worksheets("Keys").Range("D7").Value
    ' Worksheets("Keys").Range("D3").Value
    ' Worksheets("Keys").Range("D5").Value

    'Set Procedure = "Exec " & Procedure
    TargetServer.Execute (Procedure)
    MsgBox "Procedure has run"
4
  • stackoverflow.com/questions/24267080/… Commented Sep 25, 2019 at 23:03
  • @TimWilliams - I appreciate your example and I reviewed it but where do I enter the spreadsheet cell numbers within the code? So it can pass those values through the store procedure... Following are the cell numbers where I need to get the values and pass them to the Procedure: Commented Sep 26, 2019 at 0:35
  • @TimWilliams ' Worksheets("Keys").Range("D8").Value ' Worksheets("Keys").Range("D7").Value ' Worksheets("Keys").Range("D3").Value ' Worksheets("Keys").Range("D5").Value Commented Sep 26, 2019 at 0:36
  • See also stackoverflow.com/questions/26230428/… - the answers there show how the values are assigned. Commented Sep 26, 2019 at 4:36

1 Answer 1

0

Try this one... (just follow there, as exposed by Tim Williams)

With sqlCommand
    .Parameters.Item("Whatever_field_you_have").Value = _
                              Worksheets("Keys").Range("D8").Value
    ...

End With

Hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.