Is it possible to set the focus on a control during BeforeUpdate event without cancelling the update?
Before updating the data in a form in MS Access 2007, I need to check the validity of the data. When setting the focus on a control to read its properties, the update event will always cancel, regardless if I set Cancel to true or not. I have trimmed my code and left just TextBox.SetFocus in the code, and this was enough to cancel the update. I know I can read a textbox's value without setting the focus on it; but I need开发者_StackOverflow社区 to set the focus for other properties as well. Is there a workaround for my problem? Is there anything that I might be missing? Thanks
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.LastName.SetFocus
End Sub
I don't know what you're doing to the textbox in your code (Maybe you're causing some other error that is getting ignored?), but you could save all of these values in variables in the form and set them on the textbox's onchange, before/after update events.
Any chance you could post code? Setting the focus of a textbox in the form's BeforeUpdate event did not cause it to cancel (I checked the value of cancel afterwards.).
Assuming you are working with the control's BeforeUpdate event (and not the form's), why not just move the code to the control's AfterUpdate
event?
The only thing you really lose is the ability to cancel the update of the control, but you could always implement that functionality yourself in the AfterUpdate event. You would just need to keep track of the previous value of the control. To do that you'd create a PrevVal
module level variable and set it to the control's value on the Form_Current event, the Form_Undo event, and as the final step in the control's AfterUpdate event. Then if the user enters invalid data you can roll back to the previous value in the first part of the control's AfterUpdate event.
精彩评论