Open form with Enter key???

N

Niklas Östergren

Hi!

I´m trying to open a form if the user press <Return key> on the keyboard. I
have never doen this before and have got an explanation in this forum before
but I didn´t understand it.

This is what I tryed with but no success:
=======================================
Private Sub MNr_KeyPress(KeyAscii As Integer)
If vbKeyReturn Then
Call OpenForm("frmPerson", "[PersonID]=" & intPersonID)
End If
End Sub
========================================

TIA!
// Niklas
 
A

Albert D. Kallal

I guess it depends on where your current cursor is..or what is currently
displayed on the screen.

But yes..it is a great idea to enable the Enter key to open up a form based
on the current details.

If you look at he following screen shots....I always enable the cursor when
the user hits enter.

http://www.attcanada.net/~kallal.msn/Articles/Grid.htm

The code use is:

Private Sub MNr_KeyPress(KeyAscii As Integer)
If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if
End Sub

of course, the above assumes a key id of personID. Often, the key id is
"id"..so you would use:

docmd.OpenForm "frmPerson",,,"id = " & me.ID

Here is some screen shots where I use the above idea..and again enter key
opens up the form...

http://www.attcanada.net/~kallal.msn/Search/index.html
 
N

Niklas Östergren

Hi Albert!

I´m at work right now and the db I´m working on is strictly free of charge
and therfore outside my working task. But I´ll take a closer look at this
latet this eavning.

I have, though, take a fast look at the web sites you gave me link´s for and
it looks good. So I would like to thank you for handing me into the right
direction!

Thanks!
// Niklas
 
N

Niklas Östergren

Hmm!

I havn´t tryed it out but I was to curious to wait untill late evening!
I don´t know if I have missunderstod your reply but the DoCmd.OpenForm
works. What I don´t get is how to fire the code when <Enter / Return> key is
pressed. That´s my Q!

TIA
// Niklas
 
N

Neil

Niklas,

Every Access control (as far as i know) has a KeyPress event. The forms
actually have a KeyPreview property which will in turn send the keypress to
the KeyPress of a form no matter what control has the focus at the time of
the user pressing enter. For example:

You have a form named frmTest. This has a textbox on it named txtMyTextBox
and a ComboBox named cboMyCombo. If you had programed the KeyPress event for
the textbox only then the code would look like:

Private Sub txtMyTextBox_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime the user pressed *any* key in the textbox.
Your code checks and only acts if the Return key has been pressed. If you
put the same procedure in the forms KeyPress event (KeyPreview has to be set
to yes for this to work as the form doesnt receive the focus unless there
are no controls on it - not much use then :)).

Private Sub Form_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime any key was pressed in any control on the
form. Again, the form would only open if it was the Return key that had been
pressed.

Hopefully, using the above information, you can now work out where you need
to place the code and in which controls (if any) to get the desired effect
you are after.

HTH,

Neil.

Niklas Östergren said:
Hmm!

I havn´t tryed it out but I was to curious to wait untill late evening!
I don´t know if I have missunderstod your reply but the DoCmd.OpenForm
works. What I don´t get is how to fire the code when <Enter / Return> key is
pressed. That´s my Q!

TIA
// Niklas


Albert D. Kallal said:
I guess it depends on where your current cursor is..or what is currently
displayed on the screen.

But yes..it is a great idea to enable the Enter key to open up a form based
on the current details.

If you look at he following screen shots....I always enable the cursor when
the user hits enter.

http://www.attcanada.net/~kallal.msn/Articles/Grid.htm

The code use is:

Private Sub MNr_KeyPress(KeyAscii As Integer)
If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if
End Sub

of course, the above assumes a key id of personID. Often, the key id is
"id"..so you would use:

docmd.OpenForm "frmPerson",,,"id = " & me.ID

Here is some screen shots where I use the above idea..and again enter key
opens up the form...

http://www.attcanada.net/~kallal.msn/Search/index.html

--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.attcanada.net/~kallal.msn
 
N

Niklas Östergren

Hi Niel!

Thank´s a lot! It works now! The problem I hade is that I was running the
code from the KeyPress_Event BUT hade the Locked propertie set for the
control.

Is there any way around this because I realy would like to have the
propertie Locked = True on all the controls accept for one. The form is only
a list of records and I don´t want the user to be able to edit the records
from this form (frmListMembers). If the user need to edit the records he/she
just needs to open up (frmPerson) which I want to open with help of Return
key!

TIA!
// Niklas


Neil said:
Niklas,

Every Access control (as far as i know) has a KeyPress event. The forms
actually have a KeyPreview property which will in turn send the keypress to
the KeyPress of a form no matter what control has the focus at the time of
the user pressing enter. For example:

You have a form named frmTest. This has a textbox on it named txtMyTextBox
and a ComboBox named cboMyCombo. If you had programed the KeyPress event for
the textbox only then the code would look like:

Private Sub txtMyTextBox_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime the user pressed *any* key in the textbox.
Your code checks and only acts if the Return key has been pressed. If you
put the same procedure in the forms KeyPress event (KeyPreview has to be set
to yes for this to work as the form doesnt receive the focus unless there
are no controls on it - not much use then :)).

Private Sub Form_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime any key was pressed in any control on the
form. Again, the form would only open if it was the Return key that had been
pressed.

Hopefully, using the above information, you can now work out where you need
to place the code and in which controls (if any) to get the desired effect
you are after.

HTH,

Neil.

Niklas Östergren said:
Hmm!

I havn´t tryed it out but I was to curious to wait untill late evening!
I don´t know if I have missunderstod your reply but the DoCmd.OpenForm
works. What I don´t get is how to fire the code when <Enter / Return>
key
is
pressed. That´s my Q!

TIA
// Niklas


Albert D. Kallal said:
I guess it depends on where your current cursor is..or what is currently
displayed on the screen.

But yes..it is a great idea to enable the Enter key to open up a form based
on the current details.

If you look at he following screen shots....I always enable the cursor when
the user hits enter.

http://www.attcanada.net/~kallal.msn/Articles/Grid.htm

The code use is:

Private Sub MNr_KeyPress(KeyAscii As Integer)
If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if
End Sub

of course, the above assumes a key id of personID. Often, the key id is
"id"..so you would use:

docmd.OpenForm "frmPerson",,,"id = " & me.ID

Here is some screen shots where I use the above idea..and again enter key
opens up the form...

http://www.attcanada.net/~kallal.msn/Search/index.html

--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.attcanada.net/~kallal.msn
 
N

Neil

Right,

I have looked into this and I think this is because you are using the
KeyPress event. In my DB I use the KeyDown event (which works when the
control is locked but not when it is disabled - as you cant get into it
obviously :)). The two events are near enought the same except the KeyDown
event repeats when the key is held down and there is another agument passed
in 'Shift'. Shift is used to determin if any of the special keys were
pressed down (eg ctl or shift or alt). My code looks like this:

Private Sub txtTest_KeyDown(KeyCode As Integer, Shift As Integer)

Dim intCtrlDown As Integer

' Use bit mask to determine which key was pressed.
intCtrlDown = (Shift And acCtrlMask) > 0

If intCtrlDown Then
If KeyCode = vbKeyF10 Then
MsgBox "In Key Down"
End If
End If

End Sub

Even when the test textbox is locked, the message box is still displayed.
Give it a try and see if it solves your problem.

HTH,

Neil.

Niklas Östergren said:
Hi Niel!

Thank´s a lot! It works now! The problem I hade is that I was running the
code from the KeyPress_Event BUT hade the Locked propertie set for the
control.

Is there any way around this because I realy would like to have the
propertie Locked = True on all the controls accept for one. The form is only
a list of records and I don´t want the user to be able to edit the records
from this form (frmListMembers). If the user need to edit the records he/she
just needs to open up (frmPerson) which I want to open with help of Return
key!

TIA!
// Niklas


"Neil" <neil@_NoSPAM_haywood2k3.freeserve_NoJUNK_.co.uk> skrev i meddelandet
Niklas,

Every Access control (as far as i know) has a KeyPress event. The forms
actually have a KeyPreview property which will in turn send the keypress to
the KeyPress of a form no matter what control has the focus at the time of
the user pressing enter. For example:

You have a form named frmTest. This has a textbox on it named txtMyTextBox
and a ComboBox named cboMyCombo. If you had programed the KeyPress event for
the textbox only then the code would look like:

Private Sub txtMyTextBox_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime the user pressed *any* key in the textbox.
Your code checks and only acts if the Return key has been pressed. If you
put the same procedure in the forms KeyPress event (KeyPreview has to be set
to yes for this to work as the form doesnt receive the focus unless there
are no controls on it - not much use then :)).

Private Sub Form_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime any key was pressed in any control on the
form. Again, the form would only open if it was the Return key that had been
pressed.

Hopefully, using the above information, you can now work out where you need
to place the code and in which controls (if any) to get the desired effect
you are after.

HTH,

Neil.

Niklas Östergren said:
Hmm!

I havn´t tryed it out but I was to curious to wait untill late evening!
I don´t know if I have missunderstod your reply but the DoCmd.OpenForm
works. What I don´t get is how to fire the code when <Enter / Return>
key
is
pressed. That´s my Q!

TIA
// Niklas


"Albert D. Kallal" <[email protected]> skrev i meddelandet
I guess it depends on where your current cursor is..or what is currently
displayed on the screen.

But yes..it is a great idea to enable the Enter key to open up a form
based
on the current details.

If you look at he following screen shots....I always enable the cursor
when
the user hits enter.

http://www.attcanada.net/~kallal.msn/Articles/Grid.htm

The code use is:

Private Sub MNr_KeyPress(KeyAscii As Integer)
If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if
End Sub

of course, the above assumes a key id of personID. Often, the key id is
"id"..so you would use:

docmd.OpenForm "frmPerson",,,"id = " & me.ID

Here is some screen shots where I use the above idea..and again
enter
key
opens up the form...

http://www.attcanada.net/~kallal.msn/Search/index.html

--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.attcanada.net/~kallal.msn
 
N

Niklas Östrergren

Hi again Niel!

Thanks for your reply!

I´ll try it out in a while and I´ll get back to you as soon as I know!

Thanks for helping me out here! It´s apreciated!

// Niklas


Neil said:
Right,

I have looked into this and I think this is because you are using the
KeyPress event. In my DB I use the KeyDown event (which works when the
control is locked but not when it is disabled - as you cant get into it
obviously :)). The two events are near enought the same except the KeyDown
event repeats when the key is held down and there is another agument passed
in 'Shift'. Shift is used to determin if any of the special keys were
pressed down (eg ctl or shift or alt). My code looks like this:

Private Sub txtTest_KeyDown(KeyCode As Integer, Shift As Integer)

Dim intCtrlDown As Integer

' Use bit mask to determine which key was pressed.
intCtrlDown = (Shift And acCtrlMask) > 0

If intCtrlDown Then
If KeyCode = vbKeyF10 Then
MsgBox "In Key Down"
End If
End If

End Sub

Even when the test textbox is locked, the message box is still displayed.
Give it a try and see if it solves your problem.

HTH,

Neil.

Niklas Östergren said:
Hi Niel!

Thank´s a lot! It works now! The problem I hade is that I was running the
code from the KeyPress_Event BUT hade the Locked propertie set for the
control.

Is there any way around this because I realy would like to have the
propertie Locked = True on all the controls accept for one. The form is only
a list of records and I don´t want the user to be able to edit the records
from this form (frmListMembers). If the user need to edit the records he/she
just needs to open up (frmPerson) which I want to open with help of Return
key!

TIA!
// Niklas


"Neil" <neil@_NoSPAM_haywood2k3.freeserve_NoJUNK_.co.uk> skrev i meddelandet keypress
to
time
of
the user pressing enter. For example:

You have a form named frmTest. This has a textbox on it named txtMyTextBox
and a ComboBox named cboMyCombo. If you had programed the KeyPress
event
for
the textbox only then the code would look like:

Private Sub txtMyTextBox_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime the user pressed *any* key in the textbox.
Your code checks and only acts if the Return key has been pressed. If you
put the same procedure in the forms KeyPress event (KeyPreview has to
be
set
to yes for this to work as the form doesnt receive the focus unless there
are no controls on it - not much use then :)).

Private Sub Form_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime any key was pressed in any control
on
the
form. Again, the form would only open if it was the Return key that
had
been
pressed.

Hopefully, using the above information, you can now work out where you need
to place the code and in which controls (if any) to get the desired effect
you are after.

HTH,

Neil.

Hmm!

I havn´t tryed it out but I was to curious to wait untill late evening!
I don´t know if I have missunderstod your reply but the DoCmd.OpenForm
works. What I don´t get is how to fire the code when <Enter /
Return>
key
is
pressed. That´s my Q!

TIA
// Niklas


"Albert D. Kallal" <[email protected]> skrev i meddelandet
I guess it depends on where your current cursor is..or what is currently
displayed on the screen.

But yes..it is a great idea to enable the Enter key to open up a form
based
on the current details.

If you look at he following screen shots....I always enable the cursor
when
the user hits enter.

http://www.attcanada.net/~kallal.msn/Articles/Grid.htm

The code use is:

Private Sub MNr_KeyPress(KeyAscii As Integer)
If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if
End Sub

of course, the above assumes a key id of personID. Often, the key
id
is
"id"..so you would use:

docmd.OpenForm "frmPerson",,,"id = " & me.ID

Here is some screen shots where I use the above idea..and again enter
key
opens up the form...

http://www.attcanada.net/~kallal.msn/Search/index.html

--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.attcanada.net/~kallal.msn
 
N

Niklas Östergren

THANK´S a lot Niel!

It works like a charm! Just the way I wanted!
You have made my day and besides my headache have dissapeared. Have had it
all day but now it´s gone! ;-)

// Niklas


Neil said:
Right,

I have looked into this and I think this is because you are using the
KeyPress event. In my DB I use the KeyDown event (which works when the
control is locked but not when it is disabled - as you cant get into it
obviously :)). The two events are near enought the same except the KeyDown
event repeats when the key is held down and there is another agument passed
in 'Shift'. Shift is used to determin if any of the special keys were
pressed down (eg ctl or shift or alt). My code looks like this:

Private Sub txtTest_KeyDown(KeyCode As Integer, Shift As Integer)

Dim intCtrlDown As Integer

' Use bit mask to determine which key was pressed.
intCtrlDown = (Shift And acCtrlMask) > 0

If intCtrlDown Then
If KeyCode = vbKeyF10 Then
MsgBox "In Key Down"
End If
End If

End Sub

Even when the test textbox is locked, the message box is still displayed.
Give it a try and see if it solves your problem.

HTH,

Neil.

Niklas Östergren said:
Hi Niel!

Thank´s a lot! It works now! The problem I hade is that I was running the
code from the KeyPress_Event BUT hade the Locked propertie set for the
control.

Is there any way around this because I realy would like to have the
propertie Locked = True on all the controls accept for one. The form is only
a list of records and I don´t want the user to be able to edit the records
from this form (frmListMembers). If the user need to edit the records he/she
just needs to open up (frmPerson) which I want to open with help of Return
key!

TIA!
// Niklas


"Neil" <neil@_NoSPAM_haywood2k3.freeserve_NoJUNK_.co.uk> skrev i meddelandet keypress
to
time
of
the user pressing enter. For example:

You have a form named frmTest. This has a textbox on it named txtMyTextBox
and a ComboBox named cboMyCombo. If you had programed the KeyPress
event
for
the textbox only then the code would look like:

Private Sub txtMyTextBox_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime the user pressed *any* key in the textbox.
Your code checks and only acts if the Return key has been pressed. If you
put the same procedure in the forms KeyPress event (KeyPreview has to
be
set
to yes for this to work as the form doesnt receive the focus unless there
are no controls on it - not much use then :)).

Private Sub Form_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime any key was pressed in any control
on
the
form. Again, the form would only open if it was the Return key that
had
been
pressed.

Hopefully, using the above information, you can now work out where you need
to place the code and in which controls (if any) to get the desired effect
you are after.

HTH,

Neil.

Hmm!

I havn´t tryed it out but I was to curious to wait untill late evening!
I don´t know if I have missunderstod your reply but the DoCmd.OpenForm
works. What I don´t get is how to fire the code when <Enter /
Return>
key
is
pressed. That´s my Q!

TIA
// Niklas


"Albert D. Kallal" <[email protected]> skrev i meddelandet
I guess it depends on where your current cursor is..or what is currently
displayed on the screen.

But yes..it is a great idea to enable the Enter key to open up a form
based
on the current details.

If you look at he following screen shots....I always enable the cursor
when
the user hits enter.

http://www.attcanada.net/~kallal.msn/Articles/Grid.htm

The code use is:

Private Sub MNr_KeyPress(KeyAscii As Integer)
If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if
End Sub

of course, the above assumes a key id of personID. Often, the key
id
is
"id"..so you would use:

docmd.OpenForm "frmPerson",,,"id = " & me.ID

Here is some screen shots where I use the above idea..and again enter
key
opens up the form...

http://www.attcanada.net/~kallal.msn/Search/index.html

--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.attcanada.net/~kallal.msn
 
N

Neil

Glad it worked.
I have had that same headache myself many times before :)

Neil.

Niklas Östergren said:
THANK´S a lot Niel!

It works like a charm! Just the way I wanted!
You have made my day and besides my headache have dissapeared. Have had it
all day but now it´s gone! ;-)

// Niklas


"Neil" <neil@_NoSPAM_haywood2k3.freeserve_NoJUNK_.co.uk> skrev i meddelandet
Right,

I have looked into this and I think this is because you are using the
KeyPress event. In my DB I use the KeyDown event (which works when the
control is locked but not when it is disabled - as you cant get into it
obviously :)). The two events are near enought the same except the KeyDown
event repeats when the key is held down and there is another agument passed
in 'Shift'. Shift is used to determin if any of the special keys were
pressed down (eg ctl or shift or alt). My code looks like this:

Private Sub txtTest_KeyDown(KeyCode As Integer, Shift As Integer)

Dim intCtrlDown As Integer

' Use bit mask to determine which key was pressed.
intCtrlDown = (Shift And acCtrlMask) > 0

If intCtrlDown Then
If KeyCode = vbKeyF10 Then
MsgBox "In Key Down"
End If
End If

End Sub

Even when the test textbox is locked, the message box is still displayed.
Give it a try and see if it solves your problem.

HTH,

Neil.

Niklas Östergren said:
Hi Niel!

Thank´s a lot! It works now! The problem I hade is that I was running the
code from the KeyPress_Event BUT hade the Locked propertie set for the
control.

Is there any way around this because I realy would like to have the
propertie Locked = True on all the controls accept for one. The form
is
only
a list of records and I don´t want the user to be able to edit the records
from this form (frmListMembers). If the user need to edit the records he/she
just needs to open up (frmPerson) which I want to open with help of Return
key!

TIA!
// Niklas


"Neil" <neil@_NoSPAM_haywood2k3.freeserve_NoJUNK_.co.uk> skrev i meddelandet
Niklas,

Every Access control (as far as i know) has a KeyPress event. The forms
actually have a KeyPreview property which will in turn send the keypress
to
the KeyPress of a form no matter what control has the focus at the
time
of
the user pressing enter. For example:

You have a form named frmTest. This has a textbox on it named txtMyTextBox
and a ComboBox named cboMyCombo. If you had programed the KeyPress event
for
the textbox only then the code would look like:

Private Sub txtMyTextBox_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime the user pressed *any* key in the
textbox.
Your code checks and only acts if the Return key has been pressed.
If
you
put the same procedure in the forms KeyPress event (KeyPreview has
to
be
set
to yes for this to work as the form doesnt receive the focus unless there
are no controls on it - not much use then :)).

Private Sub Form_KeyPress(KeyAscii As Integer)

If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if

End Sub

The code above would run evertime any key was pressed in any control on
the
form. Again, the form would only open if it was the Return key that had
been
pressed.

Hopefully, using the above information, you can now work out where you
need
to place the code and in which controls (if any) to get the desired effect
you are after.

HTH,

Neil.

Hmm!

I havn´t tryed it out but I was to curious to wait untill late evening!
I don´t know if I have missunderstod your reply but the DoCmd.OpenForm
works. What I don´t get is how to fire the code when <Enter / Return>
key
is
pressed. That´s my Q!

TIA
// Niklas


"Albert D. Kallal" <[email protected]> skrev i meddelandet
I guess it depends on where your current cursor is..or what is
currently
displayed on the screen.

But yes..it is a great idea to enable the Enter key to open up a form
based
on the current details.

If you look at he following screen shots....I always enable the cursor
when
the user hits enter.

http://www.attcanada.net/~kallal.msn/Articles/Grid.htm

The code use is:

Private Sub MNr_KeyPress(KeyAscii As Integer)
If vbKeyReturn Then
docmd.OpenForm "frmPerson",,,"[PersonID]=" & intPersonID
end if
End Sub

of course, the above assumes a key id of personID. Often, the
key
 

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