gotfocus

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rudeman76
    New Member
    • Oct 2007
    • 58

    gotfocus

    Hello again,

    I have a form in columner style. I have the user enter in a area (dropdown) and a bag number. The program then searches the table to see if these are already added. If it is it gives an error, then is supposed to set the focus back on the bag number. Problem is it does not, it sets the focus on the next textbox. It does set the focus for about a half a second then goes to the next box.

    i have the code in the hole number got focus sub. I dont know if this is the wrong spot.
    Andrew
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You have to put it in the lost focus of area and bag number.

    Comment

    • rudeman76
      New Member
      • Oct 2007
      • 58

      #3
      I put it in the lost focus but it is still doing this. Any suggestions?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        What's the code you're using?

        Comment

        • rudeman76
          New Member
          • Oct 2007
          • 58

          #5
          Code:
          Private Sub Bag_No_LostFocus()
              Dim rst As Object
              Dim db As Object
              Dim intmsg As Integer
              
              Set db = CurrentDb
              Set rst = db.openrecordset("bags recieved")
              rst.MoveFirst
              Do While Not rst.EOF
                  
                  If rst("hole no").Value = Me.Hole_No.Value And rst("bag no").Value = Me.Bag_No.Value Then
                      If rst("area").Value = Me.Combo29.Value Then
                          intmsg = MsgBox("That bag and hole have already been used, you cannot add the bag again.", vbOKOnly, "Bag Error")
                          Me.Bag_No.SetFocus
                         Exit Sub
                      End If
                  End If
                  rst.MoveNext
              Loop
              rst.Close
              Set rst = Nothing
          End Sub
          The bags are brought to us as follows: LDD-070-5A or 120-07-007-5A (just an example). The problem I was having was that the people entering in the info were entering them in wrong. I now have it set up that the user selects the area (LDD, STW, or 120) from a drop down box. When they move over to enter in the hole number (the next set of numbers. either -##-### or -###) it changes the input mask accordingly. From there they enter in the bag number. So I have the system check the 3 sections accordingly. I have tried it with the exit sub and without. neither works. I hope this makes sense.

          Andrew

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            I can't find anything explicitly wrong with the code. Have you stepped through the code in debug mode to see if it's running and triggering correctly? I use a similar process on my forms to set focus if they violate a validation rule.

            The lost focus event is the event to use. Does the message box appear, it sets focus, and then the focus changes? This might be due to other code triggering. But if no other code is triggering, then there's no reason the code shouldn't work.

            Comment

            • rudeman76
              New Member
              • Oct 2007
              • 58

              #7
              I have stepped through and it seems to work fine there. The message box pops up. From there it sets the focus to the bag_no for like a half a second, then it goes on to the next box. I have similar code for the next box (which is tag_no) and it does the same thing. I will keep messin with it. If I get it figured out, i will let you know.

              Andrew

              Comment

              • rudeman76
                New Member
                • Oct 2007
                • 58

                #8
                I got it to work. I added a boolean in the lostfocus sub, i set it to false at the beginning of this sub and change it to true right before the exit sub line. I also took out the bag_no.setfocus line out. In the tag_no Got focus sub i added the following.
                Code:
                Private Sub Tag_No_GotFocus()
                    If blnTest = True Then
                        
                        Me.Bag_No.SetFocus
                    
                    End If
                End Sub
                Thank you Rabbit for all your help. It is greatly appreciated.

                Andrew

                Comment

                • missinglinq
                  Recognized Expert Specialist
                  • Nov 2006
                  • 3533

                  #9
                  There are a number of events in Access where common sense would tell you that a control no longer has focus, and thus you should be able to set focus on it. Unfortunately, things are not always as they seem! In LostFocus, OnExit and AfterUpdate (and maybe some others I didn't test) you cannot set focus to the object that belongs to the event! You have to move focus to another object and then back to the desired object. So for Bag_No you'd do something like this:

                  [CODE=vb] Private Sub BagNo_LostFocus ()
                  AnyOtherTextBox .SetFocus
                  Bag_No.SetFocus
                  End Sub[/CODE]

                  Linq ;0)>

                  Comment

                  Working...