setfocus not working?

B

bigomega73

I have VBA code which is run in the AfterUpdate event for a text box. It
basically checks to see if the data entered in the text box matches a
criteria. If not, it displays an message box asking if they want to continue.
If not, I want the text box to be re-selected so that they can change the
value. The following is my VBA, which uses SetFocus to re-select the text
box. But for some reason, if I try to SetFocus back on the text box, it is
completely ignored and the form moves on to the next field. However, if I
SetFocus on a different control it will do it just fine. So, I tried to
SetFocus on a different control and then back to the original text box, which
works, but instead of selecting the whole field so that the user can just
re-type their data, the cursor moves to the end of the data so that they user
first has to delete the previous data before re-entering the new data. If I
SetFocus on any other text box, the whole data field is selected like I want.
AARRRRGHHH!!! This is driving me crazy and I have not idea how to work around
this. Can someone please help?

Here's my VBA:

Private Sub TextBox1_AfterUpdate()
Dim TextBoxData As String, Response As Integer

TextBoxData = Me!TextBox1

If TextBoxData <> "Correct Field Data" Then
Response = MsgBox("That is not Correct Field Data. Do you want to
continue?", vbYesNo, "Incorrect Field Data")
If Response = vbNo Then
Me!TextBox1.SetFocus
End If
End If


End Sub
 
D

Dirk Goldgar

bigomega73 said:
I have VBA code which is run in the AfterUpdate event for a text box. It
basically checks to see if the data entered in the text box matches a
criteria. If not, it displays an message box asking if they want to
continue.
If not, I want the text box to be re-selected so that they can change the
value. The following is my VBA, which uses SetFocus to re-select the text
box. But for some reason, if I try to SetFocus back on the text box, it is
completely ignored and the form moves on to the next field. However, if I
SetFocus on a different control it will do it just fine. So, I tried to
SetFocus on a different control and then back to the original text box,
which
works, but instead of selecting the whole field so that the user can just
re-type their data, the cursor moves to the end of the data so that they
user
first has to delete the previous data before re-entering the new data. If
I
SetFocus on any other text box, the whole data field is selected like I
want.
AARRRRGHHH!!! This is driving me crazy and I have not idea how to work
around
this. Can someone please help?

Here's my VBA:

Private Sub TextBox1_AfterUpdate()
Dim TextBoxData As String, Response As Integer

TextBoxData = Me!TextBox1

If TextBoxData <> "Correct Field Data" Then
Response = MsgBox("That is not Correct Field Data. Do you want to
continue?", vbYesNo, "Incorrect Field Data")
If Response = vbNo Then
Me!TextBox1.SetFocus
End If
End If


End Sub


In the AfterUpdate event, Access has already determined where the focus is
going to go next, but hasn't yet moved the focus there. So it does no good
to set the focus back to Textbox1 in that event, because it will still move
on to the next control as soon as that event is over.

For validation of the user's entry in the text box, use the control's
BeforeUpdate event, not its AfterUpdate event. Then cancel the event if it
fails validation, by setting the event procedure's Cancel argument to True.
Like this:

'------ start of code ------
Private Sub TextBox1_BeforeUpdate(Cancel As Integer)

Dim TextBoxData As String, Response As Integer

TextBoxData = Me!TextBox1

If TextBoxData <> "Correct Field Data" Then

Response = MsgBox( _
"That is not Correct Field Data. Do you want to continue?", _
vbYesNo, _
"Incorrect Field Data")

If Response = vbNo Then
Cancel = True
End If

End If

End Sub
'------ end of code ------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top