CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2000
    Posts
    292

    Preventing Form Loading Due to Error

    Hi.

    I have a form wherein a database is being attempted to be opened in the Form_Load event. I have written an On Error GoTo statement and an error label in case an error is encountered. In this error label, the form won't continue to load. But what's happening was the form still loads but disappears immediately. I tried transferring my Form_Load codes in the Form_Activate or the Form_Initialize event, but the same thing happened. How can I really prevent the form from showing up when an error occurs? Thanks for the info in advance!


  2. #2
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Re: Preventing Form Loading Due to Error

    how about hiding the form when an error reaches the error label in that error trap.

    me.Hide




    [email protected]


  3. #3
    Join Date
    Mar 2000
    Posts
    292

    Re: Preventing Form Loading Due to Error

    Thanks for the reply. I have done that also, but the same thing happened. The form still loaded and disappreared immediately. Any more suggestions?


  4. #4
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Re: Preventing Form Loading Due to Error

    how about this:
    Form1.Visible = false





    [email protected]

  5. #5
    Join Date
    Mar 2000
    Posts
    292

    Re: Preventing Form Loading Due to Error

    Thanks again. The result is still the same.


  6. #6
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Re: Preventing Form Loading Due to Error

    Could you post the code? I will take a look at it.

    [email protected]

  7. #7
    Join Date
    Mar 2000
    Posts
    292

    Re: Preventing Form Loading Due to Error

    Here is the code:


    Option Explicit
    Dim IsError As Boolean

    Private Sub Form_Activate()
    If IsError Then
    Unload Me
    End If
    End Sub

    Private Sub Form_Load()

    On Error GoTo CantLoad

    IsError = False
    frmMenu.StatBar.SimpleText = "Please wait, Loading form..."
    If DBase_Connect = True Then
    ' Get Bank Code from Systems Setting
    txtBankCode = GetPrivateProfileInt("Bankdetails", "BankCode", 0, (App.Path & "\Bankdetails.ini"))
    If Read_Branch = True Then
    txtReferenceDate = Format(Date, "yyyymmdd")
    sRefDate = txtReferenceDate
    OpenMainTable
    End If
    frmMenu.StatBar.SimpleText = ""
    End If
    Exit Sub

    CantLoad:
    MsgBox "Error encountered in loading form!", vbCritical, "Error Message"
    IsError = True
    Screen.MousePointer = vbDefault
    If MsgBox("Table is currently in use... Retry opening?", vbYesNo + vbInformation, _
    "Reopen Table") = vbYes Then
    frmMenu.StatBar.SimpleText = "Please wait, form..."
    IsError = False
    Screen.MousePointer = vbHourglass
    Resume
    Else
    Me.Hide
    End If

    End Sub



  8. #8
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Re: Preventing Form Loading Due to Error

    I tried your code, and it just hid the form. Did you want the application to end completely? If so, just replace Me.Hide with End shown below in CantLoad trap. Let me know if that's not what you wanted.


    CantLoad:
    MsgBox "error encountered in loading form!", vbCritical, "error Message"
    IsError = true
    Screen.MousePointer = vbDefault
    If MsgBox("Table is currently in use... Retry opening?", vbYesNo + vbInformation, _
    "Reopen Table") = vbYes then
    frmMenu.StatBar.SimpleText = "Please wait, Loading form..."
    IsError = false
    Screen.MousePointer = vbHourglass
    resume
    else
    'me.Hide
    '''''''''try END - it will just kill the app:
    End
    '''''''''
    End If

    End Sub






    [email protected]

  9. #9
    Join Date
    Mar 2000
    Posts
    292

    Re: Preventing Form Loading Due to Error

    Thanks for studying my code. Sorry for the delayed response. I have no Internet access over the weekend.

    No, I don't want to end the entire app. I just want to discontinue the form loading in case an error occurs. So, End is not what I need. If you run the form as one of the forms of a main form, you will notice how the form appears and immediately disappears when the error occurs, and this is what I want to eliminate.


  10. #10
    Join Date
    Jul 2001
    Posts
    5

    Re: Preventing Form Loading Due to Error

    Hi!
    1. If this form is starting form of application, set it's visible property to false in design mode.
    2. If this form is loaded by other - call Load Form instead of Show. So in the case of success you may call Me.Show.

    Regards,
    Gennady



  11. #11
    Join Date
    Mar 2000
    Posts
    292

    Re: Preventing Form Loading Due to Error

    Thanks for the reply. I followed what you suggested. Yes, this is a form which will be loaded from another form, in fact from a menu. The error that I'm testing is that the table needed by the form is open in Design Mode in Access. This will display the error:

    Error: -2147467259
    Table 'A' is exclusively locked by user 'Admin' on machine 'B'.

    After answering No to the prompt if I want to try opening the form again and dismissing the form, the form doesn't load at all the next time I call it. But when I close the table involved (to avoid the error), and answered Yes to retry loading the form, the form loads and will reload the next time I call it. Why is this behaviour?


  12. #12
    Join Date
    Jul 2001
    Posts
    5

    Re: Preventing Form Loading Due to Error

    As far as I've finally understand, you have data bound control on the form. In this case, yes, the runtime error will occur during form load and before any form events may be fired.
    Think the better decision may be to bind to DB dynamically. In this case you may catch runtime error when setting, e.g., DataSource property of the control in the FormLoad proc.

    Regards,
    Gennady



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