Solved

Run code when enter key is pressed

Posted on 2013-01-29
5
1,671 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
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 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) 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 47

Expert Comment

by:Dale Fye (Access MVP)
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

914 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

13 Experts available now in Live!

Get 1:1 Help Now