Normal Topic XResultSetDeleteRecord() not working (Read 1938 times)
actiontech
Full Member
***
Offline



Posts: 173
Joined: Apr 10th, 2016
XResultSetDeleteRecord() not working
Apr 1st, 2018 at 3:21pm
Print Post Print Post  
Hello, I'm trying to use XResultSetDeleteRecord() to permanently delete a subrecord from a database. Here is my code:

Code
Select All
var vCust as String
var vRS as Int
var vFN as String
var vTest as String

vFN = Pictures_List
WriteLn("Pictures_List = " + vFN)
vRS = @XResultSetSearch(@FN, "ACTION!Pictures", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!Filename0=" + vFN)
WriteLn("XResultSet = " + vRS)
WriteLn("XResultSet Total = " + @XResultSetTotal(vRS))
vTest = @XResultSetValue(vRS, "Filename0")
WriteLn("XResultSet Value = " + vTest)
XResultSetDeleteRecord(vRS)
WriteLn("XResultSet Total = " + @XResultSetTotal(vRS))
XResultSetClose(vRS)
 



Sesame Slate returns:

Code
Select All
Pictures_List = Flower Vase.jpg
XResultSet = 0
XResultSet Total = 1
XResultSet Value = Flower Vase.jpg
XResultSet Total = 1
 



The last XResultSet Total seems to indicate that the subrecord wasn't deleted.

After the program runs, the subform still shows the subrecord on the active form. When opening the subform from the applications menu, it still also shows that all subrecords are present and none have been deleted.
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: XResultSetDeleteRecord() not working
Reply #1 - Apr 2nd, 2018 at 2:09pm
Print Post Print Post  
You should NEVER use the XResultSet family of commands to manipulate a record that you currently have open in a form. Viewing is okay but not changing or deleting! The X stands for External, and generally you do not want or need to use this family of commands from within the same database.

In this case, there are protections in place inside of Sesame that are preventing you from deleting that record because you are currently displaying it.

You will want to use FormDeleteRecord() to delete your subform record.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
actiontech
Full Member
***
Offline



Posts: 173
Joined: Apr 10th, 2016
Re: XResultSetDeleteRecord() not working
Reply #2 - Apr 2nd, 2018 at 2:54pm
Print Post Print Post  
Alright thanks. I saw that I could use FormDeleteRecord, but I wanted to see if it was possible to use XResultSetDeleteRecord because it's much easier to pick the exact record you want to delete, where as with FormDeleteRecord you must make sure that the current record displayed is the one you want to delete, and can't use any search criteria to bring it up rather than relative position. Whereas with XResultSetDeleteRecord you can use your handle to search and find the exact record you want to delete.
  
Back to top
 
IP Logged
 
actiontech
Full Member
***
Offline



Posts: 173
Joined: Apr 10th, 2016
Re: XResultSetDeleteRecord() not working
Reply #3 - Apr 2nd, 2018 at 3:16pm
Print Post Print Post  
Working Code:

Code
Select All
#include "sbasic_include.sbas"

var vName as String
var vUser as String
var vFolder as String
var vList as String
var vFile as String
var vCust as String
var vRS as Int
var vFN as String
var vRec as Int

If @Mode() = 1
{
	If @Len(Record_ID) > 1 Then
	{
		vName = Record_ID
		vUser = @GetLocalEnvVar("USERPROFILE")
		vFolder = vUser + "\Google Drive\AAction Photos\" + vName
		vList = @LocalListDirectory(vFolder)
		vFile = vFolder + "\" + Pictures_List

		//Retrieves picture filename in the proper format with directory.

		If @DirectoryExists(vFolder)
		{
			If FileExists(vFile)
			{
				If @ASKUSER("Are you sure?","","")
				{
					FileDelete(vFile)

					vFN = Pictures_List
					vRS = @XResultSetSearch(@FN, "ACTION!Pictures", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!Filename0=" + vFN)
					vRec = @XResultSetValue(vRS, "Record00")
					XResultSetClose(vRS)

					//Extracts record number.

					WarningLevel(0)
					FormDeleteRecord("ACTION!Pictures", vRec)
					WarningLevel(1)

					//Deletes subrecord without warning.

					SubformVisibility("LE3", 0)

					//Clears subform.
				}
			}
			Else
			{
				@Msgbox("File does not exist.","","")
			}
		}
		Else
		{
			@Msgbox("Folder does not exist.","","")
		}

		vList = @LocalListDirectory(vFolder)
		PopulateListelement(Pictures_List, vList)

		//Refreshes picture list.
	}
}
 

  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: XResultSetDeleteRecord() not working
Reply #4 - Apr 4th, 2018 at 1:32pm
Print Post Print Post  
I am assuming that Record00 is a field that you are filling in with the record number? So Record 2 gets a 2 in that field. If that is the case, your code will most likely delete an incorrect record if the user ever sorts the subform.

If you want to find the position regardless of sorting you'll either want to use a loop and @FormFieldValue() or @FormGetValues() and @FindStringArray().

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
actiontech
Full Member
***
Offline



Posts: 173
Joined: Apr 10th, 2016
Re: XResultSetDeleteRecord() not working
Reply #5 - Apr 5th, 2018 at 1:55pm
Print Post Print Post  
Thanks, I appreciate that as that was a very hard problem to figure out and I'll look into that in the future for future developments. Record00 was initially @ResultSetCurrentPosition() but seeing as that  would be fraught with issues, such as if the user sorted the result set or any number of other reasons like that a record was deleted or something, I decided to do something different with that field. I decided to create my own internal record number. So I use XResultSetValue() to automatically add a number to that record. But it only does it when On Element Entry of the Add Picture button. And since it's a relational database it knows to only create these record numbers for just that main record that it's working with, in this case the Customer. Here's the code I have established for that:

ACTION :: Add_Picture :: On Element Entry

Code
Select All
#include "sbasic_include.sbas"

var vName as String
var vList as String
var vUser as String
var vFolder as String
var vDir as String
var n as Boolean
var vCWD as String
var vFile as String
var vCmd as String
var vShell as String
var vHandle as Int
var vFN as String
var vCount as Int
var vTemp as Int
var vCustomer as String

If @Mode() = 1
{
	If @Len(Record_ID) > 1 Then
	{
		vName = Record_ID
		vUser = @GetLocalEnvVar("USERPROFILE")
		vFolder = vUser + "\Google Drive\AAction Photos\" + vName
		vList = @LocalListDirectory(vFolder)

		If Not (@DirectoryExists(vFolder))
		{
			n = @CreateDirectory(vFolder)

			If (n = True)
			{
				@Msgbox("New folder created.",vFolder,"")
			}
		}

		vCWD = @LocalCWD()
		LocalCWD(vUser + "\Downloads")
		vFile = @LocalFileDialog("Add Picture", "*.{jpg,jpeg,png,tiff,gif}")
		LocalCWD(vCWD)

		If vFile > " "
		{
			vDir = vUser + "\""Google Drive""\""AAction Photos""\" + @CHR(34) + vName + @CHR(34)
			vCmd = "move" + " " + @CHR(34) + vFile + @CHR(34) + " " + vDir
			vShell = @Shell(vCmd)
		}

		vList = @LocalListDirectory(vFolder)
		PopulateListelement(Pictures_List, vList)

		vHandle = @XResultSetNew(@FN, "ACTION!Pictures")
		XResultSetCreateNewRecord(vHandle)
		vFN = @AccessStringArray(@ReverseStringArray(@Replace(vFile, "\", ";")), 1)
		XResultSetValue(vHandle, "Filename0", vFN)
		vCount = @CountStringArray(@LocalListDirectory(vFolder))
		XResultSetValue(vHandle, "Record00", vCount)
		vTemp = @XResultSetValue(vHandle, "Record00")
		vCustomer = Record_ID
		XResultSetValue(vHandle, "Customer_ID", vCustomer)
		XResultSetClose(vHandle)

		FormCommit("ACTION")
	}
}
 



It's important to note that the Pictures element of this and the "Descriptions" element of this is completely seperate. The "Pictures" subform actually does not house any pictures like one might think, like having an image box or something. All it houses is the record number field (Record00), a filename field (so it's linked to the Filename of the picture that's selected in Pictures_List), and a Description field. And the whole point of the whole subform is to just have a text editor Description box for each picture that is in a way linked to it, and pulls up automatically when you click the picture and then disappears when no picture is selected, and saves automatically and all that.

The other code heavy part of this system is:

ACTION :: Pictures_List :: On Element Immediate Change (This is the List Element that populates the Pictures directory; the folder in Google Drive that syncs between Server and Client computers.)

Code
Select All
#include "sbasic_include.sbas"

var vName as String
var vUser as String
var vFolder as String
var vFile as String
var vFN as String
var vCust as String
var vHandle as Int
var vRec as Int
var vPos as Int
var vHandle2 as Int
var vLdSpec as Int
var vStSpec as Int

If @Mode() = 1
{
	If @Len(Record_ID) > 1 Then
	{
		vName = Record_ID
		vUser = @GetServerEnvVar("USERPROFILE")
		vFolder = vUser + "\Google Drive\AAction Photos\" + vName
		vFile = vFolder + "\" + Pictures_List
		SetAltImagePath(Picture_Preview_Box, vFolder)
		Picture_Preview_Box = vFile

		//Displays a variable picture at a variable image path in an image box.

		vFN = Pictures_List
		vHandle = @XResultSetSearch(@FN, "ACTION!Pictures", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!Filename0=" + vFN)
		vRec = @XResultSetValue(vHandle, "Record00")
		XResultSetClose(vHandle)

		//Extracts record number.

		vCust = Record_ID
		vHandle2 = @XResultSetSearch(@FN, "ACTION!Pictures", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!Customer_ID=" + vCust)
		XResultSetSort(vHandle2, "Record00:-1")
		XResultSetCurrentPosition(vHandle2, vRec)
		vPos = @XResultSetCurrentPosition(vHandle2)
		XResultSetClose(vHandle2)

		//Looks up the current position of that particular record from a full result set of that customer's subrecords
		//and stores it in a variable.

		FormResultSetCurrentPosition("ACTION!Pictures", vPos)

		//Goes to the position of the stored variable, for the current result set, of the "Pictures" subrecords, for the
		//current customer.

		If @IsBlank(Pictures_List)
		{
			SubformVisibility("LE3", 0)
		}
		Else
		{
			SubformVisibility("LE3", 1)
		}
	}
}
 



I also forgot to mention that another function of the description field is to be automatically inserted into an email when I click another button On Element Entry, that would be the Attach button, then there is a Send button to Send Email with those attachments and those descriptions inserted into the Email.

It's really cool that Sesame has these types of Email functions but I almost wish it had more than just sending and counting emails because then you could almost just use Sesame as your Email client and there would be no need for an external email client.
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: XResultSetDeleteRecord() not working
Reply #6 - Apr 5th, 2018 at 2:27pm
Print Post Print Post  
actiontech wrote on Apr 5th, 2018 at 1:55pm:
It's really cool that Sesame has these types of Email functions but I almost wish it had more than just sending and counting emails because then you could almost just use Sesame as your Email client and there would be no need for an external email client.


There is @GetEmail() for getting emails but Sesame will not replace an email client.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
actiontech
Full Member
***
Offline



Posts: 173
Joined: Apr 10th, 2016
Re: XResultSetDeleteRecord() not working
Reply #7 - Apr 5th, 2018 at 5:33pm
Print Post Print Post  
I forgot to mention I should've removed those last two variables from ACTION :: Pictures_List :: On Element Immediate Change

Code
Select All
var vLdSpec as Int
var vStSpec as Int
 



But I was initially going to use those with XResultSetSort() to make sure that the result set was in the right order but then I figured out that I didn't have to. That's why those two variables aren't used in the code above.
  
Back to top
 
IP Logged