setfocus not working

A

Ann

I have a form with two fields, one is registration no,
the other is association name.

If an invalid regNo is entered, I check it in the
underlying table, if there is no record, a message box
shown telling need to reenter a valid regno.
My code set the focus back to the regno, but after I
click message box OK, then the focus goes to the next
text fields, association name instead of regno field.


Here is the code:

SQLWhere$ = "([AssocRegistrationNo] like Forms!
[frmPrincipalCheckIn]![PrincipalNo])"

If IsNull(DLookup("[AssocName]", "tblAssociation",
SQLWhere$)) Then

MsgBox "Record not found, please enter a valid
regno"

Forms![frmPrincipalCheckIn]![PrincipalNo] = ""
Forms![frmPrincipalCheckIn]![PrincipalNo].SetFocus

Else
Me.txtPrincName =
DLookup"[AssocName]", "tblAssociation", SQLWhere$)
confirm.SetFocus

End If
 
W

Wayne Morgan

You don't say where you placed this code. If it is in the control's
BeforeUpdate event, you need one more line to prevent the control from
updating with the incorrect value then moving to the next control. During
BeforeUpdate the control still has the focus, so setting the focus to it
doesn't do anything. After the update, the focus moves, just as it would
have.

In the BadValue part of the If statement add the line.
If IsNull(DLookup("[AssocName]", "tblAssociation",
SQLWhere$)) Then

MsgBox "Record not found, please enter a valid
regno"
Cancel = True
'And to undo the changes to the control
Me.PrincipalNo.Undo
'The next 2 line shouldn't be needed
Forms![frmPrincipalCheckIn]![PrincipalNo] = ""
Forms![frmPrincipalCheckIn]![PrincipalNo].SetFocus

Else

If this code is somewhere else, the required change will likely be
different. Let us know where you are trying to do this.

--
Wayne Morgan
MS Access MVP


Ann said:
I have a form with two fields, one is registration no,
the other is association name.

If an invalid regNo is entered, I check it in the
underlying table, if there is no record, a message box
shown telling need to reenter a valid regno.
My code set the focus back to the regno, but after I
click message box OK, then the focus goes to the next
text fields, association name instead of regno field.


Here is the code:

SQLWhere$ = "([AssocRegistrationNo] like Forms!
[frmPrincipalCheckIn]![PrincipalNo])"

If IsNull(DLookup("[AssocName]", "tblAssociation",
SQLWhere$)) Then

MsgBox "Record not found, please enter a valid
regno"

Forms![frmPrincipalCheckIn]![PrincipalNo] = ""
Forms![frmPrincipalCheckIn]![PrincipalNo].SetFocus

Else
Me.txtPrincName =
DLookup"[AssocName]", "tblAssociation", SQLWhere$)
confirm.SetFocus

End If
 
A

Ann

Thank you so much. The code is in the after_update sub
procedure.
the code is Like this
Private Sub PrincipalNo_AfterUpdate()

On Error GoTo PrincipalNo_AfterUpdate_Err:

If IsNull(Forms![frmPrincipalCheckIn]![PrincipalNo])
Then
MsgBox "Please enter Principal Registration Number!"

Forms![frmPrincipalCheckIn]![PrincipalNo].SetFocus
Exit Sub
End If


SQLWhere$ = "([AssocRegistrationNo] like Forms!
[frmPrincipalCheckIn]![PrincipalNo])"

If IsNull(DLookup("[AssocName]", "tblAssociation",
SQLWhere$)) Then

MsgBox "Record not found, please reenter"

Forms![frmPrincipalCheckIn]![PrincipalNo] = ""
Forms![frmPrincipalCheckIn]![PrincipalNo].SetFocus

Else

Me.txtPrincName = DLookup
("[AssocName]", "tblAssociation", SQLWhere$)
confirm.SetFocus


End If

PrincipalNo_AfterUpdate_Exit:
Exit Sub

PrincipalNo_AfterUpdate_Err:
MsgBox "Error: " + Error$,
0, "PrincipalNo_AfterUpdate"
Resume PrincipalNo_AfterUpdate_Exit

End Sub
-----Original Message-----
You don't say where you placed this code. If it is in the control's
BeforeUpdate event, you need one more line to prevent the control from
updating with the incorrect value then moving to the next control. During
BeforeUpdate the control still has the focus, so setting the focus to it
doesn't do anything. After the update, the focus moves, just as it would
have.

In the BadValue part of the If statement add the line.
If IsNull(DLookup("[AssocName]", "tblAssociation",
SQLWhere$)) Then

MsgBox "Record not found, please enter a valid
regno"
Cancel = True
'And to undo the changes to the control
Me.PrincipalNo.Undo
'The next 2 line shouldn't be needed
Forms![frmPrincipalCheckIn]![PrincipalNo] = ""
Forms![frmPrincipalCheckIn]![PrincipalNo].SetFocus

Else

If this code is somewhere else, the required change will likely be
different. Let us know where you are trying to do this.

--
Wayne Morgan
MS Access MVP


I have a form with two fields, one is registration no,
the other is association name.

If an invalid regNo is entered, I check it in the
underlying table, if there is no record, a message box
shown telling need to reenter a valid regno.
My code set the focus back to the regno, but after I
click message box OK, then the focus goes to the next
text fields, association name instead of regno field.


Here is the code:

SQLWhere$ = "([AssocRegistrationNo] like Forms!
[frmPrincipalCheckIn]![PrincipalNo])"

If IsNull(DLookup("[AssocName]", "tblAssociation",
SQLWhere$)) Then

MsgBox "Record not found, please enter a valid
regno"

Forms![frmPrincipalCheckIn]![PrincipalNo] = ""
Forms![frmPrincipalCheckIn]![PrincipalNo].SetFocus

Else
Me.txtPrincName =
DLookup"[AssocName]", "tblAssociation", SQLWhere$)
confirm.SetFocus

End If


.
 
W

Wayne Morgan

Try moving the code to the BeforeUpdate and add the Cancel=True line if the
number is invalid.
 

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

Similar Threads


Top