• Code for # of Keystrokes to Trigger Cursor Move (W

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Code for # of Keystrokes to Trigger Cursor Move (W

    Author
    Topic
    #392745

    Try this:

    Private Sub TextBox_Change()
    Static X As Integer
        If X = 1 Then
            NextText.SetFocus
            X = 0
        Else
            X = X + 1
        End If
    End Sub
    

    Using Static to dim X causes it to remember the value from one call to the next.

    Viewing 1 reply thread
    Author
    Replies
    • #707010

      (Edited by SherryThing on 28-Aug-03 22:30. Can’t use a userform for this.)

      Does anyone know of any code that would cause the cursor to move to the next formfield after a certain number of keystrokes. What I need is for the cursor to move to the next formfield after any, say, two keystrokes are entered into the current formfield – so the User doesn’t have to hit the Tab key to get to the next formfield. This is a protected form in Word. I need code to do this in the actual Word form document – not in a Userform.

      THANKS.

      • #707164

        Hi Sherry,
        If you are using a userform this is easily accomplished without code. Just use a textbox control and set 2 properties: MaxLength=2 and AutoTab=true.

        • #707216

          Hi Kevin and Loren Ann,

          Thanks for these suggestions. However, I realize now that we need to use the formfields within the actual form document. A userform won’t work with the form we’re working with. So I’m looking for an on-entry macro I suppose, that will cause the cursor to move forward to the next formfield after x number of keystrokes. I don’t know if this is at all possible though…

          Sherry

          • #707235

            I learned early on that formfields are not quite as flexible or as powerful (from a programming perspective) as userform controls. I hope you find an answer. If so, let us know what it is. I’m curious to know how it can be done with ffields.

          • #707236

            I learned early on that formfields are not quite as flexible or as powerful (from a programming perspective) as userform controls. I hope you find an answer. If so, let us know what it is. I’m curious to know how it can be done with ffields.

          • #707363

            Oh, this is bad. There is an application event in Word known as WindowSelectionChange. It monitors many different types of “moves,” but it does not trigger for continuous typing. So even with all the rigamarole of testing it, it won’t help. This might not be something that Word’s standard Form controls can do.

            For future reference, here was the test environment:

            Word document with two text fields, the first one limited to 3 characters.

            In the document,

            1. A class module named clsWordAppEvents containing the following code:
              Option Explicit
              Public WithEvents wdApp As Word.Application
               
              Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
              ' Write selection "end" position, field name, field "results" to Immediate window
              Debug.Print Sel.End, Sel.Bookmarks(1).Name, _
                      ActiveDocument.FormFields(Sel.Bookmarks(1).Name).Result
              End Sub
            2. A regular code module (name not important) containing the following code:
              Option Explicit
              Dim wdEvts As New clsWordAppEvents
               
              Sub RegisterAppEvtHandler()
              Set wdEvts.wdApp = Word.Application
              End Sub

            Resized the windows so I could watch the Immediate window as I typed in the document (protected for forms), ran the RegisterAppEvtHandler sub and then tested.

          • #707364

            Oh, this is bad. There is an application event in Word known as WindowSelectionChange. It monitors many different types of “moves,” but it does not trigger for continuous typing. So even with all the rigamarole of testing it, it won’t help. This might not be something that Word’s standard Form controls can do.

            For future reference, here was the test environment:

            Word document with two text fields, the first one limited to 3 characters.

            In the document,

            1. A class module named clsWordAppEvents containing the following code:
              Option Explicit
              Public WithEvents wdApp As Word.Application
               
              Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
              ' Write selection "end" position, field name, field "results" to Immediate window
              Debug.Print Sel.End, Sel.Bookmarks(1).Name, _
                      ActiveDocument.FormFields(Sel.Bookmarks(1).Name).Result
              End Sub
            2. A regular code module (name not important) containing the following code:
              Option Explicit
              Dim wdEvts As New clsWordAppEvents
               
              Sub RegisterAppEvtHandler()
              Set wdEvts.wdApp = Word.Application
              End Sub

            Resized the windows so I could watch the Immediate window as I typed in the document (protected for forms), ran the RegisterAppEvtHandler sub and then tested.

        • #707217

          Hi Kevin and Loren Ann,

          Thanks for these suggestions. However, I realize now that we need to use the formfields within the actual form document. A userform won’t work with the form we’re working with. So I’m looking for an on-entry macro I suppose, that will cause the cursor to move forward to the next formfield after x number of keystrokes. I don’t know if this is at all possible though…

          Sherry

      • #707165

        Hi Sherry,
        If you are using a userform this is easily accomplished without code. Just use a textbox control and set 2 properties: MaxLength=2 and AutoTab=true.

    • #707011

      (Edited by SherryThing on 28-Aug-03 22:30. Can’t use a userform for this.)

      Does anyone know of any code that would cause the cursor to move to the next formfield after a certain number of keystrokes. What I need is for the cursor to move to the next formfield after any, say, two keystrokes are entered into the current formfield – so the User doesn’t have to hit the Tab key to get to the next formfield. This is a protected form in Word. I need code to do this in the actual Word form document – not in a Userform.

      THANKS.

    Viewing 1 reply thread
    Reply To: Code for # of Keystrokes to Trigger Cursor Move (W

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: