How to test for user leaving Unbound Control

  • Thread starter SimonG via AccessMonster.com
  • Start date
S

SimonG via AccessMonster.com

Hi,

I have an unbound control on a form. The control is used as a 'quick find',
where as the user types in the box the record moves accordingly*. This works
correctly, however when the user moves away from the control I would like the
contents of this unbound control to be removed.

* Note as the record changes the unbound control is not cleared (this is
correct for this control).

I have tried using events “On Lost Focus†and “On Exitâ€. Both of these
appear to be “all or nothingâ€, with initially, the unbound control being
cleared as soon as the record changes (not wanted). Then modifying my code
(see below), the unbound control now never clears.

'Code as applied to “On Exit†event:

Private Sub txtQuickFind_Exit(Cancel As Integer) 'unbound control is
“txtQuickFindâ€

If Me.ActiveControl.Name <> "txtQuickFind" Then
Me![txtQuickFind] = ""
End If

End Sub

Can someone suggest how I can test for and clear the unbound control only
when the user moves to another control?

Many thanks,
Simon
 
S

SimonG via AccessMonster.com

Hi Ryan,

Sorry, this doesn't work, as it takes out what the user has types as soon as
its entered.

The idea of the function/unbound box is the user enters a character; the
record moves accordingly; the user enters another character the record moves,
etc. When the user has the record they are looking for they will move away
from the unbound text box, which then clears its contents. It's this final
exit control / clear control I'm struggling to make work.

Regards,
Simon
 
R

Ryan

In my apps I use buttons for quickserches and navigation. Right next to the
quick search control I would add a botton that would run the code to take me
to the record the quick search control references. I can give you the code
to do this if you like. Another way to handle your issue would to put a On
Focus event of control that comes after txtQuickFind that looks like this.

Me.txtQuickFind = ""
DoCmd.GoToControl "txtQuickFind

In the After Update event of the txtQuickFind control I would run the code
you are using to go the the record specified. This will take you to the
record specified first, then go to the next control, clear the txtQuickFind,
and take you back to the txtQuickFind control.
 
S

SimonG via AccessMonster.com

Hi Ryan,

The idea of the quick search was to do just a quick search. The user goes to
a control enters characters, the control uses the 'on change' event to
refresh the shown record as more characters are entered, until the required
record is found or a record unfound message is displayed. All of which works,
and the last thing left to do is to clear the control when the user moves
away.

The problem here isn't the searching, which I'm happy with, rather how to
recognise and test for the user leaving the control. Thinking about the user
leaving this control (given what they have just done), we don't know how they
are going to move away (mouse or keyboard), or which control they are next
going to go to. And this is the heart of my problem, how to test when a user
has left a control.

Regards,
Simon
 
R

Ryan

I know you have already tried it, but the On lost focus is the correct event
for what you are trying to do. Try this...

Private Sub txtQuickFind_LostFocus()
Me.txtQuickFind = ""
End Sub

I tested this several time on several different apps, so if it doesn't work
maybe you could post your On Change code.
 
S

SimonG via AccessMonster.com

Hi Ryan,

Yes this is what I tried first (prior to posting), the problem with this
snipit is it clears the control as soon as the record changes. Where what we
want to do is allow the user to continue adding to the string until they have
found the record they are looking for.

Here's the code behind the 'On Change' event:

Private Sub txtQuickFind_Change()
Dim strFindThis As String
Dim strTable As String
Dim strWhere As String
Dim intFound As Integer

strFindThis = Me!txtQuickFind.Text
strTable = "t_Family"

'Quick find works on severial key fields in record:
strWhere = "[FamilyName] Like '*" & strFindThis & "*' OR " & _
"[FirstName] Like '*" & strFindThis & "*' OR " & _
"[AddressLine1] Like '*" & strFindThis & "*' OR " & _
"[PostCode] Like '*" & strFindThis & "*'"

intFound = Nz(DLookup("[FamilyID]", strTable, strWhere), 0)

If intFound = 0 Then
'warn user record not found
MsgBox "Could not locate [" & strFindThis & "]"
Else
' move to first record found
Me.RecordsetClone.FindFirst "[FamilyID] = " & intFound '& "'"
Me.Bookmark = Me.RecordsetClone.Bookmark
' keep curser at the end on entered search string:
intFound = Len(strFindThis)
Me![txtQuickFind].SelStart = intFound
End If

End Sub

Hopefully the comments and example should make it clear what is happening.
Remember this code goes on the 'On Change' event of an Unbound text box.

Regards,
Simon
 
R

Ryan

It looks to me like every possible senerio your code produces moves to
another control. Even though it brings focus back to txtQuickFind, it has
already left. The only thing I can think to do is set the On Focus event of
every other control on the form to clear the txtQuickSearch.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top