Pass variable from one sub to another

1.7k Views Asked by At

How can I pass uniqueId and uniqueId from sub run to Sub DisplayCustomError. I tried to pass through DisplayCustomError but it's giving "Cannot use parentheses when calling a Sub".

Expected result: uniqueId and uniqueId should go to Sub DisplayCustomError to create a json object.

sub run
    On Error Resume Next
    wrapper.getVariable( "IRR" ).value = excel.range( "'Cases'!$H$783" )
    Dim uniqueId , uniqueId , errorMessage
    If Err.Number <> 0 And excel.range( "'Cases'!$H$783" ) = "" Then
     errorCode = "MC2006"
     uniqueId = "12"                 
     errorMessage= "Error while executing EVMLite.           
     DisplayCustomError(errorMessage)
     On Error Goto 0         
     Call Err.Raise(vbObjectError + 10, "EVM Failed to execute. ", errorMessage)  
    End If      
end sub

Sub DisplayCustomError(errorMessage)
If Err.Number <> 0 Then
    Dim objHTTP, URL, json, uniqueId, networkInfo, jobId
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")    
    URL = "http://10.93.24.223:9005/vpp/logerror"
    objHTTP.Open "POST", URL, False
    objHTTP.SetRequestHeader "Content-Type", "application/json"
    json = "{""jobId"": """& jobId &""", ""uniqueId"": """& uniqueId &""", ""errorCode"": """& errorCode &""", ""errorMessage"": """& errorMessage &"""}"
    objHTTP.send (json)
 End If

end sub

1

There are 1 best solutions below

15
Shai Rado On

Change the Call line from:

DisplayCustomError(errorMessage)

To:

DisplayCustomError errorMessage 

Edit 1: to pass multiple parameters:

First, you need to re-define your Sub:

Sub DisplayCustomError(errorMessage As String, uniqueId As Long)

Then, when you call it, make sure you pass the correct number and type of parameters:

DisplayCustomError errorMessage, uniqueId

B.T.W you can pass the parametes with differnet names and it will still work. For example:

DisplayCustomError errorMessage, uniqueId

And then

Sub DisplayCustomError(errMsg As String, uId As Long)

Edit 2: Full code edited (relevant parts)

Sub run()

    On Error Resume Next
    wrapper.getVariable("IRR").Value = Excel.Range("'Cases'!$H$783")

    ' modified the line below
    Dim uniqueId As String, errorMessage As String

    If Err.Number <> 0 And Excel.Range("'Cases'!$H$783") = "" Then
        ErrorCode = "MC2006"
        uniqueId = "12"
        errorMessage = "Error while executing EVMLite.           "
        DisplayCustomError errorMessage, uniqueId ' <-- modifed this line
        On Error GoTo 0
        Call Err.Raise(vbObjectError + 10, "EVM Failed to execute. ", errorMessage)
    End If

End Sub

Sub DisplayCustomError(errMsg As String, uID As String) ' <-- modifed this line

If Err.Number <> 0 Then
    Dim objHTTP, URL, json, networkInfo, jobId ' <-- removed uniqueId from this line
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = "http://10.93.24.223:9005/vpp/logerror"
    objHTTP.Open "POST", URL, False
    objHTTP.SetRequestHeader "Content-Type", "application/json"

    ' --- modifed the line below ---
    ' *** WHere do you get the value of jobId and ErrorCode ***
    json = "{""jobId"": """ & jobId & """, ""uniqueId"": """ & uID & """, ""errorCode"": """ & ErrorCode & """, ""errorMessage"": """ & errMsg & """}"
    objHTTP.send (json)
End If

End Sub