How to CONDITIONALLY show a form in VB 6.0

I have a form I want to show that includes a progress bar with progress messages.  In short, there are a ton of dropdowns on the form, each of whose options are read from the database, and then after the options are loaded, I check another data source to see if I need to select any of them by default.  I would like to have a progress bar with status messages on the form "i.e. loading data 1 of 9..." and then "validating data 1 of 9..."

Two questions:

1.  What is the recommended approach if I want to AUTOMATICALLY UNLOAD the form if any of the validations fail?

2.  What is the recommended approach for AUTOMATICALLY UNLOADING the form if an error occurs in the Form_Load event handler?

This issue has been bugging me for years, so if there are industry-standard slick practices for handling these two things I'd love to hear them.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


1) You could call in every error manager routine the Form_Unload method.
2) If you are referring to the form with the progress bar and messages, you WILL have to wait until the load event finished to be able to unload it. I think you could try to HIDE  the form and the unload it. Remember to call DoEvents if you want to hide the form.

DalTXColtsFanAuthor Commented:
I didn't specify in the original question that I'm showing the form modally.  Don't know if that'll change your answers or not, but here's kind of the approach I'm taking:

If UserEnteredValidData then

The UserEnteredValidData function would show the form modally.  If there was an error during Form_Load or if the user did not enter all data correctly, this function should return False, otherwise it should return True.

So within the UserEnteredValidData function, how should I trap things like, the database was unavailable, data was in the wrong format, or even unexpected errors in Form_Load so that UserEnteredValidData will return false in those cases and not show the form?

Does this make the question more clear or less clear?

It does make the question more clear. The most simple way of doing that would be to call every routine in the ProceedWithUpdate but even doing that, it may happen that the user entered all valid data and even though the operation can not be completed due to a DB error.

The only thing you can do in this cases is to pop up a message box with the error and call the appropiate code to unload the form once the user has accepted the message error, which probably will be located in the DoNotProceedWithUpdate.

I used to program everything like a function (bad practice) so if some part of the WHOLE operation was wrong, I rose a error message, abort the procedure and roll-back any DB transaction.

The code looked like:

If Not TableCouldBeCleaned then
   Unload frmMessageWithProgress  
   Exit Sub
End if

If Not ... then
   Unload frmMessageWithProgress  
   Exit Sub
End if

'If everything went OK,
Unload frmMessageWithProgress
question 1
Use a boolean and check after every step if it's still true

question 2
put a timer on the form and enable it in the form load (no other code in the  form load)
In your timer procedure you put all your code (first line is to disable the timer itself).  This way you can unload from the moment you have an error

You could make continue a global variable in the form and put in the unload event
If continue = true then
Private Sub Form_Load()
    Timer1.Interval = 100
    Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim Continue As Boolean
    Continue = True
    Timer1.Enabled = False
    'step 1  (set continue = false if step 1 fails)
    If Continue = False Then
        Unload Me
    End If
    'step 2  (set continue = false if step 2 fails)
    If Continue = False Then
        Unload Me
    End If
    'do all your step
    Unload Me 'Everything is loaded so the form may close
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.