setting focus on a control that was canceled.

  • Thread starter Thread starter ping
  • Start date Start date
P

ping

the code below was previously advised by allen browne for
prevention of duplicate entry.
it works great but here comes the question. after cancel
is true, the cursor would be blinking at the right last
entered character of the control, how do i get it to be
blinking at the leftmost character highlighting the text
thats previously not saved? so that the user is able to
straight away enter in the correct input without a need to
backspace. i need to do this cos my app is gonna use a
scanner and has no keyboard therefore i cant backspace.

i've tried adding clientcode.setfocus after cancel = true,
but it ends with a error saying that i have to save the
field before i can setfocus. i also tried making
clientcode = "", but cant work too. how do i get around
this?

thks in advance

Private Sub txtClientCode_BeforeUpdate()
Dim strWhere As String
With Me.txtClientCode
If .Value = .OldValue Then
'do nothing.
Else
strWhere = "[ClientCode] = """ & .Value & """"
If Not IsNull(DLookup
("ClientCode", "ClientTable", strWhere))
Then
MsgBox "Duplicate"
'Cancel = True
End If
End If
End With
End Sub
 
ping said:
the code below was previously advised by allen browne for
prevention of duplicate entry.
it works great but here comes the question. after cancel
is true, the cursor would be blinking at the right last
entered character of the control, how do i get it to be
blinking at the leftmost character highlighting the text
thats previously not saved? so that the user is able to
straight away enter in the correct input without a need to
backspace. i need to do this cos my app is gonna use a
scanner and has no keyboard therefore i cant backspace.

i've tried adding clientcode.setfocus after cancel = true,
but it ends with a error saying that i have to save the
field before i can setfocus. i also tried making
clientcode = "", but cant work too. how do i get around
this?

thks in advance

Private Sub txtClientCode_BeforeUpdate()
Dim strWhere As String
With Me.txtClientCode
If .Value = .OldValue Then
'do nothing.
Else
strWhere = "[ClientCode] = """ & .Value & """"
If Not IsNull(DLookup
("ClientCode", "ClientTable", strWhere))
Then
MsgBox "Duplicate"
'Cancel = True
End If
End If
End With
End Sub


You can add:
.SelStart = 0
.SelLength = Len(.Text)
before the Cancel = True

Alternatively, you might want to consider using:
.Undo
instead?
 
You can add:
.SelStart = 0
.SelLength = Len(.Text)
before the Cancel = True

Alternatively, you might want to consider using:
.Undo
instead?


thanks i'll give that a try
 
Back
Top