set the focus from text box to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hannoudw
    New Member
    • Aug 2010
    • 115

    set the focus from text box to another

    Hi i have a form that contain text boxes . I want when i just fill the 1st text box to set the focus on the second text box without pressing on "Enter".
    and when i fill the second one also to set the focus on the 3rd one . Till the end of the text boxes.

    My form contain: txtArticle must be 7 digit
    txtSize 2 digit
    txtQty (default value = 1)
    txtRealPrice (this on is locked)
    txtPriceAfterDi scount (this on is locked)
    txtTotalPrice

    I want when i enter 7 digit in the txtArticle to set the focus on the txtSize, and when enter 3 digit in the txtSize to set the focus on txtQty.
    And since the txtQty is by default 1 , i want to set the focus on the txtTotalPrice.

    I did try the txt.setfocus
    I managed to set the focus from txtArticle to txtSize to txtQty. but i couldn't set it to txtTotalPrice
    And this is the code hope that some one will help
    these are the on change and the after update event that i wrote for the article
    Code:
    if (len(me.txtArticle.text)=7) then
    txtSize.setFocus
    end if
    And that one is the on change and the after update event that i wrote for the size.
    Code:
    if (len(me.txtSize.text)=3) then
    txtQty.setFocus
    end if
    And in the txtQty i wrote this (on change , and on after update)
    Code:
    if (me.txtQty.value=1) then
    txtTotalPrice.setFocus
    end if
    But i keep getting this error : Can't get the focus on the txtTotalPrice.
    Many thanks for all of the great team . :)
  • munkee
    Contributor
    • Feb 2010
    • 374

    #2
    I would be checking to see if these are the correct names. Try using Me.controlname it will allow intellisense to kickoff so you know you are referencing the correct thing.

    Comment

    • hannoudw
      New Member
      • Aug 2010
      • 115

      #3
      do you mean to use me.txtname.setf ocus instead of txtname.setfocu s ??

      Comment

      • munkee
        Contributor
        • Feb 2010
        • 374

        #4
        yes just to atleast check you have the naming right

        Comment

        • hannoudw
          New Member
          • Aug 2010
          • 115

          #5
          Ok i wrote this now:
          Code:
          Private Sub Article_AfterUpdate()
          If (Len(Me.Article.Text) = 7) Then
          Me.size.SetFocus
          End If
          End Sub
          
          
          Private Sub Article_Change()
          If (Len(Me.Article.Text) = 7) Then
          Me.size.SetFocus
          End If
          End Sub
          
          Private Sub size_Change()
          If (Len(Me.size.Text) = 3) Then
          Me.quantite_vendu.SetFocus
          End If
          End Sub
          
          
          Private Sub size_GotFocus()
          If (Len(Me.size.Text) = 3) Then
          Me.quantite_vendu.SetFocus
          End If
          End Sub
          
          Private Sub quantite_vendu_BeforeUpdate(Cancel As Integer)
          If (Me.quantite_vendu.Value = 1) Then
          Me.prix_vente_unitaire.SetFocus
          End Sub
          when i enter the txtArticle i'm getting this error:
          Run-Time error 2110
          Microsoft office cant move the focus to the control size.

          And on debug this line from the Article_Change is marked in yellow :
          Code:
          Private Sub Article_Change()
          If (Len(Me.Article.Text) = 7) Then
          Me.size.SetFocus  ' this line is marked in yellow
          End If
          End Sub

          Comment

          • munkee
            Contributor
            • Feb 2010
            • 374

            #6
            Actually I have had a try at setting up your code and to be honest it is quite confused. I however think you have some circular reference style problem here.

            Basically if you have something entered in to size which then forces the focus to switch in to a different control, you will no longer be able to set focus back to size as it will always be trying to switch to a different control.

            If I were you I would step through the code after setting a breakpoint in a control and see how the code is running. But it does seem that you are telling the code to not allow focus in a control since it has a length greater than say 3 characters, but later are then trying to set focus back to it.

            Comment

            • munkee
              Contributor
              • Feb 2010
              • 374

              #7
              Had a mess around with your code and the problem is as I said, more importantly this is your problem code:

              Code:
              # Private Sub size_GotFocus()
              # If (Len(Me.size.Text) = 3) Then
              # Me.quantite_vendu.SetFocus
              # End If
              # End Sub

              You can not set focus back to size because you are making the focus of size move to a different box. Remove the size_gotfocus code and see how you get on with that.

              Comment

              • mshmyob
                Recognized Expert Contributor
                • Jan 2008
                • 903

                #8
                Could be because the word "Size" is a reserved key word. You should always preface your field names with the commonaly used prefixes (ie: lngSize, intSize, txtSize) etc.

                cheers,

                Comment

                • hannoudw
                  New Member
                  • Aug 2010
                  • 115

                  #9
                  @ munkee
                  i removed the
                  Code:
                  Private Sub size_GotFocus()
                  and now the focus is on the quantity.
                  I'm trying to set the focus on the txtTotalPrice by righting this code but it's not working
                  Code:
                  Private Sub quantite_vendu_GotFocus()
                  If (Me.quantite_vendu.Value = 1) Then
                  Me.prix_vente_unitaire.SetFocus
                  End If
                  End Sub
                  but i'm keep getting this message:
                  cannot move the focus to the prix_vente_unit aire!!
                  I'm wondering if it's because that i have 2 text box (before prix_vente_unit aire in tab order) and those 2 text boxes are enabled =No and locked =true!?? as i mentioned in #1.

                  And second can i move the focus directly to a new record in the form? the default view of the form is "Continuous Forms" . So can i move the focus from the txtqty directly to a new record , or to the txtarticle in the new record?
                  Thanks a lot for your time.

                  Comment

                  • hannoudw
                    New Member
                    • Aug 2010
                    • 115

                    #10
                    Hi
                    I changed the form to a unbound text boxes.
                    My form now contains 108 text box. (12 unbound record)

                    1st unbound record:
                    txtType1, txtInvoice_deta il_num1,txtArti cle1, txtSize1, txtQuantity1, txtPrice1,txtDi scountedPrice1, txtCashMoney1, txtDiscount1

                    2nd unbound record:
                    txtType2, txtInvoice_deta il_num2,txtArti cle2, txtSize2, txtQuantity2, txtPrice2,txtDi scountedPrice2, txtCashMoney2, txtDiscount2

                    And the same for the rest of the records

                    I want to set the focus from the txtarticle1 to the txtsize1 when the length of txtarticle1 is 7 , and from the txtsise1 to the txtArticle2 when the length of txtsize1 is equal of 3 .... etc... till the end of the 5 unbound records in the form.

                    This is the code
                    1st this function make sure that the article number that i'm entering in all txtArticle(i) is a valid article

                    Code:
                     
                    Public Function CheckIfExist(ByRef id As Long) As Boolean
                    
                    Dim db As DAO.Database
                    Dim rs As DAO.Recordset
                    Dim intResult As Integer
                    Dim strSQL As String
                    
                    Set db = CurrentDb
                    
                    strSQL = "SELECT COUNT(*) As RecordCount FROM item where article = " + CStr(id)
                    
                    Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
                    
                    intResult = rs("RecordCount")
                    
                    rs.Close
                    db.Close
                    If intResult > 0 Then
                    CheckIfExist = True
                    Else
                    CheckIfExist = False
                    End If
                    End Function
                    And now i'm putting the focus from TxtArticle1 to Txtsize1 to txtCashMoney1.
                    Code:
                    Private Sub txtarticle1_AfterUpdate()
                    If (CheckIfExist(CLng(Me.txtarticle1.Value)) = False) Then
                    MsgBox "The article number that you entered is false"
                    Me.txtType1.SetFocus
                    Else
                    
                    Dim db As DAO.Database
                    Dim dataset1, dataset2 As DAO.Recordset
                    Dim FirstPrice, SecondPrice As Integer
                    Set db = CurrentDb
                    
                    Set dataset1 = db.OpenRecordset("select nz(Price,0) " _
                    & "as firstprice " _
                    & "from item " _
                    & "where article= " & CStr(Me.txtarticle1.Value) & "", dbOpenDynaset)
                    
                    Set dataset2 = db.OpenRecordset("select nz(Price_after_discount,0) " _
                    & "as secondprice " _
                    & "from item " _
                    & "where article= " & CStr(Me.txtarticle1.Value) & "", dbOpenDynaset)
                    
                    Me.txtInvoice_detail_num1.Value = 1
                    FirstPrice = CLng(dataset1![FirstPrice])
                    SecondPrice = CLng(dataset2![SecondPrice])
                    Me.txtPrice1.Value = FirstPrice
                    Me.txtDiscountedPrice1.Value = SecondPrice
                    
                    If (Me.Price_after_discount1.Value = 0) Then
                    Me.txtCashMoney1.Value = FirstPrice
                    Me.txtDiscount1.Value = 0
                    Else
                    Me.txtCashMoney1.Value = SecondPrice
                    Me.txtDiscount1.Value = FirstPrice - SecondPrice
                    End If
                    db.Close
                    
                    End If
                    
                    End Sub
                    
                      Private Sub txtarticle1_Change()
                      If (Len(Me.txtarticle1.Text) = 7) Then
                           Me.txtsize1.SetFocus
                           End If
                      End Sub
                    The code is working but the problem is that when i update the txtArticle1 to a wrong article number (That means when the article number don't exist in the table item) I got this error:
                    Can't get the focus on txtsize1. Help please .
                    And thanks for the lovely team. :)

                    Comment

                    Working...