Results 1 to 4 of 4
  1. #1
    ssissons is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2014
    Posts
    44

    Highlight part of a textbox value, when setting the focus


    I am editing all of the form fields, when I click the "Save" button. This is creating a string that will be displayed in a msgbox. When the user clicks on "OK", I want to set the focus on the first field in the error list, and highlight part, or all, of the data in the field.

    So, the questions are:
    1. Can I set a variable to the name of a field, that will be used in the SetFocus command?
    2. Can I highlight a portion of the data in a text box? i.e. If the txtTitle length is more than 35 characters, set the focus to the txtTitle and highlight the first 35 characters (or the characters after the 35th).

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can use the name of a control to adjust its properties via VBA. One property is Back Color, I believe. Changing the Back Color to yellow would give a highlight affect. I do not believe you can affect a portion of the Back Color vs the entire control's Back Color.

    Alternatively, you could use HTML to format portions of the text within a TextBox control that is using Rich Text.

  3. #3
    ssissons is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2014
    Posts
    44
    Okay, now a little more explanation for #1.

    Let's say I have 15 fields on my form, and 3 of them have errors. I have concatenated the three error messages into one string, with vbCrLf in between. I display a MsgBox with the complete error message. When the user clicks the "OK" button, I want to set the focus to the first field with an error. Can I have a variable with the control name, and use that with the Setfocus method? How would I do that?

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by ssissons View Post
    ...Can I have a variable with the control name, and use that with the Setfocus method? How would I do that?
    You could create a variable to store the name of a control. However, the controls are already part of the Form's collection. There is already memory set aside for the controls and there is not any need to use a variable to identify the control. I sometimes assign a control to a variable using the index of a control within the collection.


    I think it is something like this
    Dim ctl as Control
    Set ctl = Me.Controls(2)
    ctl.Setfocus


    But you can simply do the same thing this way
    Me.NameOfControl.SetFocus

    If you want to get real fancy, I sometimes use the Tag property vs. using the name property of controls. You can assign a text value of the Tag property via the Property Sheet. Then you can use VBA to iterate the controls within the Form's collection. If a Tag matches the search during the iteration. You can change the background color of the control and set focus. Using this approach, you would want to declare a variable to use while iterating the collection.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 09-07-2012, 10:45 AM
  2. Replies: 12
    Last Post: 06-08-2012, 02:37 AM
  3. Setting the focus on a subform
    By Paul H in forum Forms
    Replies: 3
    Last Post: 01-10-2012, 09:17 AM
  4. Setting focus on a tabbed form
    By crowegreg in forum Forms
    Replies: 21
    Last Post: 08-27-2011, 09:22 AM
  5. Setting Focus on a Form
    By MFeightner in forum Forms
    Replies: 1
    Last Post: 07-30-2009, 07:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums