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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move a sub form from one main form to another in code

Status
Not open for further replies.

Larsson

Programmer
Jan 31, 2002
140
SE
Hi!
Short question:
How to move the reference for a sub form from one main form to another?

Long question:
I have a main form with a sub form. The sub form contains links to other sub forms. When the user clicks on a link the sub form is unloaded and the new sub form is loaded in its place. This works perfect.
But the problem is that this is very similar to a website, so now the users want to have a history function also. They want to have the sub forms remember their state between going to different sub forms.

Example:
Sub form Orders contains a list of orders for a customer.
Sub form OrderDetails contains the details for an order.
The user can go to OrderDetails from Orders (then Orders get unloaded and OrderDetails get loaded in its place). When the user has finished looking at the details he/she moves back to the Orders (OrderDetails get unloaded and Orders get loaded).
Then the sub form Orders shall show the same state as before it was unloaded.

I want to do this by not unloading the sub form, but to change its reference to another main form (a hidden one) and then when I want to use it again, just change the reference back to the original main form.

I have tried this with this code:
Code:
Dim frm As Form, frmSaved As Form
Set frm = Me.Parent

If (Not IsLoaded("SavedForm_f")) Then
     DoCmd.OpenForm " SavedForm _f"
End If
Set frmSaved = Forms("SavedForm _f")

frmSaved! SavedForm.SourceObject = Me.Parent.SubForm_f.SourceObject
Me.Parent. SubForm _f.SourceObject = "OrderDetails_f"

But that somehow makes a copy of the original sub form. I have checked this by displaying a message when the OnLoad-event fires, and it does that after this line: frmSaved! SavedForm.SourceObject = Me.Parent.SubForm_f.SourceObject


Does anyone have a suggestion for how I shall solve this?


Ps. By changing sub form I can simulate a website and the users don’t have to bother with a lots of forms that opens up all the time, the interface is more slim this way.
 
What do you mean by state? Is that just the current record? If it is I think I would simply use a global variable. Then before changing subforms get the current bookmark and save to the global variable. Reload the subform and set the bookmark equal to global variable.
 

Why not simply hide the subforms that are not currently in use by setting the visible property?


Randy
 
By state I mean the current record and some other stuff.
I have tried to save the bookmark in a global variable and then assign it again when I reload the form.
But that doesn't work, if I do that I get the following error:
Not a valid bookmark (3159).
It seems as Access doesn't like it when I try to save a bookmark for later use.


I can't hide the subform because I use the same subfrom control to load all subforms in. In other words, I don't have a control per subform, one for all.
So I can't set is as not visible, I need to move the subform to another control without losing the state.
 
I have solved it now, but not as I tried to do.

I have given up on trying to move the subform. Instead I save the subforms state (current record, filter and some other stuff) in a class that is global.
I save the state when the user moves away from the subform and load it again when the user gets back to the subform.

To save the current record I save the key for the table, not the bookmark. This way I can use Recordset.FindFirst() to move to the same record again when I'm restoring the state.
 

Another possibility.... have you considered using a tab control and putting each subform on different tabs?


Randy
 
Larsson,
Your approach was basically what I was thinking by using globals to save the "state" variables, but I did not mean to say bookmark. I should have said absolute position or unique key. If you open a form, save a book mark, close the form, and then reopen it you get a whole new set of bookmarks thus you will get the not valid book mark error.

Help menu:
Note Bookmarks are not saved with the records they represent and are only valid while the form is open. They are re-created by Microsoft Access each time a bound form is opened.

Sorry for the confusion.
 
Randy700,
The subform is already in a tabcontrol, so I don't want to have another tabcontroll inside the tabcontroll. I lose way to much space and the links are a more elegant solution then tabs ;-)

MajP,
My first thought was to save the state, but didn't know how to do with the bookmark, that's why I tried to just move the reference of the form, like you can do in a modern language, but apparently not in VBA.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top