[RESOLVED] [2008] Problem closing/opening forms
Hi,
I have Form1 which contains a button which opens Form2. Form2 contains a datagrid. I have coded it, so that when an item is double-clicked, it opens the database record which is shown on Form1.
(Form1 is the database showing names, addresses etc, Form2 is a datagrid that shows everything in the database, allowing one item to be selected and load the details into Form1 - hope this is making sense).
Anyway, what happens is this...
1) Either when double-clicking my grid item, the whole application closes down (because I want to close Form2 after the item is selected)
OR
2) It loads the details into Form1, but opens a new instance of Form1 - thus leaving me with the original Form1 which is blank (which I want to close without closing the whole app), a new populated Form1 (which is correct) and grid in Form2 (which I wanted to close down).
Can anyone help with this please?
The code I am using at the moment is as per item (2) above:
Code:
Dim Form1 As New Form1
MainForm.GetRecordFromGrid(intAccId)
MainForm.ShowDialog()
Me.close
I've tried all sorts of variations form1.close, form1.hide and in different orders - always getting different results but never what I actually want.
Thanks.
Re: [2008] Problem closing/opening forms
Sorry, code typo above... should actually read
vb Code:
Dim Form1 as New Form1
Form1.GetRecordFromGrid(intAccId)
Form1.ShowDialog()
Me.close
Re: [2008] Problem closing/opening forms
you don't need to open a new form1. use the form1 you already have open
remove this line:
+ this line:
Re: [2008] Problem closing/opening forms
Thanks but it doesn't work. I get this error:
Quote:
Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.
I also need to run a procedure contained in Form1, passing the variable from Form2.
If I open a new form, it executes the variable and populates the form's text boxes. If I go to the existing Form1 it passes the variable (confirmed by using msgbox() to see if it's there) but doesn't populate the text boxes.
Re: [2008] Problem closing/opening forms
if it works with a new form, go with that.
just close the first instance of form1 when you open form2, then use a new instance of form1 to display your records
Re: [2008] Problem closing/opening forms
I can't do that, because closing the first instance of Form1 closes the entire application... this is the whole problem.
:(
Re: [2008] Problem closing/opening forms
Quote:
Originally Posted by ShiftySid
Thanks but it doesn't work. I get this error:
I also need to run a procedure contained in Form1, passing the variable from Form2.
If I open a new form, it executes the variable and populates the form's text boxes. If I go to the existing Form1 it passes the variable (confirmed by using msgbox() to see if it's there) but doesn't populate the text boxes.
ok. don't use
as form1 is already open. try it that way then let us know what the problem is with the procedure that doesn't run
Re: [2008] Problem closing/opening forms
I've resolved it.
Code extract for button that calls Form2 is
Code:
Me.Hide()
Dim Form2 As New Form2
Form2.ShowDialog()
Code in Form2 (once item in datagrid is double clicked) closes form2 and calls a new Form1 as follows, then executes the procedure.
Code:
Me.Hide()
Dim Form1 As New Form1
Form1.GetRecordFromGrid(shrAccId)
Form1.ShowDialog()
This seems to work fine. Not sure about the implications of using HIDE however. Presumably, if the app isn't closed down after a certain period of time, the user will end up with several hidden Form1's, hogging system resources?
Anyway, does what I need it to do for now - so that's one for another day.
Cheers all.
Re: [RESOLVED] [2008] Problem closing/opening forms
you can change your projects settings so the application exits when the last form closes, instead of when the startup form closes. so you could close all those hidden forms.
the setting is in Project-->Properties - application tab
Re: [RESOLVED] [2008] Problem closing/opening forms
That was something I tried earlier, but it still closes down the whole application - unless the Project | Properties options don't take effect in debug mode and only work when compiled?
There's only one user of the app, and that's my other half so I shall leave it as it is! :o)
Re: [RESOLVED] [2008] Problem closing/opening forms
Have you also tried out My.Forms.Form1.Show etc?
Usually that way gets rid of the problems you seem to have.
Re: [RESOLVED] [2008] Problem closing/opening forms
Bingo! It does solve the problem - thank you.