Normal Topic Saerching the zero Items in a subform (Read 891 times)
Amor
Full Member
Members
***
Offline


No personal text

Posts: 366
Location: Germany
Joined: Feb 7th, 2004
Saerching the zero Items in a subform
Jan 3rd, 2012 at 10:23am
Print Post Print Post  
Hello!
I would like to search internally the parent form records with no subform items (with zero items).

And from a external form how to use @XResultSetSearch()   to select that.

thank you for the help
  

Dr. med. Amor Belhareth&&Medizin Labor &&Germany
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Saerching the zero Items in a subform
Reply #1 - Jan 9th, 2012 at 9:39pm
Print Post Print Post  
Hello Amor,

Are you using Natural or Relational linking?

-Ray
  

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


No personal text

Posts: 366
Location: Germany
Joined: Feb 7th, 2004
Re: Saerching the zero Items in a subform
Reply #2 - Jan 9th, 2012 at 10:13pm
Print Post Print Post  
Hello Ray!
i´m using a natural linking.
  

Dr. med. Amor Belhareth&&Medizin Labor &&Germany
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Saerching the zero Items in a subform
Reply #3 - Jan 11th, 2012 at 6:01pm
Print Post Print Post  
If you are checking from a form, you can use @FormResultSetTotal. If you are checking from an XResultSet, you may be able to use @XResultSetValue and use the sub-form's field as the argument. If there are no children, it should return a string with the value -1.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Saerching the zero Items in a subform
Reply #4 - Jan 11th, 2012 at 6:22pm
Print Post Print Post  
Hello Amor,

The following little chunk of code would be run from the Main Menu. It gets all the parent records in an application and then loops through checking to see if they have any children. If it finds children, it removes that record from the XResultSet(). If it does not find children it advances to the next record and checks it. Once it checks all records, it opens the appropriate form and displays just the parent records that have no children.

You will need to update Form, Database and Field names to match your application.

Code
Select All
Var vParentRS as Int
Var vChildRS as Int
Var vLoop as Int
Var vCnt as Int
Var vRemoved as Int

	vParentRS = @XResultSetSearch(@FN, "GEM TYPES", 0, 2, "!GEM_TYPE=/=")
	If vParentRS >= 0 Then
	{
		vRemoved = 0
		vLoop = 1
		vCnt = @XResultSetTotal(vParentRS)
		While vLoop <= (vCnt - vRemoved)
		{
			XResultSetCurrentPosition(vParentRS, vLoop)
			vChildRS = @XResultSetSubSet(vParentRS, "Gems")
			If @XResultSetTotal(vChildRS) > 0 Then
			{
				//Has Children so remove this record
				XResultSetRemoveRecord(vParentRS)
				vRemoved = vRemoved + 1

			}
			Else
			{
				//No Children so keep this record in the Resultset
				vLoop = vLoop + 1
			}
			XResultSetClose(vChildRs)
		}
		XResultSetOpenForm(vParentRS, "Gem Types")
		XResultSetClose(vParentRS)
	} 




-Ray
  

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


No personal text

Posts: 366
Location: Germany
Joined: Feb 7th, 2004
Re: Saerching the zero Items in a subform
Reply #5 - Jan 11th, 2012 at 8:08pm
Print Post Print Post  
Thank you very much Ray. It work fine and fast.
  

Dr. med. Amor Belhareth&&Medizin Labor &&Germany
Back to top
 
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Saerching the zero Items in a subform
Reply #6 - Jan 12th, 2012 at 1:53pm
Print Post Print Post  
I would like to add that you should also include the following code to the end of Ray's code.

If you don't, you will discover some strange behavior with the record counter, record navigation, and may even cause you to create duplicate records by mistake. Adding the following code will get the counter and result set back in sync.

Code
Select All
FormResultSetCurrentPosition(@Layout, @FormResultSetTotal(@Layout))
FormResultSetCurrentPosition(@Layout, 1) 



I know that it seems silly to have two FormResultSetCurrentPosition commands in a row, but that's what it takes get things in sync.

Here are the various results that I found:

// Works partially...
// Does NOT work well with navigation buttons that change their visibility based on result set position
ResultSetCurrentPosition(@ResultSetTotal())
ResultSetCurrentPosition(1)


// Does NOT work by itself if there is just one record found
FormResultSetCurrentPosition(@Layout, @FormResultSetTotal(@Layout))


// This works in all cases - shows first record
FormResultSetCurrentPosition(@Layout, @FormResultSetTotal(@Layout))
FormResultSetCurrentPosition(@Layout, 1)


// This works in all cases - shows first record
FormResultSetCurrentPosition(@Layout, 1)
FormResultSetCurrentPosition(@Layout, 1)


// This works in all cases - shows last record
FormResultSetCurrentPosition(@Layout, @FormResultSetTotal(@Layout))
FormResultSetCurrentPosition(@Layout, @FormResultSetTotal(@Layout))

  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Saerching the zero Items in a subform
Reply #7 - Jan 12th, 2012 at 3:10pm
Print Post Print Post  
Ah Carl you are right. I believe you can do this in one line by adding
XResultSetCurrentPosition(vParentRS, 1)
to the code right before the form is opened.

Code
Select All
Var vParentRS as Int
Var vChildRS as Int
Var vLoop as Int
Var vCnt as Int
Var vRemoved as Int

	vParentRS = @XResultSetSearch(@FN, "GEM TYPES", 0, 2, "!GEM_TYPE=/=")
	If vParentRS >= 0 Then
	{
		vRemoved = 0
		vLoop = 1
		vCnt = @XResultSetTotal(vParentRS)
		While vLoop <= (vCnt - vRemoved)
		{
			XResultSetCurrentPosition(vParentRS, vLoop)
			vChildRS = @XResultSetSubSet(vParentRS, "Gems")
			If @XResultSetTotal(vChildRS) > 0 Then
			{
				//Has Children so remove this record
				XResultSetRemoveRecord(vParentRS)
				vRemoved = vRemoved + 1

			}
			Else
			{
				//No Children so keep this record in the Resultset
				vLoop = vLoop + 1
			}
			XResultSetClose(vChildRs)
		}
		XResultSetCurrentPosition(vParentRS, 1)
		XResultSetOpenForm(vParentRS, "Gem Types")
		XResultSetClose(vParentRS)
	}
 



-Ray
  

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



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Saerching the zero Items in a subform
Reply #8 - Jan 12th, 2012 at 4:13pm
Print Post Print Post  
Ray,

Excellent. That is more elegant, resolves the issues I was seeing, and works perfectly in all cases that I tested.

One more thing I would add, is a conditional just before opening the form, to check that there is actually a parent record with zero subrecords, and a message if there are none found. This just reduces the confusion of leaving a user looking at a blank form in update mode.

Code
Select All
Var vParentRS as Int
Var vChildRS as Int
Var vLoop as Int
Var vCnt as Int
Var vRemoved as Int

	vParentRS = @XResultSetSearch(@FN, "GEM TYPES", 0, 2, "!GEM_TYPE=/=")
	If vParentRS >= 0 Then
	{
		vRemoved = 0
		vLoop = 1
		vCnt = @XResultSetTotal(vParentRS)
		While vLoop <= (vCnt - vRemoved)
		{
			XResultSetCurrentPosition(vParentRS, vLoop)
			vChildRS = @XResultSetSubSet(vParentRS, "Gems")
			If @XResultSetTotal(vChildRS) > 0 Then
			{
				//Has Children so remove this record
				XResultSetRemoveRecord(vParentRS)
				vRemoved = vRemoved + 1

			}
			Else
			{
				//No Children so keep this record in the Resultset
				vLoop = vLoop + 1
			}
			XResultSetClose(vChildRs)
		}
		XResultSetCurrentPosition(vParentRS, 1)

		If vLoop > 1
			XResultSetOpenForm(vParentRS, "Gem Types")
		Else
			@MsgBox("No records found that have zero subrecords.", "", "")

		XResultSetClose(vParentRS)
	}
 



  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged