SelLength in DblClick event not working (1 Viewer)

Skupfer

New member
Local time
Today, 08:38
Joined
May 18, 2007
Messages
5
I have got a textbox which is Locked by default, containing the current user's ID. It is necessary to allow some 'special' users to overwrite the userid. Hence I got the dblclick event on the textbox (txtUserID) to un-lock the text box.
As a default a double click will select the contents of the text box. I want to change that behavior and only select part of the text, i.e. the last character. Somehow the code ignors the SelLength and SelStart..... UNLESS I have a breakpoint in the dblclick event (anywhere), in which case it works.
I have inserted DoEvents, Form.Resfresh and sleep 2 secs. None of that made any difference. Google didn't turn up any help either.
This is part of a Access DB (2000 9.0) module (VBA 6.0). What am I doing wrong?
Code:
Private Sub txtUserID_DblClick(Cancel As Integer)
    With txtUserID
        .Value = .Text
        .locked = Not (.locked)
        If .locked Then
            .BackColor = 16777215
            .TabStop = True
            .SetFocus
            .SelStart = Len(.Value) - 1
            .SelLength = 1
        Else
            .BackColor = 12632256
            .TabStop = False
            txtPwd.SetFocus
        End If
    End With
End Sub
 

MarkK

bit cruncher
Local time
Today, 01:38
Joined
Mar 17, 2004
Messages
8,178
Try cancelling the event after your selection code runs. I bet Access runs your code, and then processes the double click event, which by nature yields a .SelLength of zero, overriding your work.
Code:
Private Sub txtUserID_DblClick(Cancel As Integer)
    With txtUserID
        .Value = .Text  'aren't these necessarily equal already???
        .locked = Not (.locked)
        If .locked Then
            .BackColor = 16777215
            .TabStop = True
            .SetFocus  'if dbl-clicked isn't focus already here???
            .SelStart = Len(.Value) - 1
            .SelLength = 1
        Else
            .BackColor = 12632256
            .TabStop = False
            txtPwd.SetFocus
        End If
    End With
    'cancel the event so Access default DblClick behaviours do not occur???
    Cancel = True
End Sub
I'm not sure this will work but it is what I'd try next.
 

WayneRyan

AWF VIP
Local time
Today, 08:38
Joined
Nov 19, 2002
Messages
7,122
Skupfer,

I was just testing this and saw Lagbolts reply. You do want to put the
Cancel = True at the end of your code. The DblClick event is firing the
Click event after it completes (Unless Cancel = True).

Wayne
 

pono1

Registered User.
Local time
Today, 01:38
Joined
Jun 23, 2002
Messages
1,186
Also, as best as I can tell, I don't think you need the If-End statement...

Code:
    With TxtUserID
        .Value = .Text
        .Locked = Not (.Locked)
        .BackColor = vbWhite
        .TabStop = False
        .SelStart = Len(.Value)
    End With
    Cancel = True
 

geekay

Registered User.
Local time
Today, 14:08
Joined
Aug 8, 2006
Messages
46
Two errors you made in the code
One is the absence of the statement
Cancel = True
Second one; the condition in your if statement is against your requirement. It must be
If Not (.Locked) Then ...................

Third one is a suggestion. You have included many unwanted lines in the code. To meet your purpose only little is needed as pono1's reply to your post
 

Skupfer

New member
Local time
Today, 08:38
Joined
May 18, 2007
Messages
5
Thank you very much vor replying.

Cancel was missing. :)

The extra lines will get removed.
 

Users who are viewing this thread

Top Bottom