+ Reply to Thread
Results 1 to 4 of 4

Set Focus

Hybrid View

rvc81 Set Focus 12-04-2010, 06:37 AM
snb Re: Set Focus 12-04-2010, 07:36 AM
Andy Pope Re: Set Focus 12-04-2010, 07:52 AM
rvc81 Re: Set Focus 12-04-2010, 12:01 PM
  1. #1
    Registered User
    Join Date
    11-29-2009
    Location
    dubai
    MS-Off Ver
    Excel 2003
    Posts
    56

    Set Focus

    hi,

    my user form has 3 text boxes

    Text Box 1 = uf_UserInfo.txt_UI_EmpName
    Text Box 2 = uf_UserInfo.txt_UI_EmpNameID
    Text Box 3 = uf_UserInfo.txt_UI_Dept

    If I use the below code then it works fine,

    If Trim(uf_UserInfo.txt_UI_EmpName.Value) = "" Then
      uf_UserInfo.txt_UI_EmpName.SetFocus
    
    MsgBox "Please enter the Relevent Information and click on Update"
    
    Exit Sub
    End If
    However; I cannot get 1 or all 3 criteria's to match. If I use the following code then I get an error? I am sure it is to do with 'SetFocus' method.

    
    If Trim(uf_UserInfo.txt_UI_EmpName.Value) Or Trim(uf_UserInfo.txt_UI_EmpNameID.Value )  Or Trim(uf_UserInfo.txt_UI_Dept.Value) = "" Then
        
    uf_UserInfo.txt_UI_EmpName.SetFocus
    uf_UserInfo.txt_UI_EmpNameID.SetFocus
    Else: uf_UserInfo.txt_UI_Dept.SetFocus
      
      MsgBox "Please enter the Relevent Information and click on Update"
      Exit Sub
    End If
    Please help.
    Thanks
    R

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Set Focus

    What is the essence of focus ?
    In optometrics doublefocus can be achieved, in VBA it can't.



  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Set Focus

    as snb points out only one control can have focus.
    You need to check each textbox and deal with the first empty one

    Private Sub CommandButton1_Click()
    
        Select Case 0
        Case Len(TextBox1.Text)
            MsgBox "Complete textbox 1"
            TextBox1.SetFocus
            Exit Sub
        Case Len(TextBox2.Text)
            MsgBox "Complete textbox 2"
            TextBox2.SetFocus
            Exit Sub
        Case Len(TextBox3.Text)
            MsgBox "Complete textbox 3"
            TextBox3.SetFocus
            Exit Sub
        End Select
        
        MsgBox "All textboxes populated"
        
    End Sub
    When you use code that check all the textboxes at once you will detect an empty one but you will not know which one to set focus to.
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
            MsgBox "Complete textboxes"
            TextBox1.SetFocus
            Exit Sub
        End If
        
        MsgBox "All textboxes populated"
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    11-29-2009
    Location
    dubai
    MS-Off Ver
    Excel 2003
    Posts
    56

    Re: Set Focus

    Thanks guys..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1