Force Focus back to Control

bconner

Registered User.
Local time
Yesterday, 22:12
Joined
Dec 22, 2008
Messages
183
Hello All,

I have a text box where the user enters an invoice number and I am checking table "dbo_Tbl_AP" to make sure the invoice doesn't already exist. If the Invoice exists I want to show the label "lbl_Add_Duplicate_Invoice" and force the focus back to the text box "txtADD_Invoice". When I run the code below it shows the label when there is a duplicate but it's allowing the user to move the focus to another control.


Code:
Private Sub txtADD_Invoice_LostFocus()
 
Dim strDupInvoice As Variant

strDupInvoice = Nz(DLookup("[INVOICE NUMBER]", "dbo_Tbl_AP", "[INVOICE NUMBER] = " & Nz(Forms![Frm_Add_Additional_Invoices]![txtADD_Invoice], 0)), 0)
 
If strDupInvoice <> 0 Then
 lbl_Add_Duplicate_Invoice.Visible = True
 Me.txtADD_Invoice.SetFocus

Else

If strDupInvoice = 0 Then
 lbl_Add_Duplicate_Invoice.Visible = False
 
 
End If
End If
 
end sub
 
Use the control's BeforeUpdate so you can use Cancel argument to have the focus remain in the control.

Code:
Private Sub txtADD_Invoice_BeforeUpdate(Cancel As Integer)
 
Dim strDupInvoice As Variant
strDupInvoice = Nz(DLookup("[INVOICE NUMBER]", "dbo_Tbl_AP", "[INVOICE NUMBER] = " & Nz(Forms![Frm_Add_Additional_Invoices]![txtADD_Invoice], 0)), 0)
 
If strDupInvoice <> 0 Then
 lbl_Add_Duplicate_Invoice.Visible = True
 [COLOR=red][B]Cancel = True [/B][/COLOR]
Else
If strDupInvoice = 0 Then
 lbl_Add_Duplicate_Invoice.Visible = False
 
 
End If
End If
 
end sub

All validationcode should go in the BeforeUpdate event

JR
 
Just fyi: it's not moving because the Lost Focus event hasn't completed. If you move to another control and set focus back to the control it should work.

BUT.. I'm in support of JANR's method.
 
JANR thank you very much for your help, your solution worked perfectly.
vbaInet thank you for your advice as well.
 

Users who are viewing this thread

Back
Top Bottom