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

Moving from Form to Form

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,559
US

When I have a FormB that I go to from other FormA, I just have simple:

Code:
[green]'Code in FormA[/green]
FormB.Show
Unload Me

[green]'Code in FormB[/green]
FormA.Show
Unload Me
And that gets me between the Forms A and B

But what about if I have a FormX that I need to access from several Forms and then in FormX I need to go back to the Form I came from?

Right now in every Form that accesses FormX I have something like:
Code:
Option Explicit
Public frmGoBackTo As Form

Private Sub cmdReturn_Click()
Me.MousePointer = vbHourglass

frmGoBackTo.Show
frmGoBackTo.Refresh

Me.MousePointer = vbDefault
Unload Me
    
Exit Sub
Code:
With FormX
    Set .frmGoBackTo = Me
    Show
End With
Unload Me

This way works just fine, I just want to know if there is a better way to do it. I am sure there is....

Have fun.

---- Andy
 
Not sure if keeping the reference to .frmGoBackTo will prevent the instance Me from unloading...if there's an ojbect reference than the object is in memory, but maybe that's as you want it to .Show faster.

It might be just as well to use the form name in a string variable. Also, if the forms aren't modal, could another form overwrite the .frmGoBackTo (or strFormGoBackTo)?
--Jim
 

Thank you Jim for replay.

I did have the forms names in a string, but it was way too much to write:
Code:
Select Case strFormGoBackTo
    Case "frmFormOne"
        frmFormOne.Show
    Case "frmFormTwo"
        frmFormTwo.Show
    Case ...
       ...
End Select

Unload Me
and with 15 or 20 Forms to deal with - it was a nighmare. And if I wanted to add another Form, I had to go to the Select case and add them every time.

BTW, these Forms are not modal, but I have just one Form at the time open.

Have fun.

---- Andy
 
>but it was way too much to write

In which case might want to look at my FormByName function from thread222-795815
 

Thanks strongm for this tip, but it looks to me that my OP - although not perfect - gives me the least amount of work (and typing).


Have fun.

---- Andy
 
Why not create buttons that show and hide the forms. Example
Private Sub Generate_Click()
form2.show
form1.hide
End Sub

Would that work?
 

CecilXavier, if you read my OP, I would like to know the best way to access one Form (FormX) from many other Forms in my application.

As an example, if I would have frmConsultant form, this Form would be available from many other Forms (different departments of my organization, and there are many of those). So every time in my app you access frmConsultant, you need to know which Form you accessed it from, and you would be able to go back to the Form you came from. That's it.

And I think for now I'll stick with my original plan.

Have fun.

---- Andy
 
I'm sorry. Cold medicine making me loopy. Read it completely wrong.
 

Don't be sorry, just change the medicine to the one that makes you happy :)

Have fun.

---- Andy
 
As a matter of interest, why are you unloading forms that you know you want to go back to? I understand that for some design reason you only want to have one form showing, but why not just Hide the forms you don't want to see?
 

strongm, you are right.

I do Hide the form since I know I will be back in it.

The example above is just the sample code.

Have fun.

---- Andy
 
In one multiform app I did I used a public numbering and form numbering system for forms in each forms_load.
When I show each form I dont unload the main calling form and it reappears when I unload any the subsequent forms.

Eg In a module
Public FormNumber as integer
Public MyForm as Form
Public MyFormIsOpen(20) as Boolean (optional)

Sub FormA_Load()
Formnumber=1
MyFormIsOpen(1)=true

Sub FormB_Load()
Formnumber=2
MyFormIsOpen(2)=true

Sub FormA_Unload
Set MyForm=Me
MyFormIsOpen(1)=false

Sub FormB_Unload
Set MyForm=Me
MyFormIsOpen(2)=false

and so on

Myform then is always the last used form

If I want to access info on a text box on another form I can refer to any form's controls by number in the module.

Sub GetMyData
Select case FormNumber
case 1
Set MyForm=FormA

case 2
Set MyForm=FormB

End select
End sub

MyData=Myform.Text1.text

End Function

I can use MyformOpen(Formnumber) to see if a form is open if necessary

 
>I used a public numbering and form numbering system

I'm curious. Why would you simply duplicate something that VB essentially already provides via the Form name and the Forms collection?
 
I was trying to treat all the forms as indexed to make access simple by allocating a different number to each form.

Eg I also had a different warning notice frame on all forms and I wanted the warning steered only to the visible form's notice if the warning was generated in a separate common module.

Are you saying that vb forms are effectively indexed anyway?
 
I'm always puzzled by programming that gets lost in Forms anyway. Normally you have a simple hierarchy of loading and unload/hding them anyway. In a few cases you might have multiples (MDI project using a Form per "document") but these should be trackable via an array of Form objects and the Forms collection.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top