Devendra Vastrakar
Devendra Vastrakar

Reputation: 61

pass multiple argument from one sub to another sub

I am trying to pass multiple arguments from one sub to another sub, but it's not working. Even if I try to define variable as string.

Sub run
    Dim uniqueId, errorMessage, jobId, ErrorCode
    uniqueId = "abcc"
    jobId = "efgh"
    ErrorCode = "ijkl"
    errorMessage = "mnop"
    DisplayCustomError errorMessage, uniqueId, jobId, ErrorCode
End Sub

Sub DisplayCustomError(errorMessage, uniqueId, jobId, ErrorCode)
    WScript.Echo uniqueId
    WScript.Echo jobId
    WScript.Echo ErrorCode
    WScript.Echo errorMessage
End Sub

Upvotes: 1

Views: 6565

Answers (2)

Jean-Pierre Oosthuizen
Jean-Pierre Oosthuizen

Reputation: 2683

VBA and VBScript are a bit different.

But for VBA the following code will work perfectly.

A few pointers:

  • You always wants to Dim all your variables, I understand this is probably just a quick example you provided but I felt to mentioned it.

  • When receiving variables in a Sub you always want to define them in the Sub which is receiving them as to make correct use of the variable in the proceeding code.

  • Have a look here for a question I asked about only passing certain variables to a Sub. This might help when you only want to pass one or two variables to a "receiving" Sub

    Option Explicit
    
    Sub run()
    
        Dim uniqueId As String
        Dim errorMessage As String
        Dim jobId As String
        Dim ErrorCode As String
    
        uniqueId = "abcc"
        jobId = "efgh"
        ErrorCode = "ijkl"
        errorMessage = "mnop"
    
        DisplayCustomError errorMessage, uniqueId, jobId, ErrorCode
    
    End Sub
    
    Sub DisplayCustomError(ByVal errorMessage As String, ByVal uniqueId As String, _
                           ByVal jobId As String, ByVal ErrorCode As String)
    
        Debug.Print uniqueId
        Debug.Print jobId
        Debug.Print ErrorCode
        Debug.Print errorMessage
    
    End Sub
    

Upvotes: 3

Plagon
Plagon

Reputation: 3138

It has to be Call DisplayCustomError(...) because its a sub, only functions are called withouth anything in front. Also:

Sub DisplayCustomError(ByVal errorMessage as String, ByVal uniqueId as String, ByVal jobId as String, ByVal ErrorCode as String)

Upvotes: -1

Related Questions