setfocus to textbox

J

Jeff

I am having trouble getting the setfocus command to work. I have a textbox
that is linked to a database field. I want to make sure they have entered
something in the textbox before the move to the next field. I put vb code in
the loseFocus event. I looks like this.

If IsNull(Me.Survey_ID.Value) Then
Me.Survey_ID_Status.Value = "BAD"
Me.Survey_ID.SetFocus
Else
Me.Survey_ID_Status.Value = "GOOD"
End If

I can see the value change in the Survey_ID_Status field when I run the
form, but it never sets the focus back to the Survey_ID textbox. It just
keeps moving forward.

It's like it just ignores the Setfocus command.
 
K

Klatuu

That will not work. Here is how it is done:
Use the Survey_ID control's Before Update event. It can be canceled and
when it is, the focus does not move.

Private Sub Survery_ID_BeforeUpdate(Cancel As Integer)

If Trim(Nz(Me.Survery_ID, vbNullString)) & vbNullString = vbNullString
Then
MsgBox "A Value is required"
Cancel = True
End If

End Sub

this part:
Trim(Nz(Me.Survery_ID, vbNullString)) & vbNullString

Will catch not only a Null, but cases where a user has accidentily put a
space in the control.
 
S

strive4peace

Hi Jeff,

rather than the LostFocus event, use the control BeforeUpdate event and
CANCEL the change ... better yet, use the FORM BeforeUpdate event to
ensure that all required data is filled before a record (new or changed)
is saved

what is Survey_ID? something that the user is supposed to fill out
before a record is saved?

to validate a record and prevent it from being saved, put code in the
form BeforeUpdate event

'~~~~~~~~~~~~~~~~~~~~~~~~~~
'----------------- make sure all required data is filled out

'make sure Survey_ID is filled out
If IsNull(me.Survey_ID) then

'if it is not filled out, then move the focus to that control
me.Survey_ID.setFocus

'give the user a message
msgbox "You must enter the Survey ID",,"Missing Data"

'don't save the record yet
Cancel = true

'quit checking and give them a chance to fill it out
exit sub
end if
'~~~~~~~~~~~~~~~~~~~~~~~~~~

Warm Regards,
Crystal

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

*
:) have an awesome day :)
*
 

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