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

setfocus

Status
Not open for further replies.

ytakbob

Programmer
Jul 20, 2000
105
US
I have a afterupdate check of a birthdate field in a script and a fieldname.setfocus if the birthdate does not pass the edit. Yet the cursor does not position itself back to the field, it goes to the next field that I have tabbed to.
On beforeupdate, I get a 2108 error indicating I have to first make sure the field is saved before executing the setfocus

any suggestions welcomed and appreciated. Bob Schmid
bob_schmid@hmis.org
330-746-1010 ext. 1347
 
Hiya,

Just a suggestion, but it's not a good idea to 'tie' a user to a field because of a bad entry - which is your intention.
It's possible that they can never leave that field, I would instead: message the user and clear the field, but don't tie 'em to it.

I'm assuming that you are saying that you've tried your data check in both the before and after update event triggers (if not, what have you got in your beforeupdate?).

I think that you are setting focus to another field in another RECORD (the before/afterupdate events are executed only when you leave that record).

The beforeupdate error is correct - you're attempting to leave the record altogether BEFORE Access can 'save' the record change.

The afterupdate problem:
I would check that your logic in your vba code in the afterupdate event is correct by using something like:

if(check passed) then
msgbox "Passed"
else
msgbox "Failed"
endif

I believe that you'll get a "Passed" message.

Regards,

Darrylle





"Never argue with an idiot, he'll bring you down to his level - then beat you with experience."
 
Hiya,

Ignore my response above (apart from not tying the user to a field) - I've been checking this and I'm now assuming that you can't use setfocus in a 'beforeupdate' trigger at all.

Someone may well come along and solve it for you, but I'm still looking at it.

Regards,

Darrylle "Never argue with an idiot, he'll bring you down to his level - then beat you with experience."
 
Hi again,

I'm now sure that you can't use setfocus in the beforeupdate event because you would bypass the essential update attempt of the field and Access won't allow that.

I'm NOT sure about the afterupdate setfocus, but think it's because you're trying to set focus to the field from it's own event when it already has focus.

I suggest that you do the check in the Lostfocus event - that should solve your problem.

I still advise however that you simply nullify the field and message the user without setting focus.

Regards,

Darrylle "Never argue with an idiot, he'll bring you down to his level - then beat you with experience."
 
When you use the before update event for a control, one of the parameters passed is cancel. If your data fails one of the validation tests do something like the following:

if not valid_data then
msgbox "Invalid Data..."
cancel = true
exit sub
end if

Setting Cancel to true will keep the focus on the control in question, it doesn't matter what you clicked or pressed. Also, if you want to rollback the field changes, include controlname.undo in the above if statement.

Good Luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top