Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update the grid from a different form

Status
Not open for further replies.

admdev

Programmer
May 17, 2006
56
0
0
US
Hi all,

Hope somebody can help me with this.

I am trying to update the grid on a form from another form. In other words, say form A has a grid. Form A calls form B, then form B updates the grid on form A.

Any help would be appreciated.

Thanks.
 
There are several ways of doing this, but some may depend on how do you call the form B.

The universal method could be like this
Code:
for each loForm in _screen.Forms
   if lower(loForm.name) = 'myformbname'
      loForm.Mygrid.refresh()
      exit
   endif
next
 
If you pass the FORMA name to FORMB as a parameter and have a FORMB property for the FORMA name (ie. foParentFormName)
you can do

FORMA call to FormB
do form FORMB with THISFORM

FORMB.INIT
lparameter poCallingForm
thisform.foParentForm=poCallingForm

then when you need to update the grid you do this in FORMB
thisform.foParentFormName.GridName.Refresh()



David W. Grewe Dave
 
Thanks guys for the reply!

dgrewe- I was trying what you suggested, but the grid was not updating. Here is what I am doing.

1. Creating a cursor in the Init event of form B.
2. Inserting the data I need from form B into the cursor.
3. Assign the cursor to the grid.RecordSource.
4. Refreshing the grid as you suggested.

No data is displayed.

Thanks.
 
1. Do both forms share the same Data session?

2. It is usually recommended to create one cursor for the grid in form's Load and when you need to re-build the cursor do it using a select into a separate temp cursor, zap grid's cursor and append (or insert) from the temp cursor. This technique is called "safe select".
 
?? 1. Creating a cursor in the Init event of form B. ?? Oh

You left that part out

So you will also have to pass the DataSession number as a parameter from FORMA to FORMB .In FORMB set the DataSession to the same one as FORMA.

So you will need another FORMB.Property and assign it the Parameter value like you did with the Formname.



David W. Grewe Dave
 
dgrewe,

I already tried with sending the data session as a parameter to form B and it works. The problem is that within the same method I have to access the form Bs data session, which I accomplished by setting the data session back to the data session number of the form, right after updating the cursor and refreshing the grid.

After I set the data session back to the Form Bs data session, within the same form B I run a select statement. For some reason, the cursor is not even created.

That is one of the reasons I wanted to avoid changing the data sessions back and forth and that is why I posted the question, hoping that somebody would give me a different solution.

So I guess the question is now, why after changing the data session back to form B itself, this new cursor is not even created?

Thanks to all for the replies! I really appreciate it.

 
If the form B is the child form to form A, you should have Form A to run in Private DS, but form B in Default DS. This way form B will share form's A DS and you avoid all the problems.

Watch out, however, for the way you instantiate your form. If you do it through the Form's mananger, you may have an interesting effect of Form's B Default DS not the Form's A DS but really default DS.
 
I am trying to update the grid on a form from another form. In other words, say form A has a grid. Form A calls form B, then form B updates the grid on form A.

Try this.

****Form B datasession = 1 Default Data session
****Form B init()
lParameters pWcalled,pDatasessionID
with this
.addproperty("whocalled",pwhocalled)
.datasessionid = pdatasessionid
*******
*******
endwith
Now you have total control over form A with
thisform.whocalled.<< all events, properties, Methods, controls etc>>

*** Form A
**** does not matter if Private or Default data session
do form b with thisform,thisform.datasessionid

Do not have any tabels in Form B Dataenvironment, keep them ALL in Form A DE. You cannot keep switching data sessions this will give you unpredictable results.
 
I agree with you Imaginecorp. That is why I am trying to use a different aproach.

Here is how I am doing it.

1. On the Init event of form A, I am creating the cursor which is the RecordSource of the grid on form A.

2. A command button on form A calls form B sending a few parameters. Among those, are the form B datasessionid, the form B itself as an object, and other data fields.

3. Form B is used to select certain fields from three listboxes.

4. The user clicks "Save", the datasession is changed to form A and the selected fields from the listboxes are inserted into the cursor of the form A grid.

5. The form A grid is refreshed.


Is it posible to send a cursor to form B as a parameter? Is this even possible?

How about the cursortoxml? I have not used it, but I read somewhere this would do something similar.

Thanks.


 
I think you're making things far more complex than they should be. Also since you're creating cursor for the grid in form's Init I assume, that you add your grid in run-time after you created the cursor, correct?
 
?? 4. The user clicks "Save", the datasession is changed to form A and the selected fields from the listboxes are inserted into the cursor of the form A grid. ??

OH You did not mention this before. (again)

Before you leave FORMB
I would use and array in FORMB to get the fields the user selected (is.faFldSelect[x])

lnOrgCnt=thisform.poCallingForm.GridName.Columncount
lnClmAdd=(N)WHo Many Fields the user selected from FORMB
thisform.poCallingForm.GridName.Columncount=lnOrgCnt+lcClmCnt
for x=1 to lnClmAdd step 1
thisform.pcCallingform.gridname.column[x+lnOrgCnt].Controlsource=thisform.faFldSelect[x]
thisform.poCallingForm.gridname......
do this for the rest of the properties in FORMA grid to update.
endfor

Anything else you forgot to tell us???


David W. Grewe Dave
 
Sorry guys for not describing everything from the begining, but I wanted to make the question as simple as possible.

Thanks for all the suggestions you guys provided. I really appreciate it.
 
The user clicks "Save", the datasession is changed to form A and the selected fields from the listboxes are inserted into the cursor of the form A grid.

You do not need to do this step. You should be able to insert the fields into the cursor in Form B. Once you do that, do a
thisform.<<parentform variable>>.grid1.refresh
Both forms will share the cursor. Just to verify try a select <<cursorname>> in form B...
Somebody correct me here, but I dont think Select - SQL cursors care about datasessionids and are generally accessable from anywhere...
 
You're wrong. Cursors created in one DS are not visible in another. But I don't see the reason for form B have its own private DS. If the form B is really a child for form A it should share form's A DS. If both forms are trully independant and want to have each its own DS then there are methods of passing cursors between forms. One is described in this FAQ another is to use CursorToXML/XMLToCursor functions.
 
Hello Ilyad;

Sure there are 100 ways to skin a cat... none of which the cat would appreciate though...

The simplest is to create an object with an array and load both forms into it... but then you have to remember to blank it out or clear the object...

When I posted the above, I did not have time to test the universality of cursors, but you are right.

I thought I did say
****Form B datasession = 1 Default Data session

Thanks for the link...But I generally keep away from "expert" opinion... Just a quark I have
 
The FAQ describes a method with an object and arrays (you have the same idea, I presume, so it would just save you time implementing the idea).

Anyway, let's wait for the thread originator should the forms be independant or they are engaged in a relationship <g>
 
Visited Universalthread, I see the usual gang of supects are still around... used to know Mike when he was poor...

Do you really expect somebody who is not an "expert", (my apology to admdv), to follow Srdjan's code, when simple Parameter passing would do?
 
What do you mean by parameter passing? You're basically proposing the same idea, create an object, put cursor in array, then retrieve it in the other form. Or do you mean pass DS ID between forms and try to switch DS using SET DATASESSION command?
 
Hello Ilyad;

Sorry. What I meant was passing the form as a parameter to the child form.
I am sure you know that when one form calls another for further input, the child should be Modal. As you do not want the user to perform any actions on the parent while the child is open.
To perform some action on the parent and child, you could create an object on the fly with an array, send both forms to it, but here you have to remember to blank and/or remove the object.
A simpler way would be to pass the parent form to the child and if needed pass the child back to the parent. For this both forms must share the same data session. The child should have a default data session. If both forms were Private, any changes made in the child would cause a conflict when the parent, sharing the same table, is saved. This would be true as well if a cursor made the changes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top