Form Open canceled -- but other form's events are executed.

M

Michel S.

Hi !

Sorry if this seems a pre-newbie question, but this one is puzzling me
(maybe because the lack of sleep ?).

I have a form with the following code in its Open event :


Private Sub Form_Open(Cancel As Integer)

If IsOkToProceed = False Then
MsgBox "Can't use this form now - Open canceled"
Cancel = True
End If

End Sub


The IsOkToProceed return false (I added the MsgBox to verify it) and
cancel is set to true.

Despite this, the form's code execution continues with the "Load"
event, etc.. until it fails at the end of the "Current" event, with
the message "Error 2001 - Error set by application or object" (BTW,
isn't it supposed to be "Error 2001 - You canceled the previous
operation" ?).

The problem is that the load and current events perform operations I
don't want to do if the form can't be used.

Is this the way it is supposed to be ?

Don't a canceled open immediately returns to the line where the open
command was given ?

Thanks

PS: I'm using Access 2002
 
A

Armen Stein

Hi !

Sorry if this seems a pre-newbie question, but this one is puzzling me
(maybe because the lack of sleep ?).

I have a form with the following code in its Open event :


Private Sub Form_Open(Cancel As Integer)

If IsOkToProceed = False Then
MsgBox "Can't use this form now - Open canceled"
Cancel = True
End If

End Sub


The IsOkToProceed return false (I added the MsgBox to verify it) and
cancel is set to true.

This is all happening in a main form? No subforms involved?

If so, sounds like possible corruption. Try opening the database with
the Decompile switch on the command line. If that doesn't help, move
all your objects to a new database.
 
M

Michel S.

Armen Stein a présenté l'énoncé suivant :
This is all happening in a main form? No subforms involved?

If so, sounds like possible corruption. Try opening the database with
the Decompile switch on the command line. If that doesn't help, move
all your objects to a new database.

Thanks for your reply.

There's no subform at all in this form.

I decompiled (at least I think it did -- didn't get any feedback at
all), the mdb file size decreased by more that 70% but the Load and
Current code still execute.

Since it's not the expected behavior, I suppose there's no way in the
load event to know that the Open was cancelled (beside setting my own
flags) ?

Anyway, I don't want to work around a misbehaving code -- I'd like to
fix it once for all !
 
A

Armen Stein

Armen Stein a présenté l'énoncé suivant :

You didn't mention whether you had imported all objects into a new
database container. Believe me, that solves the weirdest problems
sometimes.
 
M

Michel S.

Le 2006-08-09, Armen Stein a supposé :
You didn't mention whether you had imported all objects into a new
database container. Believe me, that solves the weirdest problems
sometimes.

Thanks again for your reply.

I just created a new container and imported forms, tables and code only
but still no change in the behavior : current and load events are
still executing. :-(

Few questions remains :

1) Am I supposed to get a feedback informing me that the database was
decompiled (Access 2002) ? I didn't get any message informing me that
it was happening.

2) If an object is corrupted in a .mdb and I import it "as is" in a new
container without any other operation, is there any reason why the
corruption problem should be solved ?

3) Beside deleting and recreating the form, any other possible
solutions to explore ?

Thanks again !
 
M

Michel S.

Additional info :

If I open the form directly from the database window, the open event
cancels correctly and the Load and Current events don't execute.

But the problem remains if I use the following from a simple
switchboard form I made (not from Acecss switchboard wizard) :

Private Sub cmdImport_Click()

On Error Resume Next
DoCmd.OpenForm Form_ImportData, acNormal, , , acFormReadOnly, _
acWindowNormal
MsgBox Err.Number ' <-- displays 2001

End Sub

The Form_ImportData Current and Load events also have a MsgBox
statement showing that they are executed in that case.

Hope this helps finding what going wrong.


 
A

Armen Stein

Few questions remains :

1) Am I supposed to get a feedback informing me that the database was
decompiled (Access 2002) ? I didn't get any message informing me that
it was happening.

--> No, it happens silently.

2) If an object is corrupted in a .mdb and I import it "as is" in a new
container without any other operation, is there any reason why the
corruption problem should be solved ?

--> We've found that the process of importing all the objects will
usually "leave behind" any corruption, especially VBA code corruption.
Occasionally, we'll need to completely rebuild a form from scratch.

3) Beside deleting and recreating the form, any other possible
solutions to explore ?

--> That's the only one I can think of at this point.
 
A

Armen Stein

Additional info :

If I open the form directly from the database window, the open event
cancels correctly and the Load and Current events don't execute.

But the problem remains if I use the following from a simple
switchboard form I made (not from Acecss switchboard wizard) :

Private Sub cmdImport_Click()

On Error Resume Next
DoCmd.OpenForm Form_ImportData, acNormal, , , acFormReadOnly, _
acWindowNormal
MsgBox Err.Number ' <-- displays 2001

End Sub

The Form_ImportData Current and Load events also have a MsgBox
statement showing that they are executed in that case.

Try changing your OpenForm parameters. We don't use acFormReadOnly -
you might try removing that. Nothing there should cause this problem,
but it might narrow down the issue.

You might also try building a very simple new form with the cancel logic
in the On Open event to see if it also has a problem.
 
M

Michel S.

Armen Stein a émis l'idée suivante :
Try changing your OpenForm parameters. We don't use acFormReadOnly -
you might try removing that. Nothing there should cause this problem,
but it might narrow down the issue.

You might also try building a very simple new form with the cancel logic
in the On Open event to see if it also has a problem.


Tried it with the original form - only DoCmd.OpenForm Form_frmName,
acNormal - no change.


In addition, I tried with a less than simple unbound form (1 command
button, code in Open event is simply : "Cancel = True" and a MsgBox
in Load and Current events).

Results :
- When called from OpenForm command, the Current (but not Load) event
is fired and the error message is correct : "2001: Operation
cancelled"

- When opened from the database window (or switching from design mode
to Form mode), it simply does nothing, which is what is expected.


Finally, I tested with another (more complex) form I designed - this
one containing no Form_Open routine.

This form opens and execute flawlessly from the database window, but if
I call it from a DoCmd.OpenForm command, the Load and Current subs
execute, but before the form appears on screen, it fails with a very
odd error : "error 2498 - An expression you entered is the wrong data
type for one of the arguments".

Debugging this error bring me back to the calling DoCmd line, and I
can't even single step further in the code.

Is there any exorcist on this forum ?? :'(


May I ask you if you do the same thing as my very simple form, if you
get the same results I do ?

Thanks again !

PS: for recall, it's Access 2002, sp3, all availiable patches applied.
Machine is free of viruses, spywares or other malwares.
 
A

Armen Stein

I'm using Access 2003, but it shouldn't make any difference.

I tried a simple form that Cancels the On Open event. As expected, the
On Current and On Load events did not fire. This was true whether I
opened the form directly from the database window, or from code in
another form.

I'm afraid I've run out of ideas on this one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top