Results 1 to 28 of 28

Thread: Help with Focus/Lost Focus

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    41

    Help with Focus/Lost Focus

    Here is the code another member gave me.


    Private Sub TextBox1_Change()
    If Val(TextBox1.Text) < 1 Or Val(TextBox1.Text) > 5 Then
    MsgBox "Please enter a number between 1 and 5".
    TextBox1.SetFocus
    End If
    End Sub

    That works fine, it limits users from entering values less than one and greater than 5 but if the user simply presses enter or tab the box remains blank.

    I have tried several ways to capture this event. Is this a problem with the AutoTab feature?

    Thanks.

  2. #2
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Help with Focus/Lost Focus

    Try using the LostFocus event :

    Code:
    Private Sub Text1_LostFocus()
        If Val(Text1) < 1 Or Val(Text1) > 5 Then
            MsgBox "Valid Values between 1 and 5 !"
            Text1.SetFocus
        End If
        
    End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    41

    Re: Help with Focus/Lost Focus

    There is no lostfocus event in excel vba.

    Is there another way to do this?

  4. #4
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Help with Focus/Lost Focus

    ummm .... I hate to disappoint you but Im afraid there is.

    I am presuming you are using a textbox, based on your code above.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    41

    Re: Help with Focus/Lost Focus

    I tried to write my own

    Private Sub Textbox1_lostfoucs()

    and it did not work ... any reasons?

  6. #6
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Help with Focus/Lost Focus

    Ummm ..... you've spelt LostFocus as "lostfoucs"

    Just as a matter of interest ..... if you double click your textbox whilst in design mode this takes you to the default "Change" event for a textbox.

    If you use the pull-down box in the VBA environment that lists all the events for the textbox can you not see "LostFocus" listed ?

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    41

    Re: Help with Focus/Lost Focus

    OK... Somethings up. I don't have a mouseover event.

    What should I use instead.

    I want a label to change color when the mouse is over it.

    That is all.'

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help with Focus/Lost Focus

    Use the mousemove event.
    VB Code:
    1. Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    2.  
    3. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width