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

  1. Meseker
    Posted January 22, 2010 at 06:25 | Permalink

    Hi Stefan,

    I have been using so far QlikView 8.5. I tried to install yesterday V9 without uninstalling the previous version but to a different folder in …\Program Files. It didn’t complain and everything looked fine. I tried to do what you did in your examples with the vValueToDisplay etc. and when I get to the point to add a new action to run a macro, QV just gave me the option to type in the name of the macro without the Edit Module… button. So I had to type in the name of the macro, come out of the wizard for creating the actions and went to the usual way of editing the module, tools\edit module. This works fine but why is the button not available anymore? Is it a common problem?

    Thanks for your help
    Meseker

    • Posted January 22, 2010 at 09:48 | Permalink

      Hi Meseker,

      do you mean the “Edit Module” button?

      Best regards
      Stefan

      • Meseker
        Posted January 22, 2010 at 22:50 | Permalink

        Hi Stefan,

        yes i meant the “Edit Module”

        Best,
        Meseker

        • Posted January 29, 2010 at 13:09 | Permalink

          Sorry, I can’t reproduce this behaviour!

          Best regards
          Stefan

          • Meseker
            Posted January 29, 2010 at 20:00 | Permalink

            The question was not to reproduce it for me but merely if you had this same experience before!

    • Posted February 4, 2010 at 19:20 | Permalink

      No, I did not have the same experience before :)

      Best regards
      Stefan

  2. Rich Tefft
    Posted May 9, 2014 at 17:57 | Permalink

    In v10 and 11, we have found that QlikView seems to exectuve Action steps asynchronously. It does not wait for step 1 to complete before executing step 2, etc. In your example, we found that sometimes the variable is set correctly and the macro sees it, and sometimes the macro executes before the variable value is set. This renders a lot of our previous work useless, and IMHO is a huge bug in QlikView.

2 Trackbacks

  1. […] This post was mentioned on Twitter by countnazgul, Stefan Walther. Stefan Walther said: New Blog-Entry: : QlikTip # 18: A workaround for passing parameters to QlikView-macros http://bit.ly/5OPgA6 […]

  2. […] QlikTip # 18: A workaround for passing parameters to QlikView-macros […]