Access 2002 - which button was clicked? (1 Viewer)

dcobau

Registered User.
Local time
Today, 22:44
Joined
Mar 1, 2004
Messages
124
Hi all,

maybe I am going about this the wrong way, if so I would appreciate some advice.

I have a popup form (frmSearchPubs) with a field (txtTitle) in which I enter a string to be used as the criteria for a record set to be displayed in another form. After the users enters the string they had to press the Enter key twice to activate the command button (btnSearch) that triggers the code to produce the filtered recordset.

I was asked to modify the procedure so that users had to press the Enter key only once so I put the same code that's on btnSearch on the OnExit event of txtTitle. Naturally the code is also triggered if I click on btnCancel if I want to cancel the search procedure.

I thought that if I could put in the OnExit event some code that can identify which button was clicked or activated I could then direct the code to do different things such as (the following code does not activate the procedure, but is used for testing purposes only):

If Screen.ActiveControl.Name = "btnCancel" Then
MsgBox "Cancel is pressed"
Else
MsgBox "Find is pressed"
End If

This does not work, I keep on getting "Find is pressed" no matter how I exit from txtTitle. Any ideas?

thanks

Dave
 

missinglinq

AWF VIP
Local time
Today, 08:44
Joined
Jun 20, 2003
Messages
6,420
It sounds like this form's sole purpose is to have the search text entered then conduct the search. If this is true, and the only purpose <Enter> will have in this form is to initiate the search, you can solve solve your problem by doing the following:

'This tells Access to check all special keys when they're pressed and see if an action other than their default action should be executed

Private Sub Form_Load()
Me.KeyPreview = True
End Sub



'This is where the programmer defines which special keys are to be treated differently, and what they are to do

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyReturn
'Place the search code you now have behind your btnSearch here
Case Else
End Select
End Sub


BTW, you can reassign the function of almost any special key; your Arrows, Function keys, Escape, etc.

Hope this helps.

The Missinglinq
 

Users who are viewing this thread

Top Bottom