QlikTip #18: A workaround for passing parameters to QlikView-macros

When calling macros from the user interface you cannot pass a parameter to the function called in QlikView.

This behavior is quite annoying …!

But the workaround explained here will show you a possibility how you can “simulate” passing parameters to macro-functions:

The idea behind is quite simple. In QlikView 9 we have now actions which can be added for any event (e.g. the OnClick event for buttons). The clue is that we can add multiple actions for every event, so I am doing the following:

OnClick Event for Button “cmdXY”:

  • Changing the value of a variable (e.g. vValueToDisplay)
  • Calling a function/sub in your macro (e.g. cmdXY_OnClick)
  • Within the macro and the function cmdXY_OnClick I am first retrieving the value for the variable vValueToDisplay, then I am executing the “normal” macro-code using the value read from the variable

Step by step with some screenshots:

First let’s create the variable vValueToDisplay:

Create the variable vValueToDisplay

Then let’s create some buttons:

Now we have to assign the actions for “Button 1”:

Adding the action of Action-Type "External" and "Set Variable" to Button 1

Explanation:

  • Go to the properties of the button
  • Change to the Tab Actions
  • Then select Action-Type External and Set Variable

Then we set the desired parameters:

Then we have to trigger the macro:

  • Add a second action of Action-Type External and Run Macro and enter the following values:

So, we have nearly finished, the last step is to create the macro (Go to Tools => Edit Module or use Shortcut Ctrl+M):

'// *************************************************************
'// Displays a simple message-box, showing the value of the
'// variable vValueToDisplay
'// ~~
'// This sub assumes that the variable "vValueToDisplay"
'// has been set before !!!
'// *************************************************************
sub GenericClick

'// First let's retrieve the content of the variable "vValueToDisplay"
Dim strValueToDisplay 'as String
strValueToDisplay = ActiveDocument.Variables("vValueToDisplay").GetContent().String

'// Create an alert
msgbox("The value of ""vValueToDisplay"" is: " & strValueToDisplay)

end sub

Clicking on the button will now show the content of the variable:

Further examples:

Please find some further usage-examples in the following QlikView-example-application:

How I am using this:

The example above is quite simple. In reality I am using for more complex situations, e.g. for creating Excel-sheets and so on. Therefore I am setting some variables before and then I am calling the desired function which requires the above variables to be set before …

Note at the end:

In my example you’ll find a sheet “Testing Real Parameters” where I have tested passing “real” parameters to macro-functions, but did not succeed.
Maybe I just did not manage to call a macro with a parameter correctly … If so, please tell me! :)

Bookmark and Share

and tagged , , , , , , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

7 Comments