On got focus event - i'm having a dumb day!

connexion

Registered User.
Local time
Today, 19:23
Joined
Jul 30, 2003
Messages
72
I reckon i must be losing my marbles or i'm just being lazy (probably the latter).

I've got a product search form with a text box (for the search string) and a "Find" button to perform the search when clicked.

The user types a string then clicks enter. The focus moves to the find button and it's on got focus property clicks the button to perform the search.

Great so far BUT...

I want to be able to click the same find button with a mouse too. When this happens the search runs twice because getting the focus runs it once and clicking the button runs it again!

Before i go making any changes does anyone have a super slick way of getting round this?

Vince
 
Personally I'd only have the code in the Button's On Click event and not the Got Focus event.
 
No error handling necessary, no Lost focus code necessary.

Select the button in while in design view of the form and change the DEFAULT property of the button to YES from NO. Then, if your user types and hits the Enter(Return) key, the button will click and run the code.

You can only have one button set as default on a form, which is understandable given what it does.

You can also set a button's CANCEL property to YES if you want to click a button when the Escape key is pressed. I don't believe you can set one button to be both Default and Cancel, but I've never tried, nor have I thought of a situation where I would need to.

And as JBB said, the code would go in the ON CLICK event of the button.
 
Last edited:
I hear what's been said with regard to putting the code behind the on click event but if the button can't be the default button doesn't doing that mean the user has to press enter twice, once to move the focus to the find button and then again to click it?

The form is a multi use search tool which can be used in several "modes", with additional controls added depending on the mode used. I'll check but making the find button the default button might not be possible.

In the meantime, here's my "stab in the dark", does it seem a bit too naff?

1) Insert another field (specifically to receive the focus).
2) In this field's "on got focus" run the search (now a function).
3) After the function runs pass the focus to the find button.
4) Call the function from the find button's on click event instead of from the on got focus event

Visually the user sees exactly the same thing happen but the code only runs once in either case.
 
Thanks guys.

I have checked the form and set the button as the default to run the code when enter is clicked. The code was in the on click event in any case, the on got focus event just clicked the button.

I've got a "thing" about relying on "one offs" like this in that they're not available if needed as a solution to a later problem, but what the heck, may as well use them sometime.

Thanks

Vince
 
Setting a button as default to run an operation when someone hit enter is not a "one off" solution. It is a common navigation method which mirrors how web pages work. In fact, you can change which button is the default by using code. For example, if I enter something in one text box, I can use that text box's Got Focus event to set one button's default property to true and then if I click in another text box I can use that text box's Got Focus event to set a different one's Default property to true (I tested this).
 
In fact, you can change which button is the default by using code. For example, if I enter something in one text box, I can use that text box's Got Focus event to set one button's default property to true and then if I click in another text box I can use that text box's Got Focus event to set a different one's Default property to true (I tested this).

A working example of such... For a particular field, I set a custom button to receive the Enter key event.

Code:
'Only for fldcmdtycodetypetitle, configure a different behavior when Enter key is pressed
Private Sub fldcmdtycodetypetitle_GotFocus()
  On Error GoTo Err_fldcmdtycodetypetitle_GotFocus

  Me.btnCommit.Default = False
  Me.btnPickCommodityCode.Default = True

Exit_fldcmdtycodetypetitle_GotFocus:
  Exit Sub

Err_fldcmdtycodetypetitle_GotFocus:
  Call errorhandler_MsgBox("Form: Form_partsedit, Subroutine: fldcmdtycodetypetitle_GotFocus()")
  Resume Exit_fldcmdtycodetypetitle_GotFocus

End Sub

Private Sub fldcmdtycodetypetitle_LostFocus()
  On Error GoTo Err_fldcmdtycodetypetitle_LostFocus

  Me.btnPickCommodityCode.Default = False
  Me.btnCommit.Default = True

Exit_fldcmdtycodetypetitle_LostFocus:
  Exit Sub

Err_fldcmdtycodetypetitle_LostFocus:
  Call errorhandler_MsgBox("Form: Form_partsedit, Subroutine: fldcmdtycodetypetitle_LostFocus()")
  Resume Exit_fldcmdtycodetypetitle_LostFocus

End Sub
 

Users who are viewing this thread

Back
Top Bottom