Store a sub name in a variable, and then call it from that variable

Hi everyone,

I’m trying to be organized and I need some help. I have a busy querysave (lots of scenarios/checks). Based on whatever scenario I fall in (if the save is successful) I need to call one of 6 notification subs. I could put the call to the notification_type_X sub at the end of querysave, but I don’t want to hold up the save for that OR I could put it in postsave, but I don’t want to duplicate all the scenario checks I’ve just performed in querysave.

So… I was thinking about storing the name of the notification sub I need to go to in a global variable during querysave, and running it in postsave. But what is the syntax for that?

at the end of querysave:

globalFlag = “sub_NotificationType_X()”

in postsave, what is the syntax for something like:

call

Thanks very much!

Rachel

Subject: Store a sub name in a variable, and then call it from that variable

I don’t think you can do that directly - the variables don’t last long enough, do they? BUT why not just store the sub in a field at query save and then pick it up at postsave?

Chris

Subject: RE: Store a sub name in a variable, and then call it from that variable

Well, you can use Execute, but it would probably be better to use an If or Select Case to run the code based on a variable value. (Form Globals will persist and be available through all form events.)

Subject: RE: Store a sub name in a variable, and then call it from that variable

Hi Stan!

Execute is just what I was looking for. Thank you!

I understand global variables, so that’s not an issue. Performance wise, however, is it worth it? Or would it be more efficient to do the select case?

Thanks (again), Stan!

Rachel

Subject: RE: Store a sub name in a variable, and then call it from that variable

There are two general problems with Execute. One is that the code hasn’t been compiled on save, so it will always be slower to run. With the statements you plan to use, though, that might not be noticeable (what’s a millisecond between friends). The second is that you have to be very careful to avoid executing user input directly, since you can accidentally give the user some pretty dangerous tools. Again, that wouldn’t appear to be an issue with the way you’re using it. Personally, I don’t like to see Execute (or eval() in JavaScript, etc.) in code, since the alternatives are always faster and safer, if a little more trouble to develop initially. But that’s just one old crank’s perpective.

Subject: RE: Store a sub name in a variable, and then call it from that variable

Got it! Thanks again for your help, Stan!

Have a nice day,

Rachel

Subject: Store a sub name in a variable, and then call it from that variable

Hi,

if you want to use a variable in your QuerySave-Event AND in your PostSave-Event then you need to “make it global”. To do this you need to declare it in the “(Declarations)”-Part of your form instead directly in the QuerySave-Event. The syntax is the same “Dim VariableName As String”.

(You can even declare it in the “(Declaration)”-part of the “(General)” section and then you can use this “Global” variable in enven more elements on your form.)

To call a function which name you have in a variable you can use “Execute(FunctionName)”.

But I would not recommend it, because the debugger can’t “dive” into such a function.

You will be much better of to pass in your global variable only the type of action you want to perform and then call the separate subs in your Post-Save event like this:

Select Case ActioType

Case “1” : Call SubAction1

Case “2” : Call SubAction2

…

Select End

Bye

Hynek

PS: Sorry if some of the explanation is too “trivial” and you already knew this.

Don know if i understood your problem excactly :-))