Hi Guys,
Getting quite determined to start my database app. I've got a Main stack and within that main stack I have a function which connects to the database and returns a connectionId into a global field called connID. This all occurs on the preopen stack command and is present in the stack script.
I've then created a substack called customer (and I'll create others called Supplier, Timesheets etc), and am evaluating if on preopen (connID>0) populate my datagrid. This is producing an error, so my question is, if I have made a connection in the main script, can I use that connection id in substacks or do I need to create a new connection for each substack.
Or do I have to take it further and create a connection everytime I query the database.
Hope my question makes sense. Really want to get going with the designing the core of my system.
Thanks
Database connection ID
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Re: Database connection ID
Hi jalz,
## returns a connectionId into a global field called connID
There is no such thing as "global field"
Is this a FIELD or a (global) variable?
Of course you can access the database from all (sub-)stacks, you could do this:
1. use a GLOBAL variable instead of a field
2. Access that field that holds the CONNECTIONID from the substack:
...
put fld "the one with the connection ID" of cd 1 of stack "your mainstack here" into tConnID
...
Important hint:
## This all occurs on the preopen stack command
This will FAIL in a standalone stack, since the neccessary Livecode libraries (database lib in this case)
have not been loaded at that point! Use "openstack" or "opencard" instead!
Best
Klaus
## returns a connectionId into a global field called connID
There is no such thing as "global field"

Is this a FIELD or a (global) variable?
Of course you can access the database from all (sub-)stacks, you could do this:
1. use a GLOBAL variable instead of a field
2. Access that field that holds the CONNECTIONID from the substack:
...
put fld "the one with the connection ID" of cd 1 of stack "your mainstack here" into tConnID
...
Important hint:
## This all occurs on the preopen stack command
This will FAIL in a standalone stack, since the neccessary Livecode libraries (database lib in this case)
have not been loaded at that point! Use "openstack" or "opencard" instead!
Best
Klaus
Re: Database connection ID
Hi Klaus,
Apologies, I was in my FileMaker mode (my day job) You're right it was the global variable. I'm storing the connID in a global variable.
Thank you for the hint regarding opencard. I'll see how I get on later on when I am at home. I'll see if I can use the connID from the Main stack to access the records in a substack. if not I'll post my code (which is fairly basic) to get further advice
All the best
Apologies, I was in my FileMaker mode (my day job) You're right it was the global variable. I'm storing the connID in a global variable.
Thank you for the hint regarding opencard. I'll see how I get on later on when I am at home. I'll see if I can use the connID from the Main stack to access the records in a substack. if not I'll post my code (which is fairly basic) to get further advice

All the best
Re: Database connection ID
Hi jalz,
## I'll see if I can use the connID from the Main stack to access the records in a substack.
Sure you can!
You only need to declare it in the script or handler everytime before you use it:
---
global gConnID
## Now use it
...
Best
Klaus
## I'll see if I can use the connID from the Main stack to access the records in a substack.
Sure you can!

You only need to declare it in the script or handler everytime before you use it:
---
global gConnID
## Now use it

...
Best
Klaus
Re: Database connection ID
Hi Klaus,
Thanks didn't realise I have to declare the global variable in each stack? I'll try and get this to work - so far its not working, probably me missing something as usual.
Secondly, The code for openstack code I have written on my main stack, under stack script seems to be executing when I open a substack. Is this normal behaviour? If it is, then I suppose I need to put the code stack script of the main stack into the card script so it doesnt execute all the time.
Apologies for the newbie questions, trying to get my teeth into live code
Thanks didn't realise I have to declare the global variable in each stack? I'll try and get this to work - so far its not working, probably me missing something as usual.
Secondly, The code for openstack code I have written on my main stack, under stack script seems to be executing when I open a substack. Is this normal behaviour? If it is, then I suppose I need to put the code stack script of the main stack into the card script so it doesnt execute all the time.
Apologies for the newbie questions, trying to get my teeth into live code

Re: Database connection ID
Exactly. That's the preferred way to do things. In fact, the docs on openstack and preopenstack sayI suppose I need to put the code stack script of the main stack into the card script so it doesnt execute all the time.
Sent to the destination card right after you open a stack.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev