Concising IF ... then statement

9 posts / 0 new
Last post
Pawelski_33
Offline
Joined: 06/05/2018 - 12:16
Concising IF ... then statement

Hello All,
I am wondering if the attached code (if statements ) can be somehow made clearer and shorter (maybe with the array of functions). This script is supposed to perform some functions based on the input from the user through the DropListBox (names of the fields in the data source presented as a string) .The functions are not perforing any tasks now (I have the rest of the code but its very lenghty due to number of different tasks included like extraction and summarization).At the moment it works up until the first If statement( but the whole IF section needs to be commented first for obvious reasons):
'Append_Period
               If UserChoice(4) <> "N/A" Then
Also I wanted to display the names of the functions (all of them) my program run through to be displayed in the msgbox but I am not sure how to get the name of the function and pass it as a string to the message box.
Any help is much apprieciated.
Thank you

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Pawelski_33,

Unfortunately IDEAScript doesn't allow for functions in an array unlike other languages so that is not an option.

In your variable declaration you have mutiple variables on the same line, in theory this should work but unfortunately there is a bug in IDEA in which only the last variable of the line would be properly defined and all the other would be defined as a variant.  Generally you won't notice a difference but in some case it could cause a problem, such as when you are passing it between functions.  So you can keep it as is knowing that the variables are mostly defined as a variant or have one line per variable.

For the keeping track of the functions sounds like you want to have some sort of logging.  There are different options that I have seen and used over the years, some of these include logging to a new database or to a text file but if you want to use a message box you might want to do something like this:

Have a global variable to hold the message such as Dim sLogEntry as String

Then create a function to hold the information:

Function log(sMessage as text)

     sLogEntry = sLogEntry & CrLf & sMessage

end Function

Then in each function you can add a message so the one that call Append_Period() you would have something like:

     call log("Append Period Performed")

and it would be added to the log.

From your script I don't see any other way than using multiple if statements to do the validation and then perform the tests if the information is available.

Other things you might want to add is right now your drop-downs hold all the same information, you might want to have only numeric fields for the numeric columns, date for date, etc.  This would make sure that a user doesn't select a character field when the function is expecting a numeric field.

Hope this helps a bit and good luck on your project.

Brian

Pawelski_33
Offline
Joined: 06/05/2018 - 12:16

Hi Brian,
Thank you for your answer - it is interesting way of passing a variable as a text within Function log - I will give it a go.
In the meantime I managed to find the solution to my code but it is a bit long-winded. The final effect is the message box which I wanted to see however I am not that happy with the code structure (please see attached).
Any comments are welcome.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

For your code why don't you make the CrLf as a global variable or even a constant and that way you don't need to keep setting it up.  Add this at the begining of the script:

Const CrLf = Chr(10) & chr(13)

Do you want the message box popping up each time or do you want to just have it pop up at the end? When do you want to have the messages appear?

Pawelski_33
Offline
Joined: 06/05/2018 - 12:16

Good point Brian - I added variable CrLf  to use it in the MsgBox displayed for PerformAnalysis Function only but as it is handy for the second MsgBox I used it as well within each section. I only started writing code in IDEA last week so long way to learn I guess.
To answer your question , I would only like the message to appear once at the end of the procedure with all the names of the function visited by the procedure but wasnt sure how to get about it.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

If you want only as the last line of code then remove all the entries except for the last one.  You are already using the FunctionExecutionMsg variable to accumulate all the messages, just make sure that this variable is a global variable and can be accessed through the script and you should be good.

Like anything else learning a new language takes time and patience.

Pawelski_33
Offline
Joined: 06/05/2018 - 12:16

Thanks Brian.
Declaring variable FunctionExecutionMsg  as a Global variable works but declaring Const CrLf = (13) & Chr(10) doesnt. No matter where I place it or whether I declare it as Const or Global Const it always generated the same syntax error.
The only way it works is when I place CrLf = (13) & Chr(10)  at the begining of each function.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Pawelski_33,

Declare it as a global variable and then define it at the beginning of the script and it should work.


Dim CrLf As String

Sub Main
	CrLf = Chr(13) & Chr(10)
	MsgBox "This works with a Carriage Return" & CrLf & "Line 2"
End Sub

Pawelski_33
Offline
Joined: 06/05/2018 - 12:16

Thank you Brian. It worked as you said.