a code to close a form using the escape button??

J

jo

Hi I would like to add a code to form that will close the form using the esc
button if required by the user?
Thanks
Jo
 
P

Peter Hibbs

Jo,

If you already have a button on the form which closes the form then
you can just set its Cancel property to Yes. The ESCAPE key will then
do whatever code you have in its On Click event.

If you don't already have a button, then you can add one to the form
and set its On Click event to :-

DoCmd.Close

and its Cancel property to Yes. If you don't want the button to be
visible you can set its Transparent property (NOT its Visible
property) to Yes (and make its size very small).

HTH

Peter Hibbs.
 
D

Dirk Goldgar

jo said:
Hi I would like to add a code to form that will close the form using the
esc
button if required by the user?


Here's one simple way:

1. Add a command button to the form. Call it "cmdClose". If you already
have a command button on the form to close the form, just skip to step 4 and
use that existing button instead.

2. If you don't want the user to be able to see and click the command
button, set properties of the button as follows:

Transparent: Yes
Tab Stop: No
Height: 0
Width: 0

And position it in some out-of-the-way location on the form.

3. Code the button's Click event to close the form:

Private Sub cmdClose_Click()

DoCmd.Close acForm, Me.Name, acSaveNo

End Sub

4. Set the button's Cancel property (on the Other tab of its property sheet)
to Yes.

And you're done.
 
J

jo

Hi Guys Ihave tried both ways and the form does not cancel when i use the esc
button? What am I doing wrong??
 
D

Dirk Goldgar

jo said:
Hi Guys Ihave tried both ways and the form does not cancel when i use the
esc
button? What am I doing wrong??


Hmm. Both Peter and I essentially suggested the same thing, and it should
have worked if implemented correctly -- unless, of course, there's something
we don't know about the situation. Here are some questions for you:

When you say the form does not cancel, what exactly do you mean? Does
nothing at all happen? Do you get an error message or prompt? Does it
close, but save a record you don't want to save?

What is your form's name?

Is it a bound form or an unbound form; that is, does it have a Record
Source?

What is the name of the command button?

Is there a subform involved? If so, is the button on the main form or on
the subform? (It must be on the main form for this to work.)

What is the value of the command button's Cancel property?

What is the value of the command button's Visible property?

What is the value of the command button's On Click property? It should be
"[Event Procedure]" (without the quotes).

What is the code for the command button's Click event procedure?
 
J

jo

Right what I have is a form that is not bound, I have labels that when you
click on them they open others forms or dialogs. I call it my floating menu
it just looks like a button with text on it.
I just tried both of your ideas and nothing work or came up as an error? I
put a command button on the form and done as you both suggested and I put the
code on the button's click event and changed the cancel yes?
There are no subforms, no record source..



Dirk Goldgar said:
jo said:
Hi Guys Ihave tried both ways and the form does not cancel when i use the
esc
button? What am I doing wrong??


Hmm. Both Peter and I essentially suggested the same thing, and it should
have worked if implemented correctly -- unless, of course, there's something
we don't know about the situation. Here are some questions for you:

When you say the form does not cancel, what exactly do you mean? Does
nothing at all happen? Do you get an error message or prompt? Does it
close, but save a record you don't want to save?

What is your form's name?

Is it a bound form or an unbound form; that is, does it have a Record
Source?

What is the name of the command button?

Is there a subform involved? If so, is the button on the main form or on
the subform? (It must be on the main form for this to work.)

What is the value of the command button's Cancel property?

What is the value of the command button's Visible property?

What is the value of the command button's On Click property? It should be
"[Event Procedure]" (without the quotes).

What is the code for the command button's Click event procedure?

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
J

jo

P.S the form's name is FLOATER and i named the command button cmdClose as
suggested.

Dirk Goldgar said:
jo said:
Hi Guys Ihave tried both ways and the form does not cancel when i use the
esc
button? What am I doing wrong??


Hmm. Both Peter and I essentially suggested the same thing, and it should
have worked if implemented correctly -- unless, of course, there's something
we don't know about the situation. Here are some questions for you:

When you say the form does not cancel, what exactly do you mean? Does
nothing at all happen? Do you get an error message or prompt? Does it
close, but save a record you don't want to save?

What is your form's name?

Is it a bound form or an unbound form; that is, does it have a Record
Source?

What is the name of the command button?

Is there a subform involved? If so, is the button on the main form or on
the subform? (It must be on the main form for this to work.)

What is the value of the command button's Cancel property?

What is the value of the command button's Visible property?

What is the value of the command button's On Click property? It should be
"[Event Procedure]" (without the quotes).

What is the code for the command button's Click event procedure?

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
D

Dirk Goldgar

jo said:
Right what I have is a form that is not bound, I have labels that when you
click on them they open others forms or dialogs. I call it my floating
menu
it just looks like a button with text on it.
I just tried both of your ideas and nothing work or came up as an error? I
put a command button on the form and done as you both suggested and I put
the
code on the button's click event and changed the cancel yes?
There are no subforms, no record source..


Are you using Access 2007? If so, is the database in a trusted location, or
have you explicitly enabled VBA code in this database? Does *any* VBA code
execute?
 
C

Clifford Bass

Hi,

Does the form have the focus when you are pressing the Esc? It will
only work when it is the topmost form. If you make the button so it can be
seen, and you click on it, what happens?

Clifford Bass
 
J

jo

Hi Clifford I done as you suggested and made the button command visible and
when click or use the ESC button I get a Complie Error saying Method or Dta
member not found. At it highlights the form's name FLOATER.
The code i have there is:
Private Sub cmdClose_Click()

DoCmd.Close acForm, Me.FLOATER, acSaveNo

End Sub
 
D

Dirk Goldgar

jo said:
Hi Clifford I done as you suggested and made the button command visible
and
when click or use the ESC button I get a Complie Error saying Method or
Dta
member not found. At it highlights the form's name FLOATER.
The code i have there is:
Private Sub cmdClose_Click()

DoCmd.Close acForm, Me.FLOATER, acSaveNo

End Sub

Ah, you've misinterpreted the line of code I posted. I didn't mean you to
substitute the form's name for the keyword "Name" in that line. The
statement should be exactly as I posted it:

DoCmd.Close acForm, Me.Name, acSaveNo
 
J

jo

Hi Dirk that works brilliant thanks very much!! I just thought when it said
name to put the form's name in..
Once again thanks for time all you guys!!
Jo
 

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