Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run code when enter key is pressed

Posted on 2013-01-29
5
Medium Priority
?
1,893 Views
Last Modified: 2013-01-29
Hi.  I am using access 2010

I have a textbox  whose name is -------------- fltrStocknum
I have a routine   whose name is -------------- SetFilter

The user would type in a number in  fltrStocknum and then hit the <enter> key.  

When this happens,   I would like to call the SetFilter routine.   Also,  the focus must remain on the fltrStockNum textbox.

 I do not wish to pass the value of fltrStockNum as a parameter to SetFilter.  Instead,  the SetFilter routine should be able to read the fltrStockNum textbox directly.


I have tried following code,  but it doesn't work because the SetFilter routine reads the value of fltrStockNum as "" at that moment.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If Screen.ActiveControl.Name = "fltrStocknum" Then
       If KeyCode = 13 Then SetFilter
    End If
End Sub


What is the best way to call the SetFilter routine when the user has hit the <enter> key.
0
Comment
Question by:peispud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 668 total points
ID: 38830743
<< SetFilter routine reads the value of fltrStockNum as "" >>

If fltrStocknum is currently in focus, you can use the Text property to get the data displayed in the box:

fltrStocknum.Text

Open in new window


See my article about Text vs Value here:

http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_10162-What-is-in-a-field-Value-vs-Text-and-the-differences-between-form-data-and-table-data.html
0
 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 668 total points
ID: 38830751
Well, you could put that in the AfterUpdate event of the textbox, but that would fire when they hit Enter, Tab, or click on some other control on your form.

Do you have the Forms "Key Preview" property (last property on the Event tab of the Properties dialog) set to Yes?
0
 
LVL 14

Assisted Solution

by:Bill Ross
Bill Ross earned 664 total points
ID: 38830758
Hi,

Use the AfterUpdate event - then you know the data is what the user is searching for...

Keeping the focus on the control seems strange to me.  How would a user exit the text box?

Regards,

Bill
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 38830763
I would also advise you to use the vbKeyReturn keycode constant rather than the 13 to make it easier to read your code.
0
 

Author Closing Comment

by:peispud
ID: 38830798
Thank you all.   The AfterUpdate event was all that I needed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question