Setting the Focus on a Control

M

Mr. JYC

Hello,

I am using Access 2000 ADO to build form control validation.

I am running into a problem. I use an event lostfocus to process some form
control validation. When I try to move the focus back to the same form
control using setfocus, it doesn't do it. It seems I have to move the focus
to some other control then to the control that I originally want to move back
to.

So for example, I have a control text1. I use the lostfocus event to test
the contents of text1. If it is false I want to move it back to text1. It
seems that I have to move to text2 (an arbitrary control) (using setfocus)
before I can move it back to text1 (again using setfocus).

Could someone help me on this?
 
K

Ken Warthen

JYC,

I frequently use the LostFocus event to validate entered data as well.
Here's a sample from one of my Access 2007 forms that works fine. Try
adapting it to your applicaiton. — Ken

Private Sub txtProjectName_LostFocus()

If Me.txtProjectName = "" Or IsNull(Me.txtProjectName) Then
MsgBox "You must enter a project name.", vbOKOnly + vbInformation,
"Missing Project Name"
Me.txtProjectName.SetFocus
Exit Sub
End If

End Sub
 
D

Douglas J. Steele

If it's a bound control, put your check in the control's BeforeUpdate event,
rather than in the LostFocus event.

Private Sub text1_BeforeUpdate(Cancel As Integer)

If Len(Me.text1 & vbNullString) = 0 Then
MsgBox "You must enter a value for text1"
Cancel = True
End If

End Sub

No need to reset the focus, since focus won't be allowed to leave the
control!
 
M

Mr. JYC

Thanks for the reponse Ken. My code is pretty much the same. The problem is
that the focus moves and doesn't say put. Is there an error or am I missing
something?
 
M

Mr. JYC

Thank you for you response Douglas.
The code did not work unfortunately. It seems that the event is not even
being triggered.
Does it make a difference what reference I am using? I am using Microsoft
ActiveX Data Objects 2.1 Library.
--
Thank you for your help!
JYC


Douglas J. Steele said:
If it's a bound control, put your check in the control's BeforeUpdate event,
rather than in the LostFocus event.

Private Sub text1_BeforeUpdate(Cancel As Integer)

If Len(Me.text1 & vbNullString) = 0 Then
MsgBox "You must enter a value for text1"
Cancel = True
End If

End Sub

No need to reset the focus, since focus won't be allowed to leave the
control!
 
D

Douglas J. Steele

It should be reference-independent.

It just occurred to me that the BeforeUpdate event won't work for checking
that a value wasn't changed: after all, you didn't update the field!

To be honest, I never bother prompting until the user has indicated that
they're finished with the record: I don't force them to fill the fields in
any particular order. That means I put my validation code into the _form's_
BeforeUpdate event, not the _control's_ event. In other words, I'd probably
use

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Len(Me.text1 & vbNullString) = 0 Then
MsgBox "You must enter a value for text1"
Me.text1.SetFocus
Cancel = True
End If

End Sub


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Mr. JYC said:
Thank you for you response Douglas.
The code did not work unfortunately. It seems that the event is not even
being triggered.
Does it make a difference what reference I am using? I am using Microsoft
ActiveX Data Objects 2.1 Library.
 

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