Results 1 to 8 of 8

Thread: Need progress bar to stop if there is an error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    I have a progress bar that begins when the user presses a command button; but if there is no text in a cbo box, I want it to generate and error -- I have the error routine generating fine with an if statement; however the progress bar still goes and then I get debug errors in my sql statement because nothing was entered in the cbo box

    How can I make the progress bar stop as well. I need to generate the error and go back to the process and not have the progress bar still progress

    Thank you in advance!!!

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374
    Post some code so people can have a look. But without seeing your routine, a guess: you should put the error handling statement for the combo box before anything involving the progress bar, and you should also have an On Error statement where you make the progress bar stop if other errors occur, 0e.g.

    [code]
    Private Sub cmdContinue_Click()

    If Combo1.text = "" Then
    MsgBox "Please make a selection before continuing.", vbInformation
    Combo1.SetFocus
    Exit Sub 'make sure you have the exit sub statement
    End If

    On Error GoTo dbError_Handler

    ProgressBar1.Visible = True
    ProgessBar1.Max = 'whatever the maximum is
    DoEvents

    'then have your recordset code in here, then make the bar invisible

    ProgressBar1.Value = ProgressBar1.Max
    ProgressBar1.Visible = False
    ProgressBar1.Value = 0
    DoEvents

    Exit Sub

    dbError_Handler:
    MsgBox "Error in database." & Chr(13) & Chr(10) & _
    Chr(13) & Chr(10) & Err.Description, vbCritical

    ProgressBar1.Value = 0
    ProgressBar1.Visible = False
    DoEvents

    End Sub


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    Can you explain to me about the exit sub; why must it be used in the msgbox and also at then end before the actual error handler? Or explain a little bit about exit sub and what it does?

    I would really appreciate it, thanks

    Oh, it worked by the way

    I just needed the exit sub

  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Exit sub does exactly what it says. It exits the sub routine immediatley, and no other code is executed.

    You always need an exit sub before an error handler. This is because if the sub does not produce an error you don't want to go to an error routine.

    If an error occurs the point of execution jumps to the error handler. Which is what we want, but if no error occurs we don't want to goto the error routine. So we put an exit sub to finish the procedure.

    You still need an error handler in case of any errors, but there is no reason why the On Error statment should not be first statment in the sub. I don't know why DrewDog_21 put it after the If statement.
    Iain, thats with an i by the way!

  5. #5
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Cool

    Hmm... I guess the On Error statement should go first. I'm just in the habit of putting my If...Exit Sub...End if things first.

    Anyway Bebe, the Exit Sub works just like lain said. You want to have the Exit Sub with the MsgBox because there you are checking to see that the user has made a selection in the combo box. If the combo box is blank, that is what causes the errors in your SQL statement. Without the Exit Sub with the Messagebox, the message box would appear but then the rest of the code will still execute, and you will get the same errors in your SQL.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    I Have noticed in a lot of other peoples code that they put the error handler routines at the end of the sub. I looked at a few other programs and have noticed this so maybe it does go at the end; kind of a programming rule maybe.


    Thanks for clarifying


  7. #7
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Yes the actual error handler goes at the end of the sub. What i mean is the "On Error Goto" line should be at the start. Well oviously after any variable declarations.

    e.g.
    Code:
    Private Sub mySub()
        Dim i as Integer
    
        On Error Goto myErrorHandler
    
        'a lot of code goes here
    
        Exit sub
    
    myErrorHandler:
        MsgBox "Opps!"
    
    End Sub
    [Edited by Iain17 on 05-05-2000 at 06:09 PM]
    Iain, thats with an i by the way!

  8. #8
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516

    Random comment:

    Why is it that everybody's text (not in code blocks) is coloured like
    VB code.

    I think somebody didn't terminate a code tag ...

    [/code]

    I think I just have. Here is the test: an apostrophe

    'Is this green?
    Courgettes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width