PDA

View Full Version : SetFocus Method Not working



lethal
05-06-2010, 09:25 PM
Hi All,

I am trying to place the cursor in Textbox1 when the Else runs in the following VBA code however it won't go to Textbox1. Anyone got any suggestions? I have tried changing the SetFocus to Textbox2 and Textbox3 and that works fine... I just can't work out why it will not set the focus to Textbox1. Any help would be greatly appreciated!

The whole idea is that a bar code will be scanned into Textbox1.
- If the value that is received in Textbox1 is valid then the form will automatically move the cursor to Textbox2 ready to accept a scan event.
- If the value received in Textbox1 is not valid it should clear all text from Textbox1 and then put the cursor back in Textbox1 ready to accept a scan again.


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim conNotePrefix, consignmentNoIn, consignmentNo, leftFiveChars As String

consignmentNoIn = TextBox1.Value

consignmentNo = Mid(consignmentNoIn, 7, 13)
leftFiveChars = Left(consignmentNo, 5)
conNotePrefix = "69761"

' check to see that it is a valid consignment
If StrComp(leftFiveChars, conNotePrefix, vbTextCompare) = 0 Then
TextBox1.Value = consignmentNo
Else
MsgBox ("Not a valid consignment Number. Please rescan!")
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub

Cheers,
Leigh

JONvdHeyden
05-07-2010, 01:25 AM
Hello

Try;


Else
MsgBox ("Not a valid consignment Number. Please rescan!")
TextBox1.Value = ""
Cancel = True

mdmackillop
05-07-2010, 04:33 AM
Hi Lethal
Welcome to VBAX
Use the BeforeUpdate event rather than Exit
Regards
MD