CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2001
    Location
    Canada
    Posts
    150

    On Error GoTo....

    what's wrong with this?

    on error GoTo NoFTP
    ' just in case the FTP server is off-line
    NoFTP: GoTo offline

    offline = MsgBox("Sorry, I think the FTP server is Off-Line. Try back later.", vbRetryCancel, "Server offline")

    If offline = vbRetry then
    Command1.Value = 1
    ElseIf offline = vbCancel then
    End
    End If




    Thankz, Drew
    [email protected]?SUBJECT=I Love Your Stuff
    Thanks, Drew

  2. #2
    Join Date
    Jun 2001
    Posts
    13

    Re: On Error GoTo....


    Sub ConnectToFTP()

    on error Goto NoFTP
    ...
    ...
    NoFTP:
    If Msgbox("message", vbRetryCancel) = vbRetry then
    Command1.value = 1
    End If

    End Sub





  3. #3
    Join Date
    Jan 2001
    Location
    Canada
    Posts
    150

    Re: On Error GoTo....

    ahh, so it's not the code but where you put the code. thanks, I did not know that.

    Thankz, Drew
    [email protected]?SUBJECT=I Love Your Stuff
    Thanks, Drew

  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: On Error GoTo....

    One thing I see wrong with it is the use of the "END" statement to terminate program execution. Microsoft Help says the only time the "END" statement should be used is in the Form_Load event of the startup form.
    The "END" statement immediately terminates program execution leaving files opened, Databases active, memory allocations left dirty resulting in memory leaks and a host of other problems.
    The correct way to terminate a application is to issue a "UNLOAD" command which will cause proper program termination freeing up acquired resources and closing files etc. If your application has multiple forms or uses its own memory management then the Form_Unload event should clean these things up as well as allowing VB to clean up.
    Here is a simple routine that should be used in every Form_Unload event that will close all Forms allowing each form to go through its cleanup process.

    '
    ' instead of the "END" statement do a
    Unload me ' Terminate program execution
    '
    ' then in the Form_Unload event
    '
    private Sub Form_Unload(Cancel as Integer)
    Dim f as Form
    for Each f In Forms
    If f.Caption <> me.Caption then Unload f
    next f
    End Sub
    '
    ' In the following example is a correct way to implement error handling for your problem
    ' MySub attempts to do something with the FTP Server which causes an error.
    The error will automatically transfer control to routine NoFTP.
    public Sub MySub()

    on error GoTo NoFTP
    ' perform some code that causes an error. VB will automatically transfer
    ' control to routine NoFTP if an error occurs
    '
    ' If you get here no error occured
    Exit Sub
    NoFTP:
    offline = MsgBox("Sorry, I think the FTP server is Off-Line. Try back later.", vbRetryCancel, "Server offline")
    If offline = vbRetry then
    Command1.Value = 1
    ElseIf offline = vbCancel then
    Unload me
    End If

    End Sub




    Another potential problem could be, but I can not tell from the sample code you supplied.
    You do Command1.Value = 1
    This should trigger a Command1_Click event but if you are already in the command1_Click event, it probably will not trigger the event. Even if it does you are getting into a recursive state which God knows what will happen depending on your code.

    John G

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

Featured