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_OnClickI am first retrieving the value for the variablevValueToDisplay, 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:
Then let’s create some buttons:
Now we have to assign the actions for “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:
Example Application
Sample QlikView application for passing parameters to macros
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!
Possibly related posts:
- QlikTip #17: Simulating the $(include) command in QlikView macros
Within load-scripts in QlikView there is the useful “$(include)” command available for including files containing some script to be used within the load-script. Doing so it is easy to encapsulate and reuse some code used... - QlikTip #19: Suppressing Macro-Security (Module Security) Dialog on QlikView-Server/QlikView-Documents
When opening documents with macros the end-user will be shown a dialog to define the desired macro-security/module security (in the QlikView Windows Client or the QlikView IE Plugin): But what, If you do not want...







Mag. Stefan WALTHER
6 Comments
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
Hi Meseker,
do you mean the “Edit Module” button?
Best regards
Stefan
Hi Stefan,
yes i meant the “Edit Module”
Best,
Meseker
Sorry, I can’t reproduce this behaviour!
Best regards
Stefan
The question was not to reproduce it for me but merely if you had this same experience before!
No, I did not have the same experience before
Best regards
Stefan
2 Trackbacks
[...] 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 [...]
[...] QlikTip # 18: A workaround for passing parameters to QlikView-macros [...]