Results 1 to 14 of 14

Thread: TextBox1.SetFocus doesn't work (word2003)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    TextBox1.SetFocus doesn't work (word2003)

    hi there,

    I made a very simple vba program using word2003 with 3 texboxen for input, and some labels for calculated output ( simlpe tax caculation with variable tax % ), 1 commandbutton to start calculating, and 1 commanbutton to " reset" the whole thing( clears the 2 input textboxen, and keeps the tax % texbox).

    all works well, but when i push the Reset button, all gets erased, but the setfocus won't work.

    code ( when reset button is pushed) is something like:

    textbox1.text = clear
    ...

    texbox1.setfocus
    end

    i would also like to use the tab key to go from inputbox to inputbox, how do i achieve that ?

    greets and thanks in advance
    sven

  2. #2
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: TextBox1.SetFocus doesn't work (word2003)

    hi,

    to get focus to the text box you want, try setting focus to another object on screen first, before going back to the one you want.

    e.g.:
    VB Code:
    1. 'you want focus to go to text1
    2.  
    3. text2.SetFocus
    4. text1.SetFocus

    to go from one input box to another, you need to look at the 'Tab Order' of the text boxes, and put them into the sequence that you want.

    HTH
    if you fail to plan, you plan to fail

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Re: TextBox1.SetFocus doesn't work (word2003)

    hey,

    Textbox1.SetFocus didn't work in any way..
    TextBox1.Select does :-)

    thanks for helping me anyway, still working on that tab, but right now i'm at school and supposed to do other things

    thanks, and have a nice weekend
    sven

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: TextBox1.SetFocus doesn't work (word2003)

    There is no TextBox1.SetFocus in Word Theequilivalent is like posted, TextBox1.Select
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: TextBox1.SetFocus doesn't work (word2003)

    ah, i see.

    if you have objects directly on the word document you cannot set focus which makes sense i suppose.

    but if you create a custom form, then you can use SetFocus within the forms code module.
    if you fail to plan, you plan to fail

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: TextBox1.SetFocus doesn't work (word2003)

    Yes, thats correct. Glad you made that point. I shouldnt have assumed that since the thread starter didnt
    mention a userform, it wasnt being used.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Re: TextBox1.SetFocus doesn't work (word2003)

    indeed, there is no user form involved, is that why i can't seem to get the tab key working ? I remember in VB 6.0 ( not vba ) that there was a "tab priority" to adjust, but i don't find anything like that in my Word VBA textbox properties ?

    greets
    sven

  8. #8
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: TextBox1.SetFocus doesn't work (word2003)

    try something like:

    VB Code:
    1. Private Sub TextBox1_Change()
    2.  
    3. If TextBox2 = validinput Then
    4.     TextBox2.Select
    5.     Else
    6.         MsgBox "Enter valid input", vbOKOnly + vbCritical, "Input Error"
    7. End If
    8.  
    9. End Sub

    you can remove the validation part of the above.

    [EDIT]

    the above doesnt work as it works after each character input so try the below:

    VB Code:
    1. Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    2.  
    3. 'sets focus to next text object when ENTER is pressed
    4. 'KeyCode = 9 can be used for TAB key
    5. If KeyCode = 13 Then
    6.     TextBox2.Select
    7. End If
    8.  
    9. End Sub

    also, the things you are looking to do will work on a UserForm, as tab order and SetFocus can both be utilised.

    is a User form an option for you?
    Last edited by Br1an_g; Apr 16th, 2005 at 09:52 AM.
    if you fail to plan, you plan to fail

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Re: TextBox1.SetFocus doesn't work (word2003)

    hi,
    let me give you the full story so you can see what i'm trying todo.

    i'm trying to convert a word file wich acts as an invoice ( for training only ) from a word file with tables, to a word file where you can input a product id number, and a number to indicate howmuch of product X the client wants, taxes and total price and productdetail will be inserted in labels when product id number and number of pieces are known.

    so i started with my 2 textboxen and some cmdbuttons to do some very basic calculation just to see if it would work, and i got the impression that it would work, after i input al needed data ( some 100 products with id number and price and taxrate ( can be 6% 12% or 21% depending on the product )

    my goal is to stay with the original word file, because, if i used a userform, i might as well start from scratch in vb6.

    so, within my word file, everyting must work as desired ;-) incuding the "tab" function.

    this is a freetime project for school.

    saturdaynight 21:47, what am i doing here ? ;-)

    thanks for helping me out
    sven

  10. #10
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: TextBox1.SetFocus doesn't work (word2003)

    perhaps if you attach the word doc as it is just now, we can make some suggestions.
    if you fail to plan, you plan to fail

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: TextBox1.SetFocus doesn't work (word2003)

    In Word you can have up to 3 different types of TextBoxes. All of which are different in the way to handle the tabbing order.
    You can have ActiveX TextBox, Word Form Field TextBox, and UserForm TextBox controls.
    So if you could attach the document or provide a small screenshot of the textboxes, its really hard to solve.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Re: TextBox1.SetFocus doesn't work (word2003)

    hi there,
    sorry for my late reply, but i hope you can find my attached wordfile with the very basic calculation.

    many thanks in advance for looking at it.
    greets
    sven
    Attached Files Attached Files

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: TextBox1.SetFocus doesn't work (word2003)

    You had the reset button set to take focus upon click. The setting was in the properties window for the reset button.
    I also took out the Clear functions and replaced them with vbNullString.
    Works fine now.
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Re: TextBox1.SetFocus doesn't work (word2003)

    thanks a lot for the effort ;-)

    greets
    sven

Posting Permissions

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



Click Here to Expand Forum to Full Width