Consulting

Results 1 to 3 of 3

Thread: SetFocus Method Not working

  1. #1
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    1
    Location

    SetFocus Method Not working

    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
    Last edited by lethal; 05-06-2010 at 10:47 PM.

  2. #2
    VBAX Regular JONvdHeyden's Avatar
    Joined
    Mar 2009
    Location
    Hampshire, UK
    Posts
    75
    Location
    Hello

    Try;

    [VBA]
    Else
    MsgBox ("Not a valid consignment Number. Please rescan!")
    TextBox1.Value = ""
    Cancel = True
    [/VBA]
    Regards,
    Jon von der Heyden (Excel 2003, OS XP Pro)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Lethal
    Welcome to VBAX
    Use the BeforeUpdate event rather than Exit
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •