How to select text box content in form

Chintsapete

Registered User.
Local time
Today, 02:11
Joined
Jun 15, 2012
Messages
137
Hi Guys

I'm a lousy beginner in VB and been piecing together code from some of you guys in the forum. What I'm trying to do is prevent accidental data change. Below code works as far as locking the control after update and unlocking it on double click. What I can’t figure out is: How do I get the code to select the content of the field so I can just type in the correction. I don’t imagine it do be that difficult but I just can't figure it out. Any help would be appreciated.

Thanks
Pete

Private Sub Morn_AfterUpdate()
DoCmd.RunCommand acCmdRefresh
DoCmd.OpenQuery "CB01 Morning shift D", acViewNormal, acEdit
DoCmd.OpenQuery "CB01 Morning shift", acViewNormal, acEdit
DoCmd.RunCommand acCmdRefresh
With Me
If Me.NewRecord Then
.Morn.Locked = False

Else
.Morn.Locked = (Len(.Morn & vbNullString) > 0)

End If
End With
End Sub

Private Sub Morn_DblClick(Cancel As Integer)

With Me
.Morn.Locked = False

End With

End Sub
 
Thanks vbaInet (sorry don't know your name) for your reply. Do you have any idea where to find some more examples than the Microsoft site. I seem to get stuck at seltext= what?? Sorry, as I said beginner of note in code, but I don't find a solution with macros either.

Private Sub Morn_DblClick(Cancel As Integer)

With Me
.Morn.Locked = False
.Morn.SetFocus
.Morn Seltext=????
End With

Cheers
Pete
 
So the SelLength (not SelText) will be the Length of the text in the textbox. Look up the Len() function as well.
 
Thanks again, but I give up, this is beyond me. I'd imagine it should be an easy thing like
With Me

.Morn.Locked = False
.Morn.SetFocus
.Morn.SelLength = Len(.Morn)

End With

But I can't get it to work and I spent a lot of time on this already. It's not an essential thing it would have been nice to have.
Cheers
Pete
 
It's not beyond you. What is happening at the moment? Is the textbox Enabled?
 
I think so, the red high lighted line takes care of that. If I run the code until there it works, but I have to select the text in the box manually. So yes it is unlocked or enabled.

Private Sub Morn_AfterUpdate()
DoCmd.RunCommand acCmdRefresh
DoCmd.OpenQuery "CB01 Morning shift D", acViewNormal, acEdit
DoCmd.OpenQuery "CB01 Morning shift", acViewNormal, acEdit
DoCmd.RunCommand acCmdRefresh
With Me
If Me.NewRecord Then
.Morn.Locked = False

Else
.Morn.Locked = (Len(.Morn & vbNullString) > 0)

End If
End With
End Sub


Private Sub Morn_DblClick(Cancel As Integer)

With Me

.Morn.Locked = False
.Morn.SetFocus
.Morn.SelLength = Len(.Morn)

End With

End Sub

Cheers
Pete
 
So you're doing it in the Double Click event of the textbox that you wish to highlight? Here's your amended code:
Code:
Private Sub Morn_DblClick(Cancel As Integer)
    Cancel = True
 
    With Me.Morn
        .Locked = False
        .SelStart = 0
        .SelLength = Len(.Value)
    End With
End Sub
By the way, Enabled and Locked are two different things.
 
Thanks a lot for the amended code, your a star. And thanks for the encouragement too, I've been working with access for a long time but always used the macros so far. And I realize there is limitations. But it's really hard to get into code and understand exactly whats happening. So thanks a lot for all your help.
It almost works. I think the problem is the format. The cell is formatted as currency (R1,000.00). What happens now is it only selects R1, and leaves all the zeros out. Any suggestions where to look
Cheers
Pete
 
There's nothing you can do there. The format is not part of the value of the textbox. If you know the length of the format you can use that length instead.
 
Alright, best thing is to use a value like 25 as the length.
 
I just figured something else, if I add numerical shapes I get R1,0 but still not to the end of the field. Not sure.
Private Sub Morn_DblClick(Cancel As Integer)
Cancel = True

With Me.Morn
.Locked = False
.SelStart = 0
.SelLength = Len(.Value & .NumeralShapes)
End With
End Sub
 
Did you see my last post? If you saw it, did you try what I advised?
 
Sorry only seen your last post after i posted the previous.
Thanks vbaINET below works perfect, presumably up to 25 digits. You're superstar. Thanks for your patients and help again.
Cheers
Pete

Private Sub Morn_DblClick(Cancel As Integer)
Cancel = True
With Me.Morn
.Locked = False
.SelStart = 0
.SelLength = 25
End With
End Sub
 
Correct! I don't imagine your numbers going over 25 digits unless you're mega rich ;)

You're welcome!
 
maybe I move to Zimbabwe one day but now I'll know how to correct the issue;)
 

Users who are viewing this thread

Back
Top Bottom