How to get contents of a field into variable

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

How to get contents of a field into variable

Post by jalz » Wed Jan 04, 2012 12:34 am

Hi all,

Im very new to LiveCode, ive got a basic sqllite db set up and can insert values that are hard coded from Live Code. My next objective is to figure out how to create forms with lists, radiobuttons, and data entry fields so the data is dynamic.

I've placed a "Text Entry Field" onto my layout and Ive not quite figured out how to get the contents/values of that field into a variable. Ive named the field "Name" and I thought something like the line below would work (Ive gone through different variations)

put the data of field "Name" into tName

any help would be great. Are option menus, combo boxes etc treated differently?

Many thanks

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How to get contents of a field into variable

Post by mwieder » Wed Jan 04, 2012 1:02 am

Don't overthink it...

Code: Select all

put field "Name" into tName 
Are option menus, combo boxes etc treated differently?
Yes. I don't have this in front of me right now, but it's either the label of the option menu or combo box or something similar, as in

Code: Select all

put the label of button "btnOptionMenu" into tSelection 
but you can also get the menuHistory of those controls.
Last edited by mwieder on Wed Jan 04, 2012 1:04 am, edited 1 time in total.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How to get contents of a field into variable

Post by mwieder » Wed Jan 04, 2012 1:04 am

...and if you want to populate a dropdown list dynamically, all you have to do is

Code: Select all

put tList into button "btnOptionMenu"
where tList is a cr-separated list of menu options.

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: How to get contents of a field into variable

Post by jalz » Wed Jan 04, 2012 9:12 am

Hi Guys,

Thanks for replying. Ive tried that but it says no such column: Jalz (Jalz being what I typed into the Name field).

I've got my complete code here below with the insert command - perhaps I'm doing something majorly wrong??? I've now split the name field into FName and SName and will be adding in the other citeria on the form once Ive sussed this bit out....

Many thanks

Code: Select all

   put field "FName" into tFName 
   put field "SName" into tSName
   put "INSERT INTO Customer (FName, SName) "  into tSQL
   put "VALUES (" & tFName & ", " & tSName & ");" after tSQL
   revExecuteSQL conID, tSQL
   if the result is not 1 then
      answer warning the result
      exit  to top
   end if


sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How to get contents of a field into variable

Post by sturgis » Wed Jan 04, 2012 5:31 pm

First name and last name are strings, they need to be quoted.
One way to do this would be to modify tFName and tSName after you fill their values
Something like

put "'" before tFName
put "'" after tFName
## Note its a ' single tick. So say your first name is Mike, you end up with 'Mike' in the tFName variable.
Do the same for surname and it should work. Don't quote numbers of course.
jalz wrote:Hi Guys,

Thanks for replying. Ive tried that but it says no such column: Jalz (Jalz being what I typed into the Name field).

I've got my complete code here below with the insert command - perhaps I'm doing something majorly wrong??? I've now split the name field into FName and SName and will be adding in the other citeria on the form once Ive sussed this bit out....

Many thanks

Code: Select all

   put field "FName" into tFName 
   put field "SName" into tSName
   put "INSERT INTO Customer (FName, SName) "  into tSQL
   put "VALUES (" & tFName & ", " & tSName & ");" after tSQL
   revExecuteSQL conID, tSQL
   if the result is not 1 then
      answer warning the result
      exit  to top
   end if


mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How to get contents of a field into variable

Post by mwieder » Wed Jan 04, 2012 6:21 pm

Or, if you're going to be doing a lot of this, you might want to create a function to surround the names in quotes, ala Ken Ray:

Code: Select all

function q pText
  -- return the text surrounded by single quotes
  return "'" & pText & "'"
end q
and then your line of code could become

Code: Select all

       put "INSERT INTO Customer (" & q("FName") & comma & q("SName" & ") "  into tSQL

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: How to get contents of a field into variable

Post by jalz » Wed Jan 04, 2012 9:03 pm

Hi Guys,

Thanks all for taking the time out - got sturgis' example to work. mwieder thanks for the function, does make the code look more elegant, Ive made it work - but I changed the code you gave me to

Code: Select all

put "VALUES (" & q(tFName) & ", " & q(tSName) & ");" after tSQL
Thanks again

Jalz

Post Reply

Return to “Databases”