First of all, the way you have written this code you are still specifying the newly opened database. You could have written code that does the same thing with the letfileglobal
statement.
let lvFinYear3 = "2018-19"
let lvNewFile = "Income & expenditure " + lvFinYear3
openfile lvNewFile
// setfilevariable lvNewFile,"lvFinYear4",lvFinYear3
letfileglobal lvFinYear4 = lvFinYear3 // this does the same thing as the previous line
message lvFinYear4
Or, you could have used setfilevariable
with ""
for the database name.
setfilevariable "","lvFinYear4",lvFinYear3
In the code above, you have defined lvFinYear3 as a local variable. So it stays in scope thruout the procedure, even though the current window and current database change when lvNewFile is opened.
If you define lvFinYear3 via a popup menu button, it will not be a local variable – it will be either a fileglobal or windowglobal variable (probably fileglobal, as that is the default unless you change the form preferences). Either way, that variable will go out of scope as soon as the openfile
statement runs. Now there is a different database, and a different window, so there are now different sets of fileglobal and windowglobal variables. You can’t access any of the ones associated with the previous database any more. So the error has nothing to do with setfilevariable
, the error would occur if you accessed lvFinYear3 in any way. As far as the program is concerned, lvFinYear3 doesn’t exist any more.
There are two solutions to this. The first one is to copy the value into a local variable.
let tempValue = lvFinYear3
let lvNewFile = "Income & expenditure " + lvFinYear3
openfile lvNewFile
letfileglobal lvFinYear4 = tempValue
message lvFinYear4
The second method is to use the fileglobalvalue(
function, which allows you to access any fileglobal variable in any open database, not just the current database.
let wasDB = info("database") let lvNewFile = "Income & expenditure " + lvFinYear3
openfile lvNewFile
letfileglobal lvFinYear4 = fileglobalvalue(wasDB,"lvFinYear3")
message lvFinYear4
In these examples I used letfileglobal
instead of setfilevariable
, because as I explained before they are doing the same thing in this code. But you could also use this same technique with the setfilevariable
statement.
setfilevariable "Some Other Database","Some Variable",fileglobalvalue("Another Database","Some Other Variable")