Skip to main content

global variable

Hi, I am new to using IDEAScripting. I would like to define global variable(s) in the main sub that I can then use in functions. Could you give me an example of how to do this? I am doing this so that I can change the name of the database to re-use the macro. Currently I am having to define the same variable in each function. 
thanks for your time,
Mike

klmi Wed, 09/25/2019 - 09:59

Hi mikew,
what you want to do sounds a little bit strange. Regarding functions you don't need to assign global variables. Example for using functions:

Option Explicit

'global variable
Dim a As Integer

Sub Main()
Dim b As Integer
Dim iResult As Integer
a = 3
b = 4
iResult = addition(a, b)
End Sub

Function addition(iNumber1 As Integer, iNumber2 As Integer) As Integer
addition = iNumber1 + iNumber2
End Function

I recommend to use the IDEA Macro Recorder or the History for the first steps. The generated code shows always direct paths like

Set db = Client.OpenDatabase("Test.IMD")

To execute a recorded macro with the currently opened database change the line above:

Set db = Client.CurrentDataBase()

If you want to show the user a file open dialog you can do that as follows (you will also find other solutions; I do not try to catch exceptions in the example!):

Sub Main()
Dim FileExplorer As Object
Set FileExplorer = CreateObject("IDEAEx.FileExplorer")
FileExplorer.DisplayDialog
dbName = FileExplorer.SelectedFile
Set db = Client.OpenDatabase(dbName)
Set FileExplorer = Nothing
End Sub

mikew Wed, 09/25/2019 - 12:27

In reply to by klmi

Thanks Klmi,
realised I was using a variable that idea didnt like. Changed the name and I could use it as a global variable. 
Maybe I am doing something incorrect:
I recorded a macro that has a number of functions performed on the same original database. This means I had to explicity reference the name of the database in each function. Because I want to use this macro for lots of different databases I was having to change the database name in each function. I changed this to have one glocal variable for the name of the database so that I only have to change this once for each database. Is there a better solution?
 

klmi Thu, 09/26/2019 - 05:09

In reply to by mikew

I like your approach to structure the code with a main sub and other subs or functions. I suggest to choose the file in the main sub and to do other global preparations (f.e. retrieve other input data) if necessary. Then subs or functions with operations to be done can called from the main sub. When it comes to functions I would try to avoid to access global variables. It's a better style of programming to pass these global variables (f.e. the db object) when calling the function in my eyes.

mikew Thu, 09/26/2019 - 03:53

In reply to by klmi

Hi klmi,
when I create a new database say by summarising the current database - will that then become the "current" database?
thanks,
Mike
 

klmi Thu, 09/26/2019 - 06:24

In reply to by mikew

When you use the complete recorded code finally the new database is opened and so becomes current database. Without the line Client.OpenDatabase(dbName) at the end that would not happen. Have a look on the following lines:

Set db = Client.OpenDatabase("Test.IMD")
Set task = db.Summarization
...
dbName = "Sum Field.IMD"
taskOutputDBName = dbName
...
Client.OpenDatabase(dbName)

The use of Client.CurrentDatabase makes sense if you first want to open an IDEA database manually and then execute a macro to make some operations with that database. However you could extract new databases or open the produced databases to work with.