Enter Key Navigation Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form named subSearchLima (not really a subform) where I have a
control called "newDOB". The form is used for data entry, and I have VB code
that, on Update of newDOB, compares it to a field on another open form
(SearchLima). The logic for the comparison is working, but I can't get the
focus to stay on newDOB if the string is not equal to the comparison field.
The default enter key behavior is continuing to move it to the next field on
the form, whether the logic is true or false. I have also tried using the
next field to move the focus back On Enter, but haven't been able to get that
to work either.

Here is the VB code in the After_Update section of newDOB:
Private Sub newDOB_AfterUpdate()
Dim gotDOB As Date
Dim getNew As Date
gotDOB = Forms!SearchLima!DOB
getNew = Me.newDOB
If getNew = gotDOB Then
ExamYear.SetFocus
Else
Me.newDOB = Null
newDOB.SetFocus
End If
End Sub

Here was the code I was trying in the next field (ExamYear) on Enter:
Private Sub ExamYear_Enter()
If Me.newDOB = Null Then
newDOB.SetFocus
End If
End Sub

Any help would be much appreciated.

Regards,
Bill
 
I believe that After_Update occurs after the Enter key is pressed and focus
is lost. Try placing the code in the On_Enter event and see what happens.
 
Bill,

A couple of ideas.

1. If these values have to be equal, why don't you just set the value of
newDOB based on the value on the SearchLima form? You could do this in the
Load or Open event of subSearchLima and either disable or lock the newDOB
field so that the user cannot change it.

2. Have you tried using the BeforeUpdate of the newDOB field control to do
this test? It would look something like:

Private sub newDOB_BeforeUpdate(Cancel as boolean)

If me.newDOB.text <> Forms!SearchLima!DOB Then
Cancel = True
Else
Cancel = False
endif

If Cancel = true then msgbox "Display some message here!"

End Sub

This would cancel the update action and leave the cursor in the newDOB field.

3. Try prefacing the control name with "me." This would look like:

If getNew = gotDOB Then
me.ExamYear.SetFocus
Else
Me.newDOB = Null
me.newDOB.SetFocus
End If

4. Personally, I like to use a standard naming convention
(http://support.microsoft.com/kb/173738) for all of my controls and other
database object. This way, when I read my code, I know I whether I am
referring to a control or to a field in the forms recordset. This is pretty
common within the developer community, and makes it much easier to: find
your controls, distinguish what type of control your looking at just by
looking at the controls name.

HTH
Dale
 
Thanks for the quick response. The Before_Update approach works to an
extent; the cursor stays in the field and doesn't move and does move if the
string is the same. However, because we are trying to save keystrokes, if,
when the strings are different, we can blank the field and still stay in it,
that would be great. I tried the null statement for the <> logic, but it
didn't work. Any ideas?
 
Back
Top