469,632 Members | 1,681 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

How do I stop a form's open event in if there's an error?

Hi everyone,

As some of you may know, we've been having trouble with an
unrecognized database format error.

Today I installed an unfinished project on the workstation of one of
my users. It's a query by form in which she enters the criteria of
the record she wants in an unbound form, and it pulls only that record
and switches to the entry form so she can enter data. I had noticed
it was faster than find or filter, so I offered it to her even though
the other functions and features aren't done yet.

One of the things in this form is code in the Open event that opens an
empty table to stabilize the links to the back end. This code has
been in both my main forms, which are bound, for quite a while. When
I wrote it it didn't have any error handling code, but we never had
any trouble with it.

My user got the UDBF error while I was away from my desk, and when I
came back she also had a run-time error on the last line of the open
empty table code. This had never happened before on the other forms.
I tried entering error handling code, and this makes it generate a
message box when it can't connect to the database instead of a
run-time error.

So far so good, but then it opens the form anyway. I don't want it to
do that because it's confusing - I thought, "Oh, maybe it will still
work" - and if I think that, my users will also.

Does anyone know how I can stop the Open event and prevent it from
opening the form in case of an error? I tried the DoCmd.Close, but it
opens anyway.

Here is the code as I have it now:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open

'Opens empty table to stay open while form is in use and improve
performance
'Closing of table not coded as .ldb file goes away when form is
closed.

Dim db As Database
Dim FormMe As Form

Set db = CurrentDB
Set FormMe = Forms!frmCriteria

Dim EmptyTable As Recordset
Set EmptyTable = db.OpenRecordset("tblEmpty", dbOpenDynaset,
dbOptimistic)

Exit_Form_Open:
Exit Sub

Err_Form_Open:
MsgBox Err.Description
DoCmd.Close acForm, FormMe, acSaveNo
Resume Exit_Form_Open

End Sub

Thanks for any help you can give!

Julia
Nov 13 '05 #1
2 3779
To stop the form from opening when there is an error place

Cancel = True

in the error handler for the Open event. Of course, this will only work on
errors that can be trapped in the Open event.

For information on corruption, these links may come in handy. You may have
seen these already, but just in case:
http://members.iinet.net.au/~allenbrowne/ser-25.html
http://members.iinet.net.au/~allenbrowne/ser-47.html

And on the UDBF error:
http://support.microsoft.com/default...roduct=acc2002
http://support.microsoft.com/default...22&Product=acc
--
Wayne Morgan
MS Access MVP
"Julia Baresch" <jb******@oldrepublic.com> wrote in message
news:50**************************@posting.google.c om...
Hi everyone,

As some of you may know, we've been having trouble with an
unrecognized database format error.

Today I installed an unfinished project on the workstation of one of
my users. It's a query by form in which she enters the criteria of
the record she wants in an unbound form, and it pulls only that record
and switches to the entry form so she can enter data. I had noticed
it was faster than find or filter, so I offered it to her even though
the other functions and features aren't done yet.

One of the things in this form is code in the Open event that opens an
empty table to stabilize the links to the back end. This code has
been in both my main forms, which are bound, for quite a while. When
I wrote it it didn't have any error handling code, but we never had
any trouble with it.

My user got the UDBF error while I was away from my desk, and when I
came back she also had a run-time error on the last line of the open
empty table code. This had never happened before on the other forms.
I tried entering error handling code, and this makes it generate a
message box when it can't connect to the database instead of a
run-time error.

So far so good, but then it opens the form anyway. I don't want it to
do that because it's confusing - I thought, "Oh, maybe it will still
work" - and if I think that, my users will also.

Does anyone know how I can stop the Open event and prevent it from
opening the form in case of an error? I tried the DoCmd.Close, but it
opens anyway.

Here is the code as I have it now:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open

'Opens empty table to stay open while form is in use and improve
performance
'Closing of table not coded as .ldb file goes away when form is
closed.

Dim db As Database
Dim FormMe As Form

Set db = CurrentDB
Set FormMe = Forms!frmCriteria

Dim EmptyTable As Recordset
Set EmptyTable = db.OpenRecordset("tblEmpty", dbOpenDynaset,
dbOptimistic)

Exit_Form_Open:
Exit Sub

Err_Form_Open:
MsgBox Err.Description
DoCmd.Close acForm, FormMe, acSaveNo
Resume Exit_Form_Open

End Sub

Thanks for any help you can give!

Julia

Nov 13 '05 #2
Thanks Wayne! I tried Cancel = True and it worked beautifully. After
I posted this yesterday I found the DoCmd.CancelEvent and used that,
but it generated a second message something like "you have cancelled
an event..." The Cancel = True doesn't do that, so it's a better
solution.

This is one of those things that so simple it's not in the
documentation...

Thanks also for the links. I hadn't seen the MS articles before, and
the one about the Visual Studio service pack might turn out to be what
we need. :-)

Julia

"Wayne Morgan" <co***************************@hotmail.com> wrote in message news:<%l***************@newssvr23.news.prodigy.com >...
To stop the form from opening when there is an error place

Cancel = True

in the error handler for the Open event. Of course, this will only work on
errors that can be trapped in the Open event.

For information on corruption, these links may come in handy. You may have
seen these already, but just in case:
http://members.iinet.net.au/~allenbrowne/ser-25.html
http://members.iinet.net.au/~allenbrowne/ser-47.html

And on the UDBF error:
http://support.microsoft.com/default...roduct=acc2002
http://support.microsoft.com/default...22&Product=acc
--
Wayne Morgan
MS Access MVP
"Julia Baresch" <jb******@oldrepublic.com> wrote in message
news:50**************************@posting.google.c om...
Hi everyone,

As some of you may know, we've been having trouble with an
unrecognized database format error.

Today I installed an unfinished project on the workstation of one of
my users. It's a query by form in which she enters the criteria of
the record she wants in an unbound form, and it pulls only that record
and switches to the entry form so she can enter data. I had noticed
it was faster than find or filter, so I offered it to her even though
the other functions and features aren't done yet.

One of the things in this form is code in the Open event that opens an
empty table to stabilize the links to the back end. This code has
been in both my main forms, which are bound, for quite a while. When
I wrote it it didn't have any error handling code, but we never had
any trouble with it.

My user got the UDBF error while I was away from my desk, and when I
came back she also had a run-time error on the last line of the open
empty table code. This had never happened before on the other forms.
I tried entering error handling code, and this makes it generate a
message box when it can't connect to the database instead of a
run-time error.

So far so good, but then it opens the form anyway. I don't want it to
do that because it's confusing - I thought, "Oh, maybe it will still
work" - and if I think that, my users will also.

Does anyone know how I can stop the Open event and prevent it from
opening the form in case of an error? I tried the DoCmd.Close, but it
opens anyway.

Here is the code as I have it now:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open

'Opens empty table to stay open while form is in use and improve
performance
'Closing of table not coded as .ldb file goes away when form is
closed.

Dim db As Database
Dim FormMe As Form

Set db = CurrentDB
Set FormMe = Forms!frmCriteria

Dim EmptyTable As Recordset
Set EmptyTable = db.OpenRecordset("tblEmpty", dbOpenDynaset,
dbOptimistic)

Exit_Form_Open:
Exit Sub

Err_Form_Open:
MsgBox Err.Description
DoCmd.Close acForm, FormMe, acSaveNo
Resume Exit_Form_Open

End Sub

Thanks for any help you can give!

Julia

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Baffin Shea | last post: by
1 post views Thread by magic man via .NET 247 | last post: by
8 posts views Thread by Matt Theule | last post: by
2 posts views Thread by Martyn Fewtrell | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.