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.
LVL 2
DalTXColtsFanAsked:
Who is Participating?
 
3_SConnect With a Mentor Commented:
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
   Transaction.commit
else
   Transaction.rollback
endif
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
    DoEvents
    '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

0
 
SpeedFingolfCommented:
Answers:

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.

0
 
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
     ProceedWithUpdate
else
     DoNotProceedWithUpdate

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?

Thanks
Joe
0
 
SpeedFingolfCommented:
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
   ShowMsgTableWithTrashData
   Unload frmMessageWithProgress  
   Transaction.Rollback
   Exit Sub
End if

If Not ... then
   ShowMsgXXXX
   Unload frmMessageWithProgress  
   Transaction.Rollback
   Exit Sub
End if

'If everything went OK,
Transaction.commit
Unload frmMessageWithProgress
0
All Courses

From novice to tech pro — start learning today.