Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2004
    Posts
    22

    Question Unanswered: on click selects all text in control

    I am designing a db for users that use mice to navigate data entry forms (instead of tabbing through the controls). I am trying to find some code that I can use on an event procedure that will select all of the text in a form control when the user clicks in that control. So far I have failed.

    The form I am working on will be used by data entry to enter multiple records. For example, they enter a project number, then enter the data on the subform. When done, they click in the project number field, hopefully all of the text in that field will be selected, they enter a new project number, and so on...

    Any help would be greatly appreciated.

    Thanks,

    Craig
    rockjock

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487
    Try this.....copy and paste this little function into the code module of your form:

    Code:
    Private Function HighlightAll(Ctrl As Control)
    	If Not IsNull(Ctrl) Then
    	 Ctrl.SelStart = 0
    	 Ctrl.SelLength = Len(Ctrl)
    	End If
    End Function
    Then in the properties window (Event Tab) enter this line into the Mouse Up event:

    =HighlightAll([myTextBoxORComboBoxName])

    where myTextBoxORComboBoxName is the name of your TextBox or ComboBox control.

    Make sure you put it into the Mouse Up event. If you want to call the function from code then you can call it this way from the Mouse Up code event:

    Code:
    Private Sub myTextBoxORComboBoxName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Call Highlight(Me.myTextBoxORComboBoxName)
    End Sub
    There ya go...


  3. #3
    Join Date
    Feb 2004
    Posts
    533
    Nice Function CyberLynx, there are a couple enhancements I like to suggest.
    1. Make the function Public for availability to any form. Just drop the 'Private' from 'Private Function', then put it in a public module, insted of the forms module.
    2. You can define 'Ctrl' in the function as the ActiveControl and avoid having to pass the control name. This makes it more portable by not having to change the control name in the calling statement when using it on other control fields and forms.

    Code:
    Function HighlightAll()
       Dim Ctrl As Control
       Set Ctrl = Screen.ActiveControl
       If Not IsNull(Ctrl) Then
        Ctrl.SelStart = 0
        Ctrl.SelLength = Len(Ctrl)
       End If
    End Function
    You can call a funtion by putting '=HighlightAll()' directly in the properties field of the control, mouse up or on click event.


    */
    ~

    Bill

  4. #4
    Join Date
    Nov 2004
    Posts
    22
    Thank you CyberLynx. I was able to incorporate your suggestions into my forms with a little modification (primarily because the controls I am applying it to are combo boxes and the code seemed to select only a portion of the concatenated value that is displayed in the combo box). The fix was to specify the SelLength = 255. I hate to use the fixed value, but it works for now.

    Thank you too bill for your suggestions. I would love to implement them so that I don't have to drop this code into every form that I create. I am having some troubles though. I created a module with the code that you suggested. Then, in the forms, I removed the function code and changed the On Mouse Up events to =HighlightAll() as you suggested. I receive an error message that access can't find the function name that I specified. I named my module HighlightAll. Perhaps I don't understand public modules enough to make this work. Any suggestions?

    Thanks again,

    Craig
    rockjock

  5. #5
    Join Date
    Nov 2003
    Posts
    1,487
    As Bill has suggested....make the function Public. Your function should then look like this in your code module:


    Code:
    Public Function HighlightAll()
       Dim Ctrl As Control
       Set Ctrl = Screen.ActiveControl
       If Not IsNull(Ctrl) Then
    	Ctrl.SelStart = 0
    	Ctrl.SelLength = Len(Ctrl)
       End If
    End Function
    Notice the word Public in front of Function....you get the picture.

    Small steps.


  6. #6
    Join Date
    Nov 2004
    Posts
    22
    I understand the Public versus Private in the function code. What I am unclear on is where to place the code. In my limited experience, I have done all of my code in class modules for forms (I think that is the correct terminology), where I set event procedures.

    Do Access databases have a general code module where I can put the function and have it accessible to all of my forms? Or, if I put it in one of my forms with the Public statement will it be accessible to other forms?

    Sorry for the newbie questions.

    Craig
    rockjock

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Craig
    Look on the database window for the modules section, insert your common functions/ modules there. As time goes on put you can build libraries of such usefull functions and use in any / many Access projects.

    The only downside is that the method of importing / sharing modules is cack handed. As far as I' aware, each time you change the library you have to reimport it.

Posting Permissions

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