Page 1 of 2

Send variable from one card to another card?

Posted: Fri May 27, 2011 4:18 am
by jesse
I have a datagrid when a row is clicked on it uses go to card "Edit Card". However... I can't figure out how to send the value
of a variable from the first card the "datagrid card" to the "edit card" card. Can someone please help me :(

Re: Send variable from one card to another card?

Posted: Fri May 27, 2011 5:48 am
by dunbarx
Use the "send" command:

Send yourMessage && yourVar to card yourCard.

The variable is send as a parameter along with the message. You can send as many as you want:

Send yourMessage && "1,2,3" to card yourCard.

Check out "the params", and all the "see also's"

Craig Newman

Re: Send variable from one card to another card?

Posted: Fri May 27, 2011 11:45 am
by Klaus
Yep, and better use QUOTES and parenthesis!

...
send ("yourMessage" && yourVar) to card "yourCard"
...
send ("yourMessage" && "1,2,3") to card "yourCard"
...

Best

Klaus

Re: Send variable from one card to another card?

Posted: Fri May 27, 2011 4:56 pm
by jesse
Thanks both. Klaus and Craig. Assuming I would send the variable CurrentID as CurrentID to the card Edit Card.
I tried the below code:

Code: Select all

send (CurrentID && CurrentID) to card "Edit Card"
group "myGrid": execution error at line 28 (Handler: can't find handler) near "W21", char 1

W21 is the value of CurrentID. Any idea whats wrong?

Re: Send variable from one card to another card?

Posted: Fri May 27, 2011 5:07 pm
by Klaus
Hi Jesse,

you cannot "send" ONLY a variable to another card!
This is only possible with handlers and their parameters!

You could make your variable global, so it can be used anywhere in your stacks
Or you set a custom property that can also be accessed anywhere.


Best

Klaus

Re: Send variable from one card to another card?

Posted: Fri May 27, 2011 6:35 pm
by jesse
Klaus,

That is the odd thing I did declare CurrentId as a global variable in the stack script.

Code: Select all

global CurrentID
and then in the new card "Edit Card" for testing purposes I added the code below to the card script, but its
only answering me back the words CurrentID not the value of the CurrentID.

Code: Select all

on OpenCard -- highlight the "go to this card" button in a set
   set the text of field "Field" to CurrentID
   answer CurrentID
  pass OpenCard
end OpenCard

Re: Send variable from one card to another card?

Posted: Fri May 27, 2011 6:45 pm
by Klaus
Hi Jesse,

you need to declare the global var in every script you use it!

Try this:

Code: Select all

on OpenCard
   global CurrentID
   set the text of field "Field" to CurrentID
   answer CurrentID
  pass OpenCard
end OpenCard
Best

Klaus

Re: Send variable from one card to another card?

Posted: Fri May 27, 2011 11:19 pm
by jesse
Thanks! That worked great the first time. It carried over the CurrentID from my Datagrid.
But the second time it did not update the CurrentID and still showed the same ID
as the first time.

Code: Select all

on OpenCard
   global CurrentID
   set the text of field "Field" to CurrentID
   answer CurrentID
  pass OpenCard
end OpenCard
The CurrentID answer showed "U03" which is correct. But then the second time I clicked on another
entry in my Datagrid and it still showed U03 which is incorrect, it did not update the CurrentID variable
on the "Edit Card" card. Am I missing something to do this?

Re: Send variable from one card to another card?

Posted: Sat May 28, 2011 4:18 am
by dunbarx
You are on your way, but do play with messages and parameters. These are basic building blocks, and should be learned almost before anything else. Put this in a card script:

on workWithMessages var1,var2
beep var1
answer var2
end workWithMessages

Now put this in a button script:

on mouseUp
send workWithMessages && "3,I hear three beeps!" to this card
end mouseUp

Do you know about sending a message directly? In the button script, write instead:

on mouseUp
learnAboutMessages 3,"I hear three beeps!"
end mouseUp

There is a difference in syntax. You need to play around. This is utterly essential.

Did you read up on "send"?

Re: Send variable from one card to another card?

Posted: Sat May 28, 2011 4:54 am
by BarrySumpter
I suggest using a custom property for the Grid like:
CurrRecID - which is assigned on each RowChange of the Grid.
Then call your EditRec card
Where the EditRec Card script retrieves CurrRecID from the Grid custom property CurrRecID
- and fills the objects with data from either the Grid columns or from the record using the CurrRecID to retrieve the record using a SQL command from the database.
You can also use this same methodology for DelectRec Card. ( I never ever use a simpel "are u shur" answerBox. I always show the full record.)
Or whatever functionality you want to do with the CurrRecID of that Grid.

hth

Re: Send variable from one card to another card?

Posted: Sat May 28, 2011 7:04 am
by jesse
Thank you to both of you. I am very new Live Code and am still learning. I am great at php though lol. Apparently very different in syntax.
I did purchase the only book out there for Live Code by Dan Shafer Revolution Software At The Speed Of Thought and am going through it right now. I'm currently starting Chapter 6. Hopefully this will expose me to more of the basics and advance functions Live code. I'll be sure to post any additional questions on the forum and hopefully you guys or anyone else can help point me in the right directions.

My ultimate goal to be able to write a complete ERP (Point Of Sale, Invoicing, Inventory, Accounting, etc...) for Small Businesses eventually. I have much experience using an ERP at work and hope to eventually replace it with my own using Live Code. The one we use at work is quite undependable and company has been ripping off its customers for the last few years by locking them into maintenance contracts that actually provide nothing but take lots of money from the small businesses. Some companies are paying 30k a year and get nothing. The company is supposed to provide a major release each year with new features but has not for over three years. I hope to replace the product with my own eventually. I think its possible just got a long way to go to learn how.

Re: Send variable from one card to another card?

Posted: Sat May 28, 2011 2:57 pm
by Klaus
Hi jesse,
jesse wrote:Thanks! That worked great the first time. It carried over the CurrentID from my Datagrid.
But the second time it did not update the CurrentID and still showed the same ID
as the first time.

Code: Select all

on OpenCard
   global CurrentID
   set the text of field "Field" to CurrentID
   answer CurrentID
  pass OpenCard
end OpenCard
The CurrentID answer showed "U03" which is correct. But then the second time I clicked on another
entry in my Datagrid and it still showed U03 which is incorrect, it did not update the CurrentID variable
on the "Edit Card" card. Am I missing something to do this?
Did you declare the global variable in your script that SETS this variable (the first time)?
Can you post this script?


Best

Klaus

P.S.
I am sure I already posted this, but what the egg :D

Check these stacks, great resource to dive into all objects, concepts and mechanisms of LiveCode:
http://www.runrev.com/developers/lesson ... nferences/

Re: Send variable from one card to another card?

Posted: Sat May 28, 2011 5:34 pm
by jesse
This is my Stack script:

Code: Select all

global gConID
global realPath
global LimitBegin
global LimitEnd
local temp
global CurrentID

on preOpenStack
   put empty into gConID 
   set itemDel to slash
   put the effective filename of this stack into realpath
   delete last item of realpath --removes .livecode file name
   put empty into field counter
end preOpenStack

on doConnect -----------------------------------------------------
     put empty into field "counter"
     if gConID >0 then
          answer info "Already Connected"
          exit doConnect
     end if
     try
          put  realpath & "/users.sqlite" into pathNdb
          //put revOpenDatabase("sqlite", pathNdb,,,,,,) into gConID
          put revOpenDatabase("MySQL",clarityproductions.com,"clarityp_lb2011","clarityp_admin","j1220",useSSL) into gConID
     catch theError
          answer info "Problem Opening Database: " & pathNdb & "  " & theError
          put 0 into gConID
          exit doConnect
     end try
     put "SQLite DB: " & pathNdb  into field status-- display SQLIte flile name with path
     answer information return & "Connected to: " & pathNdb & return & " ID is: " & gConID
end doConnect ----------------------------------------------------

on doDisconnect ---------------------------------------------------
   put empty into counter
   if not Connection() then exit doDisconnect
   try
      revCloseDatabase gConID
   catch theError
      answer warning theError
      exit doDisconnect
   end try
   put 0 into gConID
   put empty into field  status
   answer info "Connection Terminated "
end doDisconnect  ------------------------------------------------------

on doCreate -----------------------------------------------------------
   if not Connection() then exit doCreate
   try
      put "CREATE TABLE users(userID integer primary key autoincrement, name text)" into tSQL
      revExecuteSQL gConID, tSQL
      put the result into temp
      if the result is not 0 then
         answer warning the result
         exit  doCreate
      end if
   catch theError
      answer warning theError
      exit doCreate
   end try
   answer information "Table 'Users' Created Sussessfully"
end doCreate --------------------------------------------------------

on doDeleteTable ---------------------------------------------------------
     if not Connection() then exit doDeleteTable
     try
          put "DROP TABLE users;" into tSQL
          revExecuteSQL gConID, tSQL
          put the result into temp
          if the result is not 0 then
               answer warning the result
               exit  doDeleteTable
          end if
     catch theError
          answer warning theError
          exit doDeleteTable
     end try
     put empty into field  status
     answer information "Table 'Users' Deleted Sussessfully"
end doDeleteTable --------------------------------------------------------

on doPopulate ------------------------------------------------------
   if not Connection() then exit doPopulate
   try
      repeat with ii = 1 to 30
         put empty into temp
         repeat with jj = 1 to 20  -- create 20 random characters
            put temp &  numtochar(randomNum(97,122) ) into temp
         end repeat
         put "INSERT INTO users (name) values ('"  & temp & "')" into tSQL
         revExecuteSQL gConID, tSQL
         if the result is not 1 then
            answer warning the result
            exit  doPopulate
         end if
         put ii into field "counter"
      end repeat
   catch theError
      answer warning theError
      exit doPopulate
   end try
   answer information "Random Data Added to Table 'Users'"
end doPopulate ------------------------------------------------------

on doRead ----------------------------------------------------------
   if not Connection() then exit doRead
   try
      put "SELECT ID,TITLE FROM products LIMIT 100" into tSQL
      put revdb_querylist(,,gConID,tSQL) into ptext
      put false into firstLineContainsColumnNames
      set the dgText of group "myGrid" to ptext
      put "100" into LimitBegin
   catch theError
      answer warning theError
      exit doRead
   end try
end doRead

on doRead2 ----------------------------------------------------------
   if not Connection() then exit doRead2
   try      
      put LimitBegin + 25 into LimitEnd
      answer "Limit End: " & LimitEnd & "Limit Begin" & LimitBegin
      
      put ("SELECT ID,TITLE FROM products  LIMIT " & LimitBegin &","& LimitEnd) into tSQL
      put LimitEnd into LimitBegin
      answer "Limit End: " & LimitEnd & "Limit Begin" & LimitBegin
      answer tSQL
      //put "SELECT ID,TITLE FROM products  LIMIT " . LimitSetting into tSQL
      put revdb_querylist(,,gConID,tSQL) into ptext
      put false into firstLineContainsColumnNames
      set the dgText of group "myGrid" to ptext
   catch theError
      answer warning theError
      exit doRead2
   end try
end doRead2

on doUpdateRow
     put the dgHilitedLines of group "mygrid" into theLine
     put the dgDataOfLine[theLine] of group "mygrid" into theDataA
     ## theDataA is now an array variable.
     --answer theDataA["id"] && theDataA["Name"]  --for debuging
     -- now we need to update the SQLite DB
     put theDataA["id"] into xID
     put theDataA["Name"] into xName
     if not Connection() then exit doUpdateRow
     try
          put "UPDATE users SET Name='"  & xName &  "' WHERE userID='"  & xID &"'"  into tSQL
          revExecuteSQL gConID, tSQL
          put the result into temp
          if the result is not 1 then
               answer warning the result
               exit  doUpdateRow
          end if
     catch theError
          answer warning theError
          exit doUpdateRow
     end try
     answer information "Table 'Users' Updated Sussessfully"
end doUpdateRow


function Connection ----------------------------------------------
   if gConID is 0 then
      answer info "Press the Connect button first."
      return false
   else
      return true
   end if
end Connection ---------------------------------------------------

function randomNum lowerLimit,upperLimit
   return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
   end randomNum

-- For successful queries, the revExecuteSQL command returns
-- the # of rows affected for INSERT, UPDATE and DELETE statements. 
-- For all other statements, 0 is returned, so if the result is 0... then everything is good
This is my Datagrid script:

Code: Select all

on closefieldeditor
     --doUpdateRow
     --answer information the text of pfieldeditor
     --answer information the dgHilitedLines of group "myGrid" 
     --put the short name of me into theFieldname
     --send refreshline to me
     --     put the dgHilitedLines of group "mygrid" into theLine
     --     put the dgDataOfLine[theLine] of group "mygrid" into theDataA
     --     ## theDataA is now an array variable.
     --     answer theDataA["id"] && theDataA["Name"]
end closefieldeditor

on editvalue
     beep
end editvalue



on mouseDoubleUp pMouseBtnNum
    ## Let Data Grid process mouseDown and select row that was clicked on
    dgMouseDown pMouseBtnNum        
        
    ## Get value of column clicked on. The column name can be accessed in the
    ## dgColumn custom property of the column control (the target).
    put GetDataOfIndex(the dgHilitedIndexes of me, the dgColumn of the target) into theColumnValue
    put theColumnValue into CurrentID
    answer CurrentID
//send (CurrentID && CurrentID) to card "Edit Card"
       go to card "Edit Card" in a new window //as modal
    
    ## Don't pass mouseDown
end mouseDoubleUp
This is the script for my "Edit Card"

Code: Select all

on preOpenCard
      global CurrentID
   set the text of field "Field" to CurrentID
   answer CurrentID
end preOpenCard


/*on OpenCard -- highlight the "go to this card" button in a set
   set the text of field "Field" to CurrentID
   //put CurrentID into field "Field"
   answer "Hello"
   answer CurrentID
   answer gConID
  pass OpenCard
end OpenCard*/

on closeCard -- record date and time the card was last viewed
  -- in a custom property of the card
  set the lastAccessDate of the target to the seconds
  pass closeCard
end closeCard

Re: Send variable from one card to another card?

Posted: Sun May 29, 2011 12:43 pm
by Klaus
Hi jesse,

as I mentioned, you need to declare a global variable in EVERY script you use it!
So you need to add this to your DataGrid script to make it work:

Code: Select all

on mouseDoubleUp pMouseBtnNum

   ## !!!
   GLOBAL CurrentID
   ## !!!

    ## Let Data Grid process mouseDown and select row that was clicked on
    dgMouseDown pMouseBtnNum        
        
    ## Get value of column clicked on. The column name can be accessed in the
    ## dgColumn custom property of the column control (the target).
    put GetDataOfIndex(the dgHilitedIndexes of me, the dgColumn of the target) into theColumnValue
    put theColumnValue into CurrentID
    answer CurrentID
    ## send (CurrentID && CurrentID) to card "Edit Card"

   ## !!!
   ## You cannot go to another card of the SAME stack in another window!
    go to card "Edit Card"
   ## in a new window //as modal
    
    ## Don't pass mouseDown
end mouseDoubleUp
Best

Klaus

Re: Send variable from one card to another card?

Posted: Mon May 30, 2011 12:07 am
by jesse
Klaus,

Thanks that does work. You mention I can't go to another card in the same stack in a new window.
How would I send information from the Datagrid to an "Edit Card" in a new window? ?Is this even
possible?

Do you know of any examples showing a Datagrid with an editing window?