Search code examples
vbscriptmsgbox

Is this possible to call users define sub procedure using msgbox?


For example code:

show_box()
Function show_box()
result = MsgBox ("Please follow steps in document"vbCrLf & vbCrLf _ 
                 & "Click OK to call sum procedure" & vbCrLf & vbCrLf _
                 & "Click No to call substraction procedure"& vbCrLf & vbCrLf _
                 & "Click cancel to print hello")
Select Case result
       case 1
            msgbox(sum(1,2))
       case 7
           msgbox(substraction(4,2))            
       Case 2
           msgbox("Hello")
       End Select
END Function


sub sum(a,b)
  sum = a+b
  msgbox(sum)
end sub

sub substraction(a,b)
  substraction = a - b  
  msgbox(substraction)
end sub

The result should be: When I click on OK, then call sum(a,b) procedure, and so on. I tried many times using different approach but I was not able to fix that. Help will be most appreciated!!


Solution

  • Try something more like this instead:

    Function sum(a, b)
      sum = a + b
    End Function
    
    Function substraction(a, b)
      substraction = a - b  
    End Function
    
    Sub show_box()
      Dim result
      result = MsgBox ("Please follow steps in document" & vbCrLf & vbCrLf _ 
                     & "Click Yes to call sum procedure" & vbCrLf & vbCrLf _
                     & "Click No to call substraction procedure" & vbCrLf & vbCrLf _
                     & "Click Cancel to print hello",
                     vbYesNoCancel)
      Select Case result
        Case vbYes
          MsgBox(CStr(sum(1,2)))
        case vbNo
          MsgBox(CStr(substraction(4,2)))
        Case vbCancel
          MsgBox("Hello")
      End Select
    End Sub
    
    show_box()