Page 1 of 1
How to get contents of a field into variable
Posted: Wed Jan 04, 2012 12:34 am
by jalz
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
Re: How to get contents of a field into variable
Posted: Wed Jan 04, 2012 1:02 am
by mwieder
Don't overthink it...
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.
Re: How to get contents of a field into variable
Posted: Wed Jan 04, 2012 1:04 am
by mwieder
...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.
Re: How to get contents of a field into variable
Posted: Wed Jan 04, 2012 9:12 am
by jalz
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
Re: How to get contents of a field into variable
Posted: Wed Jan 04, 2012 5:31 pm
by sturgis
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
Re: How to get contents of a field into variable
Posted: Wed Jan 04, 2012 6:21 pm
by mwieder
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
Re: How to get contents of a field into variable
Posted: Wed Jan 04, 2012 9:03 pm
by jalz
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