Solved

Run code when enter key is pressed

Posted on 2013-01-29
5
1,819 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 167 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 167 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 166 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

636 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