In a QLikView script generate code dependent of parameters in function call - code-generation

I have a script in QLikView to extract and check my data. It has to perform a data extraction for 12 separate times. I made a function that works properly but some things are hardcoded and I want to call the same function 12 times with different parameters. The question that I have is about code generation dependent of the parameters in the function.
A part of my code is :
SUB SplitsenOpMiddel(middel, eersteVraag, laatsteVraag)
Temp_Middel_$(middel):
LOAD
Veldzoeknaam AS ZoekNaam,
VeldP_id AS $(middel)_VeldP_id,
Middel AS $(middel)_Middel,
MATEnr AS $(middel)_MATE,
IF(Vraagnr=1, VeldWaarde) AS $(middel)_Vraag001,
IF(Vraagnr=2, VeldWaarde) AS $(middel)_Vraag002,
IF(Vraagnr=3, VeldWaarde) AS $(middel)_Vraag003,
IF(Vraagnr=4, VeldWaarde) AS $(middel)_Vraag004,
IF(Vraagnr=5, VeldWaarde) AS $(middel)_Vraag005,
IF(Vraagnr=6, VeldWaarde) AS $(middel)_Vraag006,
IF(Vraagnr=1, VeldTypeGrp) AS $(middel)_Vraag001Type,
IF(Vraagnr=2, VeldTypeGrp) AS $(middel)_Vraag002Type,
IF(Vraagnr=3, VeldTypeGrp) AS $(middel)_Vraag003Type,
IF(Vraagnr=4, VeldTypeGrp) AS $(middel)_Vraag004Type,
IF(Vraagnr=5, VeldTypeGrp) AS $(middel)_Vraag005Type,
IF(Vraagnr=6, VeldTypeGrp) AS $(middel)_Vraag006Type
RESIDENT Data2
WHERE Vraagsort = '$(middel)';
In this case, the function call is :
CALL SplitsenOpMiddel('A', 1, 6)
So far, the numbers 1 to 6 are hardcoded but what I want is generate this code dependent of the second and third parameter.
My second function call will be :
CALL SplitsenOpMiddel('B', 7, 10)
And automatically the code needs to be :
Temp_Middel_$(middel):
LOAD
Veldzoeknaam AS ZoekNaam,
VeldP_id AS $(middel)_VeldP_id,
Middel AS $(middel)_Middel,
MATEnr AS $(middel)_MATE,
IF(Vraagnr=7, VeldWaarde) AS $(middel)_Vraag007,
IF(Vraagnr=8, VeldWaarde) AS $(middel)_Vraag008,
IF(Vraagnr=9, VeldWaarde) AS $(middel)_Vraag009,
IF(Vraagnr=10, VeldWaarde) AS $(middel)_Vraag010,
IF(Vraagnr=7, VeldTypeGrp) AS $(middel)_Vraag007Type,
IF(Vraagnr=8, VeldTypeGrp) AS $(middel)_Vraag008Type,
IF(Vraagnr=9, VeldTypeGrp) AS $(middel)_Vraag009Type,
IF(Vraagnr=10, VeldTypeGrp) AS $(middel)_Vraag0010Type
RESIDENT Data2
WHERE Vraagsort = '$(middel)';
Is there a way in QlikView to perform this?
Another part of my code where I need the same type of code generation is :
Check_Middel_$(middel):
NOCONCATENATE LOAD
ZoekNaam,
$(middel)_VeldP_id,
$(middel)_Middel,
$(middel)_MATE,
IF(ISNULL($(middel)_Vraag001),
IF(ISNULL($(middel)_Vraag002) AND
ISNULL($(middel)_Vraag003) AND
ISNULL($(middel)_Vraag004) AND
ISNULL($(middel)_Vraag005) AND
ISNULL($(middel)_Vraag006), 'G', 'F'))
AS $(middel)_Leeg_Check,
IF($(middel)_Vraag001 = 0,
IF($(middel)_Vraag002 = 0 AND
$(middel)_Vraag003 = 0 AND
$(middel)_Vraag004 = 0 AND
$(middel)_Vraag005 = 0 AND
$(middel)_Vraag006 = 0, 'G', 'F'))
AS $(middel)_0_Check,
IF(($(middel)_Vraag001 <> 0 AND NOT ISNULL($(middel)_Vraag001)),
IF(ISNULL($(middel)_Vraag002) OR
ISNULL($(middel)_Vraag003) OR
ISNULL($(middel)_Vraag004) OR
ISNULL($(middel)_Vraag005) OR
ISNULL($(middel)_Vraag006), 'F',
IF(($(middel)_Vraag002 = 0) AND ($(middel)_Vraag003 = 0) AND ($(middel)_Vraag004 = 0) AND ($(middel)_Vraag005 = 0) AND ($(middel)_Vraag006 = 0), 'M', 'G')))
AS $(middel)_Vol_Check,
$(middel)_Vraag001,
$(middel)_Vraag002,
$(middel)_Vraag003,
$(middel)_Vraag004,
$(middel)_Vraag005,
$(middel)_Vraag006
RESIDENT Middel_$(middel);
DROP TABLE Middel_$(middel);

You need to "build" the script on the fly. The code below shows how this can be achieved for the first function SplitsenOpMiddel can look. Calling this function like this call SplitsenOpMiddel('A', 1, 6) will generate the table with the required fields.
You can have a look at the sample qvw here. Tried to cover both cases but might not work as you expected but will give you an idea.
sub SplitsenOpMiddel(middel, eersteVraag, laatsteVraag)
let Temp_Middel = 'Temp_Middel_' & '$(middel)' & ':' & chr(13) & 'Load' & chr(13);
for b = $(eersteVraag) to $(laatsteVraag)
let Temp_Middel = '$(Temp_Middel)' & 'IF(Vraagnr=' & $(b) & ', VeldWaarde) AS' & ' $(middel)' & '_Vraag00' & $(b) & ',' & chr(13) &
'IF(Vraagnr=' & $(b) & ', VeldTypeGrp) AS' & ' $(middel)' & '_Vraag00' & $(b) & 'Type,' & chr(13);
next
let Temp_Middel = '$(Temp_Middel)' & 'Veldzoeknaam AS ZoekNaam,' & chr(13) &
'VeldP_id AS ' & '$(middel)' & '_VeldP_id,' & chr(13) &
'Middel AS ' & '$(middel)'& '_Middel,' & chr(13) &
'MATEnr AS ' & '$(middel)' & '_MATE ' & chr(13) &
'Resident Data2' & chr(13) &
'WHERE Vraagsort =' & chr(39) & '$(middel)' & chr(39) & ';';
$(Temp_Middel);
end sub

If your question is if it could work, I would answer yes. Variables in QlikView are handled as text to be replaced when invoked, so I see to reason why it shouldn't work.

Related

LibreOffice Calc macro to save a named sheet in a csv file

There is a Calc file named as 'Data.ods' with a sheet named 'DataSheet' which should be saved in a 'Data.csv' file in the current directory using {tab} as field delimiter and double quoted data fields. How to implement this in LO Basic macro?
You have not specified whether the macro should open the 'Data.ods' spreadsheet or whether it is already open. This code works with the current spreadsheet:
Sub Generate_CSV()
Dim sURL As String ' URL of current spreadsheet
Dim FileN As String ' URL of target CSV-file
Dim oCurrentController As Object ' Before save - activate sheet sSheetName
Dim storeParms(2) as new com.sun.star.beans.PropertyValue
Const sSheetName = "DataSheet"
GlobalScope.BasicLibraries.LoadLibrary("Tools") ' Only for GetFileName
sURL = thisComponent.getURL()
If Not thisComponent.getSheets().hasByName(sSheetName) Then
MsgBox ("Sheet """ & sSheetName & """ Not Found In Current Spreadsheet")
Exit Sub
EndIf
FileN = GetFileNameWithoutExtension(sURL) & ".csv" ' For Data.ods it will be Data.csv
REM Options to StoreTo:
storeParms(0).Name = "FilterName"
storeParms(0).Value = "Text - txt - csv (StarCalc)"
storeParms(1).Name = "FilterOptions"
storeParms(1).Value = "9,34,,65535,1,,0,true,true,true"
REM About this string see https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
storeParms(2).Name = "Overwrite"
storeParms(2).Value = True
REM Activate sheet for export - select "DataSheet"
thisComponent.getCurrentController().setActiveSheet(thisComponent.getSheets().getByName(sSheetName))
REM storeToURL can raises com.sun.star.io.IOException! Only now:
On Error GoTo Errorhandle
REM Export
thisComponent.storeToURL(FileN,storeParms())
MsgBox ("No Error Found,Upload file is saved : """ + ConvertFromUrl(FileN) + """.")
Exit Sub
Errorhandle:
MsgBox ("Modifications Are Not Saved,Upload File Not Generated" & chr(13) _
& "May be table " & ConvertFromUrl(FileN) & " is open in another window?")
Exit Sub
Resume
End Sub
(It was posted on 18 Nov 2011 there)

Why is this block of code producing runtime error 91

This block of code is causing runtime error 91 or with block not set error
this code is for opening reports in crystal report in vb6
For DocCodeCount = 0 To cboDoctorsCode.ListCount - 1
strReportTitle = "DOCTOR'S TRANSMITTAL COPY"
strSqlStatement = "PFMS '" & cboControlCode.Text & "', '" & cboDoctorsCode.List(DocCodeCount) & "', '" & sCurrentUserName & "'"
strFilename = App.Path & "\Reports\ClaimsBillProc\PF MS.rpt"
newRpt.OpenReport vADOConnection, strSqlStatement, strFilename, strReportTitle, 3, False
Set newRpt = Nothing
Next
When used in 1 time opening of report it runs fine but when it is used in this manner in the loop it causes error.
The answer is quite clear: You destroy your object within the loop.
This line is the obvious reason for your one time wonder:
Set newRpt = Nothing

How to run program with variable command line arguments?

I have the following script:
For $alpha = 1 to 10
For $beta = 1 to 10
Run('"C:\Users\MyProg.exe ' & alpha/10 & ' ' & beta/10 & ' 200 2 0.5'
;some other actions follow
Next
Next
I have checked many times that the string is well-formed, thus I have no idea why the script wouldn't run the program. Could you help me please?
Just replace the ending ' with "') and use proper variable names including the $... like $alpha instead of just alpha. Your syntax check in SciTE should have told you.
For $alpha = 1 to 10
For $beta = 1 to 10
Run('"C:\Users\MyProg.exe ' & $alpha/10 & ' ' & $beta/10 & ' 200 2 0.5"')
;some other actions follow
Next
Next

Write string as it is to a file in Matlab

In a matlab script, I'm generating a latex table. A part of that table for example looks likes this.
\multirow{2}{*}{\textbf{b1}}
&
2 & 3 & 10092 & 10763 & 103390 & 2797 & 2929 & 3008 & 5\% & 8\% \\
& 4 & 2 & 20184 & 10763 & 74508 & 1830 & 1970 & 2029 & 8\% & 11\% \\
This string is saved in variable str. Now when I try to write str to a file by using the following code.
f = fopen( 'report\results.tex', 'w' );
fprintf( f, str );
fclose(f);
I get the following warning.
Warning: Invalid escape sequence appears in format string.
See help sprintf for valid escape sequences.
That is probably due to many backslash characters in my string, which is used as escape sequence. Now how can I print this string to a file as it is.
escape the backslashes and percent signs:
str = strrep(str,'\','\\');
str = strrep(str,'%','%%');
If it's just text your printing, this'll be fine.
Minimal working example:
str = '2 & 3 & 10092 & 10763 & 103390 & 2797 & 2929 & 3008 & 5\% & 8\% \\'
str = strrep(str,'\','\\');
str = strrep(str,'%','%%');
f=fopen('testing123.txt','w');
fprintf(f,str);
fclose(f);
and the file reads:
2 & 3 & 10092 & 10763 & 103390 & 2797 & 2929 & 3008 & 5\% & 8\% \\
OR as Ben A. suggests, use fwrite:
fwrite(f,str)
and I think
fprintf(f,'%s',str)
will also do the trick, and it's best to also include a newline:
fprintf(f,'%s\n',str)

Vbs script to check date and extension

Hi I cant get the below script ive worked on to pickup the extension of the files, Can any help me out by pointing where I have gone wrong?
dim fileSystem, folder, file
dim path
dim count : count = 0
path = "C:\temp"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)
for each file in folder.Files
if file.DateLastModified > dateadd("h", -24, Now) & File.name = "txt" then
count = count + 1
end if
Next
if count < 4 then
Set WshShell = WScript.CreateObject("WScript.Shell")
strcommand = "eventcreate /T ERROR /ID 666 /L Application /SO BESROffsite /D " & _
Chr(34) & count & " Files found please check offsite copy " & Chr(34)
WshShell.Run strcommand
wScript.Quit ( 1001 )
Else
Set WshShell = WScript.CreateObject("WScript.Shell")
strcommand = "eventcreate /T Information /ID 666 /L Application /SO BESROffsite /D " & _
Chr(34) & count & " Files found offsite is working fine " & Chr(34)
WshShell.Run strcommand
wScript.Quit ( 0 )
End if
File.name is the full name including the extension, to test for the extension;
if ... fileSystem.getExtensionName(file.name) = "txt" then
You also want the logical And not the bitwise concatenating & in their too.
Alex's answer is the one you want, but just for reference if you were working just with vbs and a string filename, without the filesystemobject collection you could achieve the same via:
Right(strFilename, Len(strFilename) - Instrrev(strFilename, "."))
This essentially finds the position of the final "." in the filename, takes this away from the length of your filename, and then gives you however many character's that equals from the right hand side. This could be amended slightly to use the "Mid" command rather than the "Right" but I don't think it matters too much in a case like this.