Saving user modifications to Table Data

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving user modifications to Table Data

Post by Klaus » Wed May 14, 2014 9:23 pm

Hi Tom,

maybe declaring tFile as a local variable will help? :D

Code: Select all

local tFile

on preOpenCard
   ...
   put url("file:tAgent.txt") into tfile
end preOpenCard

on opencard
...
Best

Klaus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Saving user modifications to Table Data

Post by Simon » Wed May 14, 2014 10:34 pm

Hi Tom,
First glance:
You haven't defined "tfile" as a local variable, it gets lost after preOpenCard.

While not really a problem, in the on mouseUp you don't have to put the field into a variable first, just;
put fld "Aname" into line 1 of tfile

Simon
Edit; I see Klaus beat me to it :)
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Saving user modifications to Table Data

Post by trags3 » Thu May 15, 2014 10:04 pm

Hi Klaus & Simon,
Thank you so much. I have been checking for a response to my post and overlooked the fact that mine was the last post on a page, so I didn't see it right away. I won't let that happen again.
Looks like you both nailed it (as usual). It doesn't quite work the way I want it to regarding opening the app vs going to the page to edit and save the data.
I think I can work out those problems but I will let you know.

Thanks again for all your help.

Tom

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Saving user modifications to Table Data

Post by trags3 » Fri May 16, 2014 3:24 pm

Simon wrote:Hi Tom,
First glance:
You haven't defined "tfile" as a local variable, it gets lost after preOpenCard.

While not really a problem, in the on mouseUp you don't have to put the field into a variable first, just;
put fld "Aname" into line 1 of tfile

Simon
Edit; I see Klaus beat me to it :)
Simon & Klaus
It all seems to work except when I close the app and then reopen it The fields contain the data that was in them when the app was compiled.
I can change it and work with it but next time I use the app I need to reenter all the data. This tells me that the data is not being saved.
Here is all of the code I am now using:

Global tfile
on preOpenCard
--check to make sure your folder exists
if there is not a directory (specialFolderPath("documents") & "/sellnet") then
create folder (specialFolderPath("documents" & "/sellnet"))
else
end if
set the defaultFolder to (specialFolderPath("documents") & "/sellnet")
--load your Preference file into memory
put url("file:tAgent.txt") into tfile
end preOpenCard

on openCard
-- this loads the saved data into the fields on card 5 which are used later
-- it is done when the app begins or when the user send the program here to edit the data

put line 1 of tfile into fld "Aname" of card 5
put line 2 of tfile into fld "Aoffice"of card 5
put line 3 of tfile into fld "Acell"of card 5
put line 4 of tfile into fld "officep"of card 5
put line 5 of tfile into fld "Aemail"of card 5

-- Fld begin is = "T" on app startup
if fld "begin" of card 1 ="T"
then
put "F" into fld "begin" of card 1
go card 1
end if
--if the user directed the program here then stay on the card to edit data and save with the finish button
end openCard

The finish Button code:

global tfile
on mouseUp
--specify a path
set the defaultFolder to (specialFolderPath("documents") & "/sellnet/")
put fld "Aname" & cr & fld "Aoffice" & cr & fld "acell" & cr & fld"Officep" & cr & fld"Aemail" & cr into tfile
--save the updated file to disk
put tfile into url("file:" & (specialFolderPath("documents") & "/sellnet/tagent.txt"))
end mouseUp
I can't seem to get it.
What appears to be happening is that the data loaded in the openCard handler is not from a saved file.If the fields are edited then the data loaded anytime while the app is running is correct.
When the app is exited then everything reverts to the original data.

Thanks for your help.

Tom

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving user modifications to Table Data

Post by Klaus » Fri May 16, 2014 5:19 pm

Hi Tom,

the syntax looks OK.

Here a script with some ANSWERs in it (and some minor corrections), maybe this helps to find the culprit!

Code: Select all

global tfile
on preOpenCard
   
   ## Save some typing:
   put specialFolderPath("documents") & "/sellnet" into tFolder
   put tFolder & "/tAgent.txt" into tFilePath
   
   if there is not a folder tFolder then
      create folder tFolder
      ## No else case? Then no ELSE necceessary!
      ##else
   end if
   
   ## ALWAY check for exitsence of files before you try to access them!
   if there is a file tFilePath then
      put url("file:" & tFtFilePath) into tfile
      
      ## Check for correct content!
      ANSWER tFile
   else
      ANSWER "File not present (yet)!"
   end if
end preOpenCard

on openCard
   -- this loads the saved data into the fields on card 5 which are used later
   -- it is done when the app begins or when the user send the program here to edit the data
   
   put line 1 of tfile into fld "Aname" of card 5
   put line 2 of tfile into fld "Aoffice"of card 5
   put line 3 of tfile into fld "Acell"of card 5
   put line 4 of tfile into fld "officep"of card 5
   put line 5 of tfile into fld "Aemail"of card 5
   
   -- Fld begin is = "T" on app startup
   if fld "begin" of card 1 ="T" then
      put "F" into fld "begin" of card 1
      go card 1
   end if 
   --if the user directed the program here then stay on the card to edit data and save with the finish button
end openCard

############################################################################
global tFile

on mouseUp
   ## Not neccessary, since you use an absolute path later!
   ## set the defaultFolder to (specialFolderPath("documents") & "/sellnet/")
   put fld "Aname" & cr & fld "Aoffice" & cr & fld "acell" & cr & fld"Officep" & cr & fld"Aemail" & cr into tfile
   --save the updated file to disk
   put tfile into url("file:" & (specialFolderPath("documents") & "/sellnet/tagent.txt"))
   
   ## THE RESULT must be empty on success!
   ANSWER THE RESULT
end mouseUp
Maybe you can post your stack if that does not help?

Best

Klaus

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving user modifications to Table Data

Post by Klaus » Fri May 16, 2014 5:21 pm

Quick question: Is this on the desktop or mobile platform?

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Saving user modifications to Table Data

Post by trags3 » Fri May 16, 2014 5:24 pm

Klaus wrote:Quick question: Is this on the desktop or mobile platform?
Android

Tom

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving user modifications to Table Data

Post by Klaus » Fri May 16, 2014 5:30 pm

Ah, thamks.

Hm iOS file system is case sensitive, is Android, too?
If yes, then tAgent.txt <> tagent.txt! and should be corrected:
...
put tFolder & "/tAgent.txt" into tFilePath
...
put tfile into url("file:" & (specialFolderPath("documents") & "/sellnet/tagent.txt"))
...

To avoid this mess, I ALWAYS use lowercase for file- and foldernames on mobile!

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Saving user modifications to Table Data

Post by trags3 » Fri May 16, 2014 5:44 pm

Klaus wrote:Ah, thamks.

Hm iOS file system is case sensitive, is Android, too?
If yes, then tAgent.txt <> tagent.txt! and should be corrected:
...
put tFolder & "/tAgent.txt" into tFilePath
...
put tfile into url("file:" & (specialFolderPath("documents") & "/sellnet/tagent.txt"))
...

To avoid this mess, I ALWAYS use lowercase for file- and foldernames on mobile!
I have all folder and file names in lower case now.
I have put your code into the preOpencard handler.
On first time through the answer is that the file does not exist yet.
After editing the data
the answer comes back with a black bar and the OK button.
This seems to me that the file exists but os empty?

Tom

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving user modifications to Table Data

Post by Klaus » Fri May 16, 2014 6:00 pm

Hi Tom,

did you change this:

Code: Select all

...
put tFolder & "/tAgent.txt" into tFilePath
...
to this:

Code: Select all

...
put tFolder & "/tagent.txt" into tFilePath
...
?


Best

Klaus

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Saving user modifications to Table Data

Post by trags3 » Fri May 16, 2014 6:03 pm

Klaus wrote:Hi Tom,

did you change this:

Code: Select all

...
put tFolder & "/tAgent.txt" into tFilePath
...
to this:

Code: Select all

...
put tFolder & "/tagent.txt" into tFilePath
...
?


Best



Klaus

Klaus, Yes I did.
I also disconnect the phone from the computer to test.
Tom

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Saving user modifications to Table Data

Post by Simon » Fri May 16, 2014 6:09 pm

Hi Tom,
Are you reading the text file to see if the info is correct each time you change it? (make sure you close it so it can be written to).

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Saving user modifications to Table Data

Post by trags3 » Fri May 16, 2014 6:14 pm

Simon wrote:Hi Tom,
Are you reading the text file to see if the info is correct each time you change it? (make sure you close it so it can be written to).

Simon
Thanks Simon,
When should I close the file and what command should I use?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving user modifications to Table Data

Post by Klaus » Fri May 16, 2014 6:15 pm

Hi Simon,
Simon wrote:Are you reading the text file to see if the info is correct each time you change it? (make sure you close it so it can be written to).
The "url("file:" xyz) syntax does ecactly this in a one-liner:
...
open file xyz for read
read from file xyz ...
close file xyz...
...
:D


Best

Klaus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Saving user modifications to Table Data

Post by Simon » Fri May 16, 2014 6:19 pm

Hi Tom,
I was just thinking about Desktop for now, so open it as you would any text file.

Hi Klaus,
Yeah I meant it as a secondary check, the reason I use text file is they are easy to open and check.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply