Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2004
    Location
    Sunshine Coast, Australia
    Posts
    52

    Unhappy Unanswered: SetFocus Problem

    Hi, I have the following code:

    Private sub Username_LostFocus()

    if len(username.value)<2 or len(username.value)>12 then

    msgbox "Username must be between 2 and 12 characters!"

    username.setfocus

    end if
    end sub

    The problem is that if the username length is not between 2 and 12 characters (or has a 0 length) it wont set the focus to the username field but will move the focus to the password field (next tab sequence).

    When I put a msgbox to report the error number and description (using msgbox err.number & " " & err.description) I got the following information: 0

    so 0 tells me no error has opccured, so why is the focus not returned to the username field?

    Please help me.

    - Mark

  2. #2
    Join Date
    Feb 2004
    Posts
    533
    This is because Null values are not treated the same as numbers or text in a txt field. There's ways to handle like n = nz(fieldname, "0") or If isNull(txtname) Then...

    Code:
     
    
    Private sub Username_LostFocus()
     Dim strUser as String
     Dim nUserLen as Integer
     Dim bPassuser as Boolean
    
    strUser = Nz(Me.username, "")
    nUserLen = len(strUser)
    
    bPassuser = (nUserLen > 2 AND nUserLen < 12)
    
    if Not bPassuser then
        msgbox "Username must be between 2 and 12 characters!"
        username.setfocus
    end if
    
    end sub
    ~

    Bill

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    441
    Because it can't set focus to itself while it has focus.

    The dirty way around, is to setfocus to another control first, then back

    Me!txtSomeOtherControl.setfocus
    Me!username.setfocus

    The proper way, would be to use the before update event of the control for the validation, then use Cancel=True to cancel the event, effectively not allowing the user out of the control until the criterion is met.
    Roy-Vidar

  4. #4
    Join Date
    May 2004
    Location
    New York State
    Posts
    1,178

    L

    [QUOTE=milks1977]Hi, I have the following code:

    Private sub Username_LostFocus()

    if len(username.value)<2 or len(username.value)>12 then

    msgbox "Username must be between 2 and 12 characters!"

    username.setfocus

    end if
    end sub

    End Quote

    Did you ever notice how the underlying SQL code behind a query adds parentheses that seem almost useless? There's a reason for it. Whenever you have more than two terms (such as x=y, where the two terms are x and y), the program can misread your code. SQL adds the parentheses to force the reading of your code. Adapting the above to your code, try the following:

    if (len(username.value)<2) or (len(username.value)>12) then

    The addition of parentheses in this manner forces the program to "see" only two terms and might read your code the way you want it to.

    Sam

  5. #5
    Join Date
    Mar 2004
    Location
    Berlin, Germany
    Posts
    107
    have a look at the "Exit" event

    it "fires" before the focus moves from a control not after like lostFocus.
    you can hinder setting the focus to any other control by setting the Cancel parameter to true.

  6. #6
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926

    SetFocus

    RoyVidar is right. You need to set the focus to another control and then back to that control. The Exit event might work but I haven't tried it that way.
    Last edited by pkstormy; 02-28-05 at 16:59.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  7. #7
    Join Date
    Dec 2004
    Location
    Sunshine Coast, Australia
    Posts
    52
    I decided to go with the beforeupdate event, no other example worked for me but this fixed the problem.

    it puzzles me because throughout the code I use the setfocus command and it works. it just didnt with this form. I had no control source and all text boxes were unbound.

    thank u everyone for all your help and suggestions.

    - mark

Posting Permissions

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