Solved

Run code when enter key is pressed

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

790 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