SetFocus not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hawaiijeff
    New Member
    • Oct 2007
    • 22

    #1

    SetFocus not working

    I am writing an order receiving application in Access 2000. In this app you type in the tanker number the app looks that up in a table and should return just one row. The code below is showing how I am handling the errors of more than one row returned or no rows returned. After an error is handled I would like the focus to be returned to my tanker_txtbx so that the user can enter a new tanker number, but for some reason the application simply goes to the next box in the tab order. I have tried setting the focus to some other text boxes in the form and it goes to them, it just won't go to the one text box that I want it to go to. The code is listed below:

    Code:
    If rstTanker.RecordCount > 1 Then
    'More than one record
    MsgBox "Multiple records found with tanker number " & tanker_txtbx.Value & ".  Unable to process automatically"
    tanker_txtbx.Value = Null
    tanker_txtbx.SetFocus
    rstTanker.Close
    
    ElseIf rstTanker.RecordCount < 1 Then
    'No records returned because tanker # is not instransit
    MsgBox "No record found for tanker number " & tanker_txtbx.Value & ".  Make sure you entered it correctly"
    tanker_txtbx.Value = Null
    tanker_txtbx.SetFocus
    rstTanker.Close
    
    Else
    'set textboxes to cursor values
    lbs_per_berk_txtbx = rstTanker("LBS_PER_BERK")
    ship_date_txtbx = rstTanker("SHIP_DATE")
    snf_pct_per_berk_txtbx = rstTanker("SNF_PCT_PER_BERK")
    bf_pct_per_berk_txtbx = rstTanker("BF_PCT_PER_BERK")
    End If

    Any insight to the SetFocus command would be helpful. Thanks.

    Jeff
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What event is this code in?

    Comment

    • hawaiijeff
      New Member
      • Oct 2007
      • 22

      #3
      tanker_txtbx_Af terUpdate()

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        The AfterUpdate event happens before focus is set to the next control if they use [Enter] or [Tab]. So, if they hit [Enter], After Update triggers and then it sets control to the next tab stop, a few events happen at this time. The Lost Focus event of the current control and the Got Focus event of the next control. When you set focus in the After Update event, it doesn't stop the focus from passing to the next control.

        To control where the focus goes if they use [Enter] or [Tab], you have to use the Lost Focus event.

        Comment

        • hawaiijeff
          New Member
          • Oct 2007
          • 22

          #5
          Thank you for the lesson on SetFocus, I had a feeling something like that was happening. I wrote the following to handle this and it's still not returning focus to the tanker_txtbx textbox.

          Code:
          Private Sub tanker_txtbx_LostFocus()
          MsgBox IsNull(tanker_txtbx.Value)
          If IsNull(tanker_txtbx.Value) Then
          tanker_txtbx.SetFocus
          End If
          
          End Sub
          I put the message box in there to make sure the sub was being executed and everything was operating as expected. It IsNull() evaluates to true when I would expect it to, and to false when I would expect it to, but it still does not return focus to tanker_txtbx. Am I not understanding the LostFocus() event?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Hmm, I see no explicit reason why it shouldn't work. I think it may lie with the properties of the textbox you're trying to set focus to. Check the locked and enabled properties tanker_txtbx. I don't remember which one but if one of them is set incorrectly, then you can't set focus to that control. I would also check to see if you have any code on that control or code anywhere else that might be changing those properties.

            Comment

            • Brewer
              New Member
              • Oct 2007
              • 1

              #7
              I had the same problem you describe, the validation would kick in and the message box would display, but I couldn't get the SetFocus to send the cursor back to the field of error (I could set it to other fields as well). The Enabled value was Yes and the Locked value was No, so I believe those to be set correctly.

              My solution was to put my validation code under the Got Focus event of the next field in the tab order. The SetFocus action would then take me back to the correct (previous) field. Don't know if this is the proper solution to the problem, but it worked for me. Hope this helps.

              Comment

              • missinglinq
                Recognized Expert Specialist
                • Nov 2006
                • 3533

                #8
                Another workaround to this problem, when it occurs, is to set focus to any other control, then immediately send focus to the desired control.

                Welcome to TheScripts!

                Linq ;0)>

                Comment

                • hawaiijeff
                  New Member
                  • Oct 2007
                  • 22

                  #9
                  Thanks for everyone's suggestions. I finally got it to work by checking the properties. I always seem to go for the complicated reason first instead of looking at something simple like the properties.

                  Comment

                  Working...