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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lose focus

Status
Not open for further replies.

Beard36

Programmer
Sep 4, 2003
69
GB
Here's the situation - for one reason and another, I want to have a "Save Record now" button on a form. Which the large part of I've done. But I've got a problem (as usual :)

I do some validation on one of the controls on saving (it is a 10 digit number which has a check digit, uses a standard mod 11 method). However, if that control has the focus when I click save, the .value in that control can be different from the .text in there - the .value only gets updated when focus moves away from the control. At least I presume that's what's happening (?)

So, my question is - is there a way to force this update when I click save, either by taking focus off of the text box, or some other way?

BTW, the button I'm using is actually an image with a _click event as I wanted to use a pretty picture instead of a standard Windows format button, and I can't just give focus to the image (it gives me an error).

So am I missing something obvious here.
Any help much appreciated.

Cheers.
 
In the _Click event procedure of your image, give the focus to any non TextBox control.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
How are ya Beard36 . . . . .

If by chance you using the [blue]Validation Rule[/blue] property, this is one of the problems. The control focus will not be released until the rule is met.

Calvin.gif
See Ya! . . . . . .
 
Beard36

I guess the question is when do you validate the 10 + 1 digit number? Is it done with the AfterUpdate update of the field? Or do you do the same validation - once with the AfterUpdate for the field, and BeforeUpdate for the record event? (A function call would work best here, and failed entry should display an error message)

If the value is invalid, then I assume, your code resets the focus back to the problem field.

Okay, now we know the validated data has been validated.

When you click on the image / click event, focus should move away from the digit field to the click object.

Your update / insert record should work. If it does not, consider the following...

- Are you running the click event?
- Do you make it to the BeforeUpdate event (where the digit field is validated, again)?
- Is your validation routine for the field perventing you from moving beyond the field?

Let us know how this works out.
Richard
 
At the moment, the validation is only done when the button is pressed to save the record, so this shouldn't be preventing the focus from changing away from the entry box.

I think I've read somewhere that you can't give focus to an image - is this correct? And why would that be the case since you can click on it?

Thanks for the input so far!
 
Beard36 . . . . .
Beard36 said:
[blue]However, if that control has the focus when I click save, the .value in that control can be different from the .text in there[/blue]
Hmmmmm . . . and just how is it that this happens in your code?

Calvin.gif
See Ya! . . . . . .
 
Ok, here's a kind of cutdown version of what I'm doing to try to explain myself a bit better.

Say the entry form has three text boxes - Surname, FirstName and IDno. The user should fill these in and hit the Save button.

When the save button is pressed and the form has been filled in correctly, the record should be saved and the user should get no further information (the form will close).

However, if one or more of the boxes have not been filled in or the IDno has an invalid value, then I want a msgbox to appear with a "problems summary" (eg. "This form is incomplete - Surname has not been entered - IDno. is invalid"), and the form stays open until the user fills in the details correctly (or choose the Discard button which closes the form without saving).

Now to illustrate my problem originally posted: If I go through the input boxes and fill in Surname, move to FirstName, fill in FirstName, move to IDno, fill in IDno then click Save then the error summary appears saying that IDno has not been filled in. This is because the .value property of the IDno textbox is null. However, the .text property contains the number I've entered.

So instead of reading in the .value properties I could read in the .text properties, but to read .text you need to have the control in question in focus. (Right?)

So my workaround for the moment is - user clicks on Save, I move focus to the Surname textbox, then to the IDno textbox - this way, whichever control had focus when Save was pressed has lost it. Then I go ahead and check the validity of the IDno in code, check that the other boxes are filled in and proceed from there.

It's just that this feels like a bit of a cheap hack - why doesn't the .value property get updated to match the .text property when I click in Save (just as a reminder, the Save "button" is an Image with a _Click event, not an actual command button)?

Thanks to anyone that actually read all that and can maybe offer an explanation! ;)
 
OK Beard36 . . . .
Beard36 said:
[blue]So instead of reading in the .value properties I could read in the .text properties, but to read .text you need to have the control in question in focus. (Right?)[/blue]
The [purple]Text Property[/purple] is the [purple]current contents[/purple] of the textbox control, and is always current while the control has the focus. The [purple]Value Property[/purple] is the [purple]saved value[/purple] of the textbox control. Besides that these properties are more specific to other controls like combobox, option group, listbox and tab control.

Have you simply tried direct referencing ([purple]no focus required[/purple]), like:
[blue]Me!Surname
Me!FirstName
Me!IDno[/blue]

I see no reason they shouldn't work . . . . .



Calvin.gif
See Ya! . . . . . .
 
Still no luck..

I tried your suggestion and have basically the same problem -

Me!Surname will give the same as Surname.Value which is not always the same as Surname.Text as explained above.

:(
 
OK Beard36 . . . .

I setup the same thing and got the same results. Although the Image - Click Event triggers, the focus does'nt move. Since the focus doesn't move the control is still in edit mode!. This a new one on me . . . .

So, gotta get the focus moved to get the control out of edit mode . . . . . I always have a hidden texbox control for hiding the cursor when a form opens. Add a texbox control to the top-left corner of the form header. Name it [blue]ParkCsr[/blue]. Now set its width and height to 0.

In your save routine (before you read the controls) aadd the following line.

[blue]Me!ParkCsr.SetFocus[/blue]

Calvin.gif
See Ya! . . . . . .
 
You are correct in that an Image cannot receive the focus. So if you click on an image, the focus does not move because there is no place to move the focus to. That means that the AfterUpdate, Exit, and LostFocus events will not fire.

On the other hand, a command button can receive the focus, and clicking a command button does move the focus to the command button, provided that a validation rule does not override, thus causing those three events to fire from the control that is losing the focus.

What you're doing is not a cheap hack, it is a method of forcing the focus to move upon an event which normally will not cause the focus to move.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I like the idea of the "hidden" textbox - I had had a thought along a similar vein, but hadn't come up with a method to hide it, having it sized 0 by 0 would do the trick nicely.

So yeah, I think I'll stick with that plan.

Thanks a lot for your time all of you, especially AceMan, all much appreciated!

:)
 
Just in case anyone cares, I've got a follow up suggestion to this - I've been playing with using different images for button down / button up, and the best way to do this is to have the button's function not from the _click function of an image, but to overlay the image with a command button with .transparent set to true. This is just like setting a control to .visible false EXCEPT that the button is still enabled.

Anyone that's using the different images for button up/down will have come across this already, but for anyone trying to do what I was originally (just have a static image masquerading as a button), this information might be of use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top