Click to See Complete Forum and Search --> : Preventing Form Loading Due to Error
daneb
August 9th, 2001, 08:17 PM
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!
enigmaos
August 9th, 2001, 08:41 PM
how about hiding the form when an error reaches the error label in that error trap.
me.Hide
enigmaos@yahoo.com
daneb
August 9th, 2001, 09:02 PM
Thanks for the reply. I have done that also, but the same thing happened. The form still loaded and disappreared immediately. Any more suggestions?
enigmaos
August 9th, 2001, 09:22 PM
how about this:
Form1.Visible = false
enigmaos@yahoo.com
daneb
August 9th, 2001, 09:35 PM
Thanks again. The result is still the same.
enigmaos
August 9th, 2001, 09:39 PM
Could you post the code? I will take a look at it.
enigmaos@yahoo.com
daneb
August 9th, 2001, 10:40 PM
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
enigmaos
August 10th, 2001, 10:48 AM
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
enigmaos@yahoo.com
daneb
August 12th, 2001, 08:18 PM
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.
gena
August 13th, 2001, 02:14 AM
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
daneb
August 13th, 2001, 03:46 AM
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?
gena
August 13th, 2001, 04:39 AM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.