Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the enter key to invoke an event

Status
Not open for further replies.

tpowers

Technical User
Nov 2, 2002
153
US
Hello everyone, I have some code that allows the user to put data in one of three text boxs and then click a cmd button to get data back from the database, but what I am trying to do is allow the user to enter data into one of the fields and then press their enter button on their keyboard and get the same information back that they would get if they clicked the cmd button. is there a way to do this.


Note: I thought it was the on enter event but it did not work very well.

also I am using Access 2002 if that helps.

Thank you in advance,

TPowers
 
You could try putting the coding from your command button's OnClick event into each of the three field's AfterUpdate event. Whenever data is changed in one of the three fields and the user exits the field, the command will execute.
 
In the After Update event of the control that you're describing above, call the Command Button's On Click Event:

Call Command3_Click

The above is an example for a button called Command3.

Good Luck
 
Hey wemeier, that seems to work pretty good for those three text boxs, but here is one for you, when the data is returned to them it is in a datasheet view so that they can see any record with the criteria that they provided. Now currently I have them double clicking on a certian feild to bring up the details on that information, again the user has asked for the option just to press the enter key and get the same information.


Any ideas,

Thank you in advanced,

TPowers
 
First, they'd have to click on (or tab to) a field in one of the rows on the datasheet in order to press Enter to get further information.

You could add an Event Procedure to every field in the datasheet for the KeyDown event. Check for a KeyNumber of 13 (this is the code for Enter). If 13 is detected, show the information for whatever Me.<rowkey> is where <rowkey> is whatever key information you put in each row to identify that record.

For example:

Private Sub EmployeeNumber_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
DoCmd.OpenForm frmEmployeeInformation, , , &quot;Employee = &quot; & Me.EmployeeNumber
End If
End Sub

In my example, a field in the datasheet of the current form is named EmployeeNumber. If the user presses Enter in the EmployeeNumber column of the datasheet, a form is opened up to display information about that employee (where the field in the new form's RecordSource is Employee).

Using Me.EmployeeNumber tells Access to use the Employee number from the current row (the row on which Enter was pressed).
 
Please excuse my ignorance as I am a novice at this myself but wouldn't setting the button's default to &quot;Yes&quot; do the trick. If the enter key is pressed than it runs the code setup in the buttons event doesn't it?
 
Kaiana,

You're right, that's an easy way to do it. However, the code will execute whenever the user presses Enter -- even if the user has not selected a record from the datasheet!

By putting the coding on the datasheet fields, it ensures that the field had the focus when Enter was pressed.

Your solution will work just fine if there is absolutely nothing else on the form to get the focus.
 
Does any one know what the keycode is for the space bar?

Thank you in advance,

TPowers
 
You can use the constant vbKeySpace or the value 32.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top