Solved

Run code when enter key is pressed

Posted on 2013-01-29
5
1,643 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
5 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 167 total points
Comment Utility
<< 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 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 167 total points
Comment Utility
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
Comment Utility
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 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
Comment Utility
Thank you all.   The AfterUpdate event was all that I needed.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now