Normal Topic SelectTreeItem on Main Form for Sub-Form (Read 1183 times)
SpencerWulwick
Senior Member
Members
*****
Offline



Posts: 677
Location: Wilton Manors, Florida
Joined: Jan 16th, 2005
SelectTreeItem on Main Form for Sub-Form
Jan 20th, 2006 at 5:40pm
Print Post Print Post  
Hi -

Is there anything "special" that needs to be done to have a record deleted from a sub-form using a command button?

I created a command button, on the main form, and used the following code in the on-element-entry event.

Code
Select All
var vDelete as Int
vDelete = @SelectTreeItem("Search Update Menu!Record Commands!Delete Record (F3)") 



I highlight a record on the sub-form, press the Command Button and nothing happens.

There's one other thing I might mention but I don't think it makes a difference.  The sub-form is on a tab page.  The reason I don't think it matters is that I tried parenting the command button first to the tab page and then to the main form. Nothing happens in either case.

What am I missing, please?

Thanks!
  

- Spencer

    ** Practice random kindness & senseless acts of beauty!
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: SelectTreeItem on Main Form for Sub-Form
Reply #1 - Jan 20th, 2006 at 8:39pm
Print Post Print Post  
Hello Spencer,

Are you testing the code from Update Mode? If not the code will have no effect, since it references the Search/Update Menu. I copied your code and ran it in Update mode and it asks me if I am sure I want to delete the record when the code is ran.

When Delete Record (F3) is clicked on the menu tree, or run using @SelectTreeItem(), it checks to see which form had focus. In the case you just described, the Main form is going to have focus since the Command Button is on the Main Form. When you click the Command Button the Main form becomes the form with focus. So the current record in the Main form will be deleted when the button is clicked. If you put the command button on the Subform then the @SelectTreeItem() will delete the current record in the subform.

-Ray
  

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



Posts: 677
Location: Wilton Manors, Florida
Joined: Jan 16th, 2005
Re: SelectTreeItem on Main Form for Sub-Form
Reply #2 - Jan 20th, 2006 at 9:50pm
Print Post Print Post  
Ray -

I appreciate your feedback.

To answer your question, I did run it from update mode.  I think the problem may have been that I copied the code from the PDF file and it may have had a carriage return that didn't belong.  Although I am almost certain that I had removed the carriage return (as you can see it wasn't in the code I copied to my message) it wasn't working.  In any event, the select tree item is now working.  As you said, the focus was "thrown" and so it erroneously deletes the master record.

I had tried playing with "where" I placed the command button and - even with your answer and explanation (which is very clear and very helpful) - I am still having two problems regarding the command button.

1)  If I am in the designer file with the main and sub-forms in view, it doesn't seem to let me parent the command button to the sub-form. 

2)  If I go to the sub-form itself to add the command button, it doesn't appear in table view, which is where I need it.

Any ideas?

Also, if you could find my other thread Subform - On Element Change (posted within the last day or two), I would really appeciate some feedback on the 3 remaining questions in that thread.  This all relates to a project I am working on that I desperately need to "wrap up."

Thanks!
  

- Spencer

    ** Practice random kindness & senseless acts of beauty!
Back to top
IP Logged
 
SpencerWulwick
Senior Member
Members
*****
Offline



Posts: 677
Location: Wilton Manors, Florida
Joined: Jan 16th, 2005
Re: SelectTreeItem on Main Form for Sub-Form
Reply #3 - Jan 21st, 2006 at 5:31am
Print Post Print Post  
Ray -

I think my brain must be "fried."   I saw your answers in the other thread but at this point, I feel so overwhelmed I am having trouble figuring out my own questions - let alone your answers.  lol

BUT, let me tell you what I tried and I don't understand the outcome.

Since I can't see a command box in table view, I added a text LE (Delete) to the subform itself.

I used the following code in the on-element-entry event of the Delete LE.

Code
Select All
var vDelete as Int
vDelete = @SelectTreeItem("Search Update Menu!Record Commands!Delete Record (F3)")  



While viewing the main form with the sub-forms, in table view, in Search/Update mode, if I click on the Delete LE I am asked whether I want to remove the record (which is expected).  If I select "No" programming stops (also as should be expected).

If I select "Yes", however, the appropriate sub-form record is deleted (as expected); HOWEVER, I am then asked the same question over and over.  I think there are about 30 subforms in the database and that's about how many times I am asked the question ... so it seems that the selecttree command is executing for every record in the database. 

Now what am I overlooking?
  

- Spencer

    ** Practice random kindness & senseless acts of beauty!
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: SelectTreeItem on Main Form for Sub-Form
Reply #4 - Jan 21st, 2006 at 5:48am
Print Post Print Post  
Hi Spencer....

I have been away from Sesame for a few weeks.  Glad to see you are still here..... i have a lot of catching up to do.

===========================

Problem is that when the record is deleted, focus lands in the same field (Delete) in the next record.

Change program for user to enter a letter, like X
Change Program to
IF Delete = "X" then { Delete Routine}

That will require manual entry of X to delete the record, else nothing will be deleted.

==========================
I could call, but having Skype issues at the moment.

Happy New Year to all.
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: SelectTreeItem on Main Form for Sub-Form
Reply #5 - Jan 21st, 2006 at 5:50pm
Print Post Print Post  
Bob's solution is the safest solution. But, you might be able to add a ThrowFocus to another LE to prevent the repeated code execution.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
SpencerWulwick
Senior Member
Members
*****
Offline



Posts: 677
Location: Wilton Manors, Florida
Joined: Jan 16th, 2005
Re: SelectTreeItem on Main Form for Sub-Form
Reply #6 - Jan 21st, 2006 at 8:45pm
Print Post Print Post  
Hi -

If someone could help me "size" pictures so it wasn't so large on-display here, I would appreciate it.

In any event here is a picture of a main form and sub-forms.



What I would like to have happen, ideally, is this.  I want to delete sub-form 5, since it is an erroneous "duplicate" entry.  When I delete the record, I want the TIME on the main form to be automatically recalculated.  I would prefer not to have to enter a letter in a special box and I definitely want to avoid a command button on the main form to accomplish this.  I want it all done through programming.

The one exception is that if I could have a button on the sub-form (by each track) that would automatically both delete the record and recalculate the total TIME that would be OK.

I do not want the total recalculated on every form exit because it would then do it whether needed or not.  And, I do not want to wait until the form is exited and re-entered to see the time.  I want to see it the moment the record is deleted.

Am I asking for too much?  Smiley
  

- Spencer

    ** Practice random kindness & senseless acts of beauty!
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: SelectTreeItem on Main Form for Sub-Form
Reply #7 - Jan 22nd, 2006 at 1:53am
Print Post Print Post  
Spencer,

Did adding a ThrowFocus work to stop the multiple code executions?

If not, maybe you could use a GlobalValue to flag when to recalculate. The following code should give you an idea of how to implement it.

Code
Select All
[Code for On Entry to LE named Delete in the subform]
var vDelete as Int
GlobalValue("Recalculate", 1)
vDelete = @SelectTreeItem("Search Update Menu!Record Commands!Delete Record (F3)")
ThrowFocus(Track Title)

[Code for On Entry to LE named Track Title in the subform]
var vLoop as int
var vTime as time
var vCnt as int
if @GlobalValue("Recalculate") = 1
  {
  GlobalValue("Recalculate", 0)
  vCnt = @FormResultSetTotal("Track Details")
  vLoop = 1
  vTime = 0
  while vLoop <= vCnt
    {
    vTime = vTime + @ToTime( @FormFieldValue("Track Details", "Track Length", vLoop) )
    vLoop += 1
    }
  FormFieldValue("MyMainForm", "MyTimeLE", 0, vTime)
  }
 



I have not tested this, but it should be pretty close. It should allow you to click in the "Delete" LE in the subform, which will set the Recalculate flag, delete the current record, and send focus to Track Title. The Track Title code will see that the flag is set, which will reset the flag back to zero, loop through the subform records to total them up, and then post the total in the Time LE on the parent form.

I used what names I could figure out from the image you posted. The rest, you'll need to change to reflect the actual names.

I don't think we can put all of the code into one LE, because you would end up in an endless loop that would delete all of the records. (Well actually, I have an idea how to make it work that way, but it would be more trouble than it's worth.)

If you try this, let me know how you make out.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: SelectTreeItem on Main Form for Sub-Form
Reply #8 - Jan 22nd, 2006 at 2:39am
Print Post Print Post  
Hold on a minute. I just tested it, and it has a problem, but I almost have it fixed. Give me a few more minutes.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: SelectTreeItem on Main Form for Sub-Form
Reply #9 - Jan 22nd, 2006 at 5:17am
Print Post Print Post  
I had to move the @SelectTree command over to the Track Title LE like this:

Code
Select All
[Code for On Entry to LE named Delete in the subform]
GlobalValue("Recalculate", 1)
ThrowFocus(Track Title)

[Code for On Entry to LE named Track Title in the subform]
var vDelete as Int
var vLoop as int
var vTime as time
var vCnt as int
if @GlobalValue("Recalculate") = 1
  {
  GlobalValue("Recalculate", 0)
  vDelete = @SelectTreeItem("Search Update Menu!Record Commands!Delete Record (F3)")
  vCnt = @FormResultSetTotal("Track Details")
  vLoop = 1
  vTime = 0
  while vLoop <= vCnt
    {
    vTime = vTime + @ToTime( @FormFieldValue("Track Details", "Track Length", vLoop) )
    vLoop += 1
    }
  FormFieldValue("MyMainForm", "MyTimeLE", 0, vTime)
  }
 


This works fine the first time you click on the "Delete" LE. But after that, ThrowFocus doesn't trigger the On Element Entry event. This may be a bug with Sesame - the fact that the On Element Entry & On Element Exit events are not executing when focus is moved with ThrowFocus. They do execute if focus is moved with a mouse-click. But, because the events are not working the way that the code expects it to, the GlobalValue may not get reset to zero. Then the code may execute when you click on the Track Title element in another record, deleting that other record.

So, if the On Element Entry event was triggered by the ThrowFocus command, the code above would work perfectly. But, since it is not, I have had to completely rearrainge things. This is the best I could come up with:

Code
Select All
[Delete :: On Element Entry]
if @IsBlank(Delete)
  {
  Delete = "X"
  ThrowFocus(Track Title)
  }

[Track Details (YourSubformName?) :: On Element Exit]
var vDelete as Int
var vLoop as int
var vTime as time
var vCnt as int

if Delete = "X"
  {
  vCnt = @FormResultSetTotal("Track Details")
  vDelete = @SelectTreeItem("Search Update Menu!Record Commands!Delete Record (F3)")
  if vCnt = @FormResultSetTotal("Track Details") then Clear(Delete)
  vCnt = @FormResultSetTotal("Track Details")
  vLoop = 1
  vTime = 0
  while vLoop <= vCnt
    {
    vTime = vTime + @ToTime( @FormFieldValue("Track Details", "Track Length", vLoop) )
    vLoop += 1
    }
  FormFieldValue("MainForm", "MainFormTimeLE", 0, vTime)
  }
 



This works pretty well. The main drawback, is that you need to click in the Delete element, then click in any other subform element (or on another line in the subform). Which means it requires 2 clicks to get the warning message about deleting a record.

One caution: don't answer yes to the question if clicking in the parent form is what brought it up. That may delete the parent form. Other than those two things, it works quite well.

One thing that works better now, is that there will be an "X" on the record that you are being asked whether or not to delete - helping to clarify which line you clicked on. If you answer no to the question, the Delete element is cleared.
  


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