SetFocus problem

B

Beetle

Alright, this is driving me nuts. I have an unbound text box (txtAddInvoices)
on a form (A2003) where a user can enter an invoice number and the code in
the After Update event creates a recordset clone and looks for the invoice
number. If a match is not found it displays a message box. It works fine
except that I cannot get it to set focus back to the text box after the code
runs. Here is the current code (slightly condensed);

Private Sub txtAddInvoices_AfterUpdate()

Dim rst As DAO.Recordset
Dim strCriteria As String
Dim strMsg As String

Set rst = Me.RecordsetClone
strCriteria = "[InvoiceNumber] = '" & Me.txtAddInvoices & "'"
strMsg = "The invoice number you" & vbCrLf
strMsg = strMsg & "entered was not found." & vbCrLf & vbCrLf
strMsg = strMsg & "Either it doesn't exist or it is" & vbCrLf
strMsg = strMsg & "already assigned to a driver."

rst.FindFirst (strCriteria)

If rst.NoMatch Then
MsgBox strMsg, vbOKOnly + vbExclamation, "Invoice Not Found"
Me.txtAddInvoices.SetFocus '<<<this doesn't work
Exit Sub
End If

(code to do something else if a match is found)

End Sub

I've tried putting the set focus event in different places within the
procedure, but no luck. I can get it to set focus to the text box from
procedures in other controls, but it will not set focus back to the text box
from it's own After Update event. I'm probably just being an idiot and
missing something simple, but right now I'm just not seeing it. I'm sure a
couple of cold ones will help as soon as I get home, but in the meantime,
could someone please slap me upside the head and point out what I'm doing
wrong?

Thanks in advance
 
S

Stuart McCall

Beetle said:
Alright, this is driving me nuts. I have an unbound text box
(txtAddInvoices)
on a form (A2003) where a user can enter an invoice number and the code in
the After Update event creates a recordset clone and looks for the invoice
number. If a match is not found it displays a message box. It works fine
except that I cannot get it to set focus back to the text box after the
code
runs. Here is the current code (slightly condensed);

Private Sub txtAddInvoices_AfterUpdate()

Dim rst As DAO.Recordset
Dim strCriteria As String
Dim strMsg As String

Set rst = Me.RecordsetClone
strCriteria = "[InvoiceNumber] = '" & Me.txtAddInvoices & "'"
strMsg = "The invoice number you" & vbCrLf
strMsg = strMsg & "entered was not found." & vbCrLf & vbCrLf
strMsg = strMsg & "Either it doesn't exist or it is" & vbCrLf
strMsg = strMsg & "already assigned to a driver."

rst.FindFirst (strCriteria)

If rst.NoMatch Then
MsgBox strMsg, vbOKOnly + vbExclamation, "Invoice Not Found"
Me.txtAddInvoices.SetFocus '<<<this doesn't work
Exit Sub
End If

(code to do something else if a match is found)

End Sub

I've tried putting the set focus event in different places within the
procedure, but no luck. I can get it to set focus to the text box from
procedures in other controls, but it will not set focus back to the text
box
from it's own After Update event. I'm probably just being an idiot and
missing something simple, but right now I'm just not seeing it. I'm sure a
couple of cold ones will help as soon as I get home, but in the meantime,
could someone please slap me upside the head and point out what I'm doing
wrong?

Thanks in advance

Use the BeforeUpdate event instead of AfterUpdate. You then have the ability
to both cancel the record update and prevent the user from "moving off" the
control till either the problem is fixed or the user presses escape, thus
undoing the change.
 
M

mac

I've had a similar problem. If you issue two setfocus actions in a row it
works:
Me!txt1.setfocus
Me!txt1.setfocus
...

Don't know why this works when one setfocus doesn't, but it's worth a try.
 

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

Similar Threads

NotInList not firing ? 1
Help With Code Please 5
Combo Box to add items to a table 4
Unwanted Message Box 2
Not In List help needed 1
Not in list not working 1
Not in list requery issue 4
NotInList Problem 3

Top