Click to See Complete Forum and Search --> : Showing Forms


gknierim
February 28th, 2000, 09:33 AM
I am confused as well as having a problem with .show, .hide, vbmodal, Load and Unload. Heres the situation. My main form (frmMain) is a MDI form. From the menu, I show the SelectParams form(MDIChild is False for all forms except Main) by saying:

private Sub mnuModelParameters_Click()
SelectParams.Show vbModal
End Sub



This window is a search criteria window for the next window it shows. When the user selects his criteria and clicks the OK button, I perform the following code:

private Sub OKButton_Click()
me.Hide
set fModelParams = new ModelParams
fModelParams.Top = 0
fModelParams.Left = 0
fModelParams.Width = 9120
fModelParams.Show vbModal
Unload me
End Sub



This hides(and later unloads) the search window and displays the ModelParams window. However, when the .show gets executed, it goes into the Load procedure of the ModelParams form. In the Load procedure of the ModelParam form, I open the database and search to see if any records exist. The problem is that if there are no records, the form still displays with no records and I do not want that. Below is the code I use in the Load event of that form:

private Sub Form_Load()
'Open database and get records ... blah blah)

If rs.BOF And rs.EOF then
MsgBox "No Model Families were found. Please retry your search criteria again.", vbInformation
rsRecCount = 0
me.Hide
else
LoadList
End If
End Sub



My question is the Me.Hide above is not executing and I have tried an Unload but that doesn't work either. Ideally, I would like to display the first window so that the user can do the search again if there are no records. I am so confused on the sequence of events of show, hide, load, and unload that I'm not sure what to do here. I have been playing with this for a day and cannot get this to work.

I would greatly appreciate any suggestions or explanations. Thanks.

Johnny101
February 28th, 2000, 10:24 AM
I've had this problem as well. What I did to get around it was, in the form that checks for records, I created a public function called Setup or something. In that function, i did my search. If i found records then i called me.show if not, i just exited the function - the Form_Load procedure was never executed - hence no form was displayed or flashed.


'code for search criteria form:
Sub cmdSearch_Click()
'set the variables or whatever...
frmSearch.Search
End Sub

'now the frmSearch code:
public Sub Search()
'connect to db and search
If rs.eof then
msgbox "No records returned."
else
me.Show
End If
'clean up
rs.close
'etc...
End Sub




That seems to work really well for what i need. I hope you can make some use out of it.

John


John Pirkey
MCSD
www.ShallowWaterSystems.com

Chris Eastwood
February 28th, 2000, 10:28 AM
It's quite straightforward really :

In your code :


private Sub OKButton_Click()
me.Hide
set fModelParams = new ModelParams
fModelParams.Top = 0 ' this loads the form
fModelParams.Left = 0
fModelParams.Width = 9120
fModelParams.Show vbModal ' this shows it, even if it's hidden!
Unload me
End Sub




When you create a new instance of a form - the first time that you access one of it's properties that needs to form to be loaded (ie. setting it's position, background color, caption etc), it will dynamically load the form. (as in the bold line above).

Therefore, the 'fModelParams.top = 0' line is causing your program to load the form and execute the code in :


private Sub Form_Load()
'Open database and get records ... blah blah)
If rs.BOF And rs.EOF then
MsgBox "No Model Families were found. ... " ' etc....
rsRecCount = 0
me.Hide
else
LoadList
End If
End Sub




There's nothing wrong with this - it's finding that there are no records, then hiding the form (although it's still loaded !).

When the code in form_load has executed, the program is then setting the 'left' and 'width' parameters of the form. It's then showing the form modally (which you don't want).


A better way would be to have a public function in your form that you can check.

Eg (in your ModelParams form)


public Function AnythingToDisplay() as Boolean
'
' Do your database checking here
'
If thereisanythingfoundintheDB then
AnythingToDisplay = true
else
AnythingToDisplay = false
End If
End Function





- the idea is to take out all of that checking code in Form_Load (a form will still be loaded, even if an error is raised in that procedure).

Your original code (for the button click) would then be something like :


private Sub OKButton_Click()
me.Hide

set fModelParams = new ModelParams
Load fModelParams ' ensure it's loaded at the correct time
if fModelParams.AnythingToDisplay = true then
fModelParams.Left = 0
'
' etc
'
End IF
End Sub
'





Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

gknierim
February 28th, 2000, 11:04 AM
Thanks Chris. The public Sub was what I was missing. I understand now about loading and unloading. I did some trapping before to see what was calling the load event and I figured it out that as soon as you reference a property from that form it got called. Anyway, everything works smooth. Thanks.

Atlantisoft
February 28th, 2000, 02:07 PM
Just a friendly tip:

Another trick you can do is create an object and set it to an index of 0, creating a control array. Then you can use Load Object(Index) to create new instances of that object during run-time. Same with Unload, cept you cant unload the objects created at design time. Since we're talking about load and unload here, i thought I'd supply a neat trick.
Hope this helps you sometime in the future,
Atlantis