ADO - Get Parameter Value from SQL within VBA

I need help with modifying this VBA code to allow for retrieving from SQL Server a parameter value (EventID) to be used to update the SQL Server table(exisiting record) with the actual Error Message.

Trying to accomplish the following:

1. create intial record in SQL Server table via an existing stored procedure.
2. Validate record (eventid) does not already exists.
3. Retrive the newly created records  RecordID(EventID)
4.  Update Sql Table via ADO code for the newly create Eventid - Possibly create an UPdate query to update the Actual Error Message and other information....

see previous post for sample of Stored Procedure:

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_28216982.html
'---------------------------------------------------------------------------------------
' Procedure : ParamSPT
' Author    : 49kxs
' Date      : 8/22/2013
' Purpose   : Updates the Error Log for ISCenter - (SQL generated)
'---------------------------------------------------------------------------------------
'
Function ParamSPT(ReportName As String, MODNAME As String, ProcName As String, _
                    sErr As String, nResults As Boolean)
   
Dim sConn As ADODB.Connection
Dim sCmd As ADODB.Command
Dim sRC As ADODB.Parameter
Dim lRetVal As Long
Dim strConn As String
   
   On Error GoTo ParamSPT_Error
  
    Set sConn = New ADODB.Connection
    strConn = _
        "Driver=SQL Server;Server=AQL02; Database=TRACI_ANALYTICS;Trusted_Connection=True;"
    sConn.ConnectionString = strConn
    
    sConn.Open strConn

    'SQL variables with database's counterparts
    
    '@EventName = ReportName
    '@ModuleName = ModName
    '@ProcedureName = ProcName
    '@ErrorMessage =Err.Number & ". " & Err.Description
    '@StepSucceeded = 0 or 1 (True/False)

    sConn.Execute "EXEC [ISCenter_Monitor].[usp_log_ISCenter_Event]" & _
                    " @EventName='" & ReportName & "'," & _
                    " @ModuleName='" & MODNAME & "'," & _
                    " @ProcedureName='" & ProcName & "'"

    Debug.Print "EXEC [ISCenter_Monitor].[usp_log_ISCenter_Event]" & _
                    " @EventName='" & ReportName & "'," & _
                    " @ModuleName='" & MODNAME & "'," & _
                    " @ProcedureName='" & ProcName & "'"
    
    Set sCmd = New ADODB.Command
    sCmd.Parameters.Append .CreateParameter("returnvalue", adInteger, adParamReturnValue)
    sCmd.Execute
    'Now check the return value of the procedure
    lRetVal = cmd.Parameters("returnvalue")
    sConn.Execute "EXEC [ISCenter_Monitor].[usp_log_ISCenter_Event]" & _
                    " @ErrorMessage='" & sErr & "'," & _
                    " @StepSucceeded='" & nResults & "'"


Where returnvalue = EVENTID from SQL Server Stored procedure.

   On Error GoTo 0
   Exit Function

ParamSPT_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ParamSPT of Module basErrorEventLog"
End Function

Open in new window

Karen SchaeferBI ANALYSTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

datAdrenalineCommented:
I am not really sure what you are wanting, and what works in your code.  It would be best in your question if you identify the real object names.  In addition is seems that your VBA is accomplishing some the goals you are trying to accomplish.

Can you clarify your question a bit and indicate at what point you are having difficulty?
0
Karen SchaeferBI ANALYSTAuthor Commented:
I want to retrieve the Newly created EventID from SQL Query - so that I may update the remainig data within the SQL query.

Step 1:   I need to first create a record in SQL,
Step 2:   Capture the newly created EventID ((Record ID)
Step3:    Then run Update Query to update the ErrorMessage from Access to post to the Sql
              Query for the newly created EventID.

I need to create a new record in the [ISCenter_Monitor].[ISCenter_EventLog] with the Event Following data see below - Sql will create a new record - it is that EvnetID I need to capture inorder to update the remaining data to [ISCenter_Monitor].[ISCenter_EventLog] (where the data is null.  see  '+++++++ portion of code.  
See lines 51-53 -

the sRC value does not return an actual value instead it returns 0

data
Here is my latest attempt at the VBA Code:  
'---------------------------------------------------------------------------------------
' Procedure : ParamSPT
' Author    : 49kxs
' Date      : 8/22/2013
' Purpose   : Updates the Error Log for ISCenter - (SQL generated)
'---------------------------------------------------------------------------------------
'
Function ParamSPT(ReportName As String, MODNAME As String, RecCt As Long, ProcName As String, _
                    sErr As String, nResults As Boolean)
   
Dim sConn As ADODB.Connection
Dim sCmd As ADODB.Command
Dim sRC As ADODB.Parameter
Dim lRetVal As Long
Dim strConn As String
   
   On Error GoTo ParamSPT_Error
  
    Set sConn = New ADODB.Connection
    strConn = _
        "Driver=SQL Server;Server=AQL02; Database=TRACI_ANALYTICS;TrustedConnection = true;"

    sConn.ConnectionString = strConn
    
    sConn.Open strConn

    'SQL variables with database's counterparts
    
    '@EventName = ReportName
    '@ModuleName = ModName
    '@ProcedureName = ProcName
    '@ErrorMessage =Err.Number & ". " & Err.Description
    '@AffectedRows = RecCt
    '@StepSucceeded = 0 or 1 (True/False)

    sConn.Execute "EXEC [ISCenter_Monitor].[usp_log_ISCenter_Event]" & _
                    " @EventName='" & ReportName & "'," & _
                    " @ModuleName='" & MODNAME & "'," & _
                    " @ProcedureName='" & ProcName & "'"

 
 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Set sCmd = New ADODB.Command
    sCmd.ActiveConnection = sConn
    sCmd.CommandType = adCmdStoredProc
    sCmd.CommandText = "[ISCenter_Monitor].[usp_log_ISCenter_Event]" & _
                    " @EventName='" & ReportName & "'," & _
                    " @ModuleName='" & MODNAME & "'," & _
                    " @ProcedureName='" & ProcName & "'"
    Debug.Print sCmd.CommandText
    
    Set sRC = sCmd.CreateParameter("EventID", adInteger, adParamReturnValue)
    sCmd.Parameters.Append sRC
    sCmd.Execute
    
    'Now check the return value of the procedure
    lRetVal = sCmd.Parameters("EventID")
    Debug.Print lRetVal
  
    sConn.Execute "UPDATE  [ISCenter_Monitor].[ISCenter_EventLog]" & _
                    " Set" & _
                        " @ErrorMessage='" & sErr & "'," & _
                        " @AffectedRows ='" & RecCt & "'," & _
                        " @StepSucceeded='" & nResults & "'" & _
                    " WHERE 'EventID ='" & lRetVal & "'"
    
    'sConn.Execute "EXEC [ISCenter_Monitor].[usp_log_ISCenter_Event]" & _
                    " @ErrorMessage='" & sErr & "'," & _
                    " @StepSucceeded='" & nResults & "'" & _
                    " WHERE 'EventID ='" & lRetVal & "'"


   On Error GoTo 0
   Exit Function

ParamSPT_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ParamSPT of Module basErrorEventLog"
End Function

Open in new window

I am still getting the following error message:

msg
Debugged Values:
{ call [ISCenter_Monitor].[usp_log_ISCenter_Event] @EventName='Invoices Data Load', @ModuleName='Report Functions', @ProcedureName='InvoicesLoad' }
0
datAdrenalineCommented:
I read quickly ... and I am short on time ... but ... I wanted to suggest that you change your stored procedure ...

from ending like ....

RETURN @RETVAL  


To ending like ...

SELECT @RETVAL

---

You see the .Execute method of an ADODB connection object or an ADODB command object returns an ADODB recordset object.  You can use that to your advantage.  Take a look at the mods I made to your code...
Function ParamSPT(ReportName As String, MODNAME As String, RecCt As Long, ProcName As String, _
                    sErr As String, nResults As Boolean)
   
Dim sConn As ADODB.Connection
Dim lRetVal As Long
Dim strConn As String
   
   On Error GoTo ParamSPT_Error
  
    Set sConn = New ADODB.Connection
    strConn = _
        "Driver=SQL Server;Server=AQL02; Database=TRACI_ANALYTICS;TrustedConnection = true;"

    sConn.ConnectionString = strConn
    
    sConn.Open strConn

    'SQL variables with database's counterparts
    
    '@EventName = ReportName
    '@ModuleName = ModName
    '@ProcedureName = ProcName
    '@ErrorMessage =Err.Number & ". " & Err.Description
    '@AffectedRows = RecCt
    '@StepSucceeded = 0 or 1 (True/False)

    With sConn.Execute("EXEC [ISCenter_Monitor].[usp_log_ISCenter_Event]" & _
                    " @EventName='" & ReportName & "'," & _
                    " @ModuleName='" & MODNAME & "'," & _
                    " @ProcedureName='" & ProcName & "'", , adCmdText)
        lRetVal = .Fields(0)
        .Close
    End With

 
 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    sConn.Execute "UPDATE  [ISCenter_Monitor].[ISCenter_EventLog]" & _
                    " Set" & _
                        " ErrorMessage='" & sErr & "'," & _
                        " AffectedRows =" & RecCt & "," & _
                        " StepSucceeded=" & Abs(nResults) & _
                    " WHERE EventID =" & lRetVal, , adCmdText

   sConn.Close

   On Error GoTo 0
   Exit Function

ParamSPT_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ParamSPT of Module basErrorEventLog"
End Function

Open in new window


{note: AIR CODE!!!}

Comments:
- It does not appear that you use the 'else' part of your stored procedure since you are updating with an SQL statement ... which is fine, but you should likely remove that part from your sp.  

- Remember, parameters begin with @, but column names typically do not.

- With the code you posted --- is that production code?  if so, it does not make sense to insert a new row and set SOME fields, then UPDATE that row with additional data --- seems you could do all that with the INSERT --- so --- just wondering if this is 'play' code to learn behaviors and how to do something?

---

Gotta run for now, hopefully this helps you out!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karen SchaeferBI ANALYSTAuthor Commented:
Thanks for your input.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.