Skip to main content
Announcements
Qlik Introduces a New Era of Visualization! READ ALL ABOUT IT
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Set default field selection using variable - possible??

Hi,

I want to write a macro which will do two things:

1. Clear all selections (I know how to do).

2. Set a field selection based on the content of a variable.

    Example: I want to do something like this

    set = vYearSel = '2016'      -> in load script

    ActiveDocument.Fields("year").Select $(vYearSel)     -> in macro.

When I ran it, the macro failed to parse.   If I hardcode the value in the macro, it will work which isn't what  I want.

Anyone know how I can accomplish this?

Thanks!!

20 Replies
rupamjyotidas
Specialist
Specialist

have you explored 'On Open' Trigger option or 'Set Clear State' Option?

m_woolf
Master II
Master II

From the API Guide

set v = ActiveDocument.GetVariable("Variable1")

v.SetContent "123",true

I don't think there is an OnLeave event.  At the risk of nagging, this is all a lot simpler with actions.

Anonymous
Not applicable
Author

MW,

I'm assuming the "123" you had there is the field selection value.  So going back to our earlier example with the Year.

If 2016 was selected in the Year listbox, How would I save this value into a variable in the Macro?  In other words, how you would not hard coding "123"??

m_woolf
Master II
Master II

I assume you are limiting the year listbox to always 1 selected value.

set val=ActiveDocument.Fields("year").GetSelectedValues

strYear = val.Item(0).Text

set v = ActiveDocument.GetVariable("vYearSel")

v.SetContent strYear ,true

marcus_sommer

I don't think that there is a bug but invisible chars like in: Re: Character limit for a SET command in an INCLUDE file?

- Marcus

Anonymous
Not applicable
Author

MW,

I appreciate your help here.  I was able to gather pieces of code from your replies and put together a couple sub routines to do:

1. OnLeaveSheet -> save my Year selection into a variable

2. OnActivateSheet -> restore the saved value.

Yes, there is an "OnLeaveSheet" event when you right click on sheet -> Properties.  Your earlier reply said there wasn't.  So just wanted to point that out.

OK, now that I got code working and learning new things in QV.  Is this the most efficient way of doing 1 & 2

or there are better solutions and EASIER?  I honestly find using QV Object Model (I guess QV Developers would prefer to

call it QV API ) to be easy as well.  I just haven't been able to find a complete QV Object Model Reference yet.

Anonymous
Not applicable
Author

Marcus, thanks.  Will watch out for this stuff next time.  I just know when things dont' work even thought I've checked, rechecked and rechecked syntax and spelling, I just delete them and re-type.  Most of the times, it will work.

m_woolf
Master II
Master II

As I have said many times in previous posts, this is easier with actions instead of macros. Add to that, macros generally work only with Internet Explorer and the QV Plugin (there are some exceptions.

In the OnLeaveSheet trigger, you can add an Action that saves your Year selection into a variable.

In the OnActivateSheet trigger, you can add an action that selects the Year stored in the variable. No VB code is necessary.

If you found any of this useful, please mark replies as Helpful or Correct.

Anonymous
Not applicable
Author

Your comments noted.

Anonymous
Not applicable
Author

MW,

As suggested, I went ahead to try out by setting actions.  OK, I figured out how to save my Year selection to a variable by means of Trigger -> Action.  But I can't seem to figure out to restore these values from the variable back to the Year listbox.  Suppose my vYearSel contains "2015,2016".   How do I restore these selections?