Use Enter Key in addition to Command button?

  • Thread starter Thread starter SherryScrapDog
  • Start date Start date
S

SherryScrapDog

I have a command button that brings up the next form using an entered Last
Name to select the records. Is it possible for me to also use the Enter key
on the field where I have entered the Last Name to bring up the next form?

Thanks, Sherry
 
SherryScrapDog said:
I have a command button that brings up the next form using an entered Last
Name to select the records. Is it possible for me to also use the Enter
key
on the field where I have entered the Last Name to bring up the next form?

Thanks, Sherry

Set the "Default" property (it's on the 'Other' tab) for your command button
to Yes. This causes Access to "click the button" when you press Enter or
Return.
 
Hi Stuart,
Thanks!!! Such wonderful little things I don't know yet, but am always
learning. This worked great on my form with one command button.

I have another form that has multiple command buttons:
Enter Last name [ ] Command button
Enter First name [ ] Command button
Enter Soundex [ ] Command button

When I changed all of the command buttons, they all try to do the Soundex
option of my searches (when pressing enter). Is there a way to have each
command know which entry field it belongs to?

Thanks so much, Sherry
 
SherryScrapDog said:
Hi Stuart,
Thanks!!! Such wonderful little things I don't know yet, but am always
learning. This worked great on my form with one command button.

I have another form that has multiple command buttons:
Enter Last name [ ] Command button
Enter First name [ ] Command button
Enter Soundex [ ] Command button

When I changed all of the command buttons, they all try to do the Soundex
option of my searches (when pressing enter). Is there a way to have each
command know which entry field it belongs to?

Thanks so much, Sherry

Stuart McCall said:
message


Set the "Default" property (it's on the 'Other' tab) for your command
button
to Yes. This causes Access to "click the button" when you press Enter or
Return.

If you look carefully at the Default property for all 3 buttons, you'll find
that only one of them is set to Yes. Ths is because you can only have one
Default button on a form.

Using this form, what do you expect to happen when you hit Return?
 
Hi Stuart,
Yes, I did notice that they were changing back to No and didn't know if I
was doing something wrong.

This is what would be nice: When entering a Last name and pressing the
enter key, it would execute the Last Name search button. Then, when entering
in the First name (separate) field, it would excecute the First name search
button. Then, finally, when entering the in the Soundex field, it would
execute the Soundex search button.

However, since the vast majority of the time they are entering the Last name
search, it will certainly be helpful to leave this one as the one that works
with the enter key. That's how I have it now, especially since I figured out
only one of them would work. I think I will put a note on the First and
Soundex search options that they must click the appropriate button.

My form basically looks like this:
Enter Last Name [enterlast unbound field] Search by Last Name button
Enter First Name [enterfirst unbound field] Search by First Name button
Enter Soundex [entersoundex unbound field] Search by soundex code button

Again, thanks so much for your help. This makes several forms more friendly
and this one will be too because they use the Last name search the most.
(Genealogy system in case you haven't guessed.)

Sherry
 
SherryScrapDog said:
Hi Stuart,
Yes, I did notice that they were changing back to No and didn't know if I
was doing something wrong.

This is what would be nice: When entering a Last name and pressing the
enter key, it would execute the Last Name search button. Then, when
entering
in the First name (separate) field, it would excecute the First name
search
button. Then, finally, when entering the in the Soundex field, it would
execute the Soundex search button.

However, since the vast majority of the time they are entering the Last
name
search, it will certainly be helpful to leave this one as the one that
works
with the enter key. That's how I have it now, especially since I figured
out
only one of them would work. I think I will put a note on the First and
Soundex search options that they must click the appropriate button.

My form basically looks like this:
Enter Last Name [enterlast unbound field] Search by Last Name button
Enter First Name [enterfirst unbound field] Search by First Name button
Enter Soundex [entersoundex unbound field] Search by soundex code
button

Again, thanks so much for your help. This makes several forms more
friendly
and this one will be too because they use the Last name search the most.
(Genealogy system in case you haven't guessed.)

Sherry

Ok, if you have multiple buttons you can use another technique. Forget the
Default property (set it back to No). You can trap the keyboard code pressed
in each textbox's OnKeyDown event, like this:

Private Sub txtLastName_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
KeyCode = 0 'This "throws away" the keypress
btnLastName_Click 'This clicks the appropriate button
End If
End Sub

Notice I've assumed the control names txtLastName and btnLastName. Change
these to your actual control names.
Do the same for the other two textbox/button pairs.
 
Ok, if you have multiple buttons you can use another technique. Forget the
Default property (set it back to No). You can trap the keyboard code pressed
in each textbox's OnKeyDown event, like this:

Private Sub txtLastName_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
KeyCode = 0 'This "throws away" the keypress
btnLastName_Click 'This clicks the appropriate button
End If
End Sub

Notice I've assumed the control names txtLastName and btnLastName. Change
these to your actual control names.
Do the same for the other two textbox/button pairs.
Hi Stuart,
This code works except it is not retaining the text I enter to filter for
the next form. So, when I enter Adams (for example), I'm getting the entire
database list of names. I tried a couple of different things like copying
some of the code from the Search button into the text onKeyDown code, but
that didn't work. Do I need to put something in the Search_Click code that
would look at the contents of what I entered? If you have ideas for this,
thanks for sharing!!! If not, thanks for all of the help you have given
because I did learn about the Default property and that is so very helpful
all by itself!
With much appreciation, Sherry
 
SherryScrapDog said:
Hi Stuart,
This code works except it is not retaining the text I enter to filter for
the next form. So, when I enter Adams (for example), I'm getting the
entire
database list of names.

The code I posted cannot cause the behaviour you describe. It will only
execute when Return is pressed while the textbox has input focus. It's
exactly the same as clicking the button mousily.
I tried a couple of different things like copying
some of the code from the Search button into the text onKeyDown code, but
that didn't work. Do I need to put something in the Search_Click code
that
would look at the contents of what I entered?

Well yes, I would imagine so. Perhaps you'd better let us see some code.
If you have ideas for this,
thanks for sharing!!! If not, thanks for all of the help you have given
because I did learn about the Default property and that is so very helpful
all by itself!

FYI, you may have noticed that every button also has a Cancel property. When
this is set to Yes/True then pressing the 'Esc' key will click this button.
It works just like the Default property, ie a form can only have one cancel
button.
 
Hi Stuart,
Here is the code for one of the search options: (The others are like this
with appropriate names.) The search-Click code is what I have been using
right along.
There must be something I am doing wrong, so maybe you can spot it. Thanks,
Sherry (p.s., you probably know this, but the chr(34) allows lookups of
names with commas, such as O'Brien.)

Private Sub search_Click()
On Error GoTo Err_search_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim stWhere As String

stDocName = "MasterWithFirst"


stLinkCriteria = "[Last] like " & Chr(34) & Me![Entry] & "*" & Chr(34)

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_search_Click:
Exit Sub

Err_search_Click:
MsgBox Err.Description
Resume Exit_search_Click

End Sub

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

If KeyCode = vbKeyReturn Then
KeyCode = 0 'This "throws away" the keypress
search_Click 'This clicks the appropriate button
End If

End Sub
 
SherryScrapDog said:
Hi Stuart,
Here is the code for one of the search options: (The others are like this
with appropriate names.) The search-Click code is what I have been using
right along.
There must be something I am doing wrong, so maybe you can spot it.
Thanks,
Sherry (p.s., you probably know this, but the chr(34) allows lookups of
names with commas, such as O'Brien.)

Private Sub search_Click()
On Error GoTo Err_search_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim stWhere As String

stDocName = "MasterWithFirst"


stLinkCriteria = "[Last] like " & Chr(34) & Me![Entry] & "*" & Chr(34)

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_search_Click:
Exit Sub

Err_search_Click:
MsgBox Err.Description
Resume Exit_search_Click

End Sub

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

If KeyCode = vbKeyReturn Then
KeyCode = 0 'This "throws away" the keypress
search_Click 'This clicks the appropriate button
End If

End Sub

Hmm. I just lashed up a form with a textbox and a button and ran a few
tests. It worked perfectly for me.

Your code looks fine. Not sure why it won't work for you.

Try creating a sub in the same module with just a MsgBox "Got here" in it,
then call it from one of the keydown events, instead of the current code
(comment it out). If that works then the problem can only lie with your
stLinkCriteria as far as I can see.
 
Thanks so much for all of your time and help. I will mess around with it
and see if I can get anything to work. This site has helped me with various
things and I keep learning! (self-taught).

Have a great night and THANKS! Sherry
 
Back
Top