Self-referencing controls/forms in function

flect

Registered User.
Local time
Today, 14:41
Joined
Feb 26, 2008
Messages
86
Hello World.

I have a little piece of code that I'm trying to convert for use in a module so that it works on any control.

It basically selects all text in a box/field onMouseUp, but I want it to work without having to specify which control all the time

The basic code is:
Code:
Me.{controlname}.SelStart = 0
    Me.{controlname}.SelLength = Len(Me.{controlname}.Text)
and here's what I came up with:

Code:
Public Function SelectAll()
    Dim strActiveCtl As String
    Dim strActiveForm As String
    strActiveCtl = Screen.ActiveControl.Name
    strActiveForm = Screen.ActiveForm.Name
     
'use forms!myformname rather than me.
    'Forms!strActiveForm!strActiveCtl.SetFocus
    Forms!strActiveForm!strActiveCtl.SelStart = 0
    Forms!strActiveForm!strActiveCtl.SelLength = Len(Form!strActiveForm!strActiveCtl.Text)
    
    

End Function
The above code gives me a runtime 2450 error, which is bascially 'form not open' - despite the fact it is indeed open

Any suggestions or ideas? :o
 
Code:
Public Function SelectAll(ByRef ctl As Control)

    With ctl
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
    
End Function

Called with the OnMouseUp Property set to [Event Procedure]:-
Code:
Private Sub txtFieldA_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    SelectAll Me.txtFieldA
    
End Sub

Or called directly with the OnMouseUp Property set to:-

=SelectAll([txtFieldA])



Chris.
 
Perfect!! - Thanks ChrisO

:p
 

Users who are viewing this thread

Back
Top Bottom