Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation

Status
Not open for further replies.
Dec 5, 2001
44
US
I am trying to validate the user entry in one of the form fields.

I have written my SUB in the LostFocus event. My problem is I am not able to return the focus on the field to be validated. Once the user clicks OK on the error message the focus shift to the next control on TabIndex.:

Private Sub txtLogID_LostFocus()
If IsNull(txtLogID) Then
MsgBox "LogID cannot be empty", vbOKOnly, "LogID"
If Response = vbOK Then
Me.txtLogID.SetFocus
End If
End If
End Sub

WHY????

Thanks
 
Its because when the lost focus event is triggered, the focus is already on the next control. You could try:

a) using a different event i.e. on change, or after update

b) changing me.control.setfocus to..

control.setfocus


hope this helps

:eek:)

Hj
 
I tried both the suggestions above but they won't work. And so I improvised. Here is what I did (this is not efficient programming but then I am not a programmer)

The next control in Tab Order is txtDeptCode...

Private Sub txtLogID_LostFocus()
If IsNull(txtLogID) Then
MsgBox "LogID cannot be empty field", vbOKOnly, "LogID"
End If
End Sub

Private Sub txtDeptCode_GotFocus()
If IsNull(txtLogID) Then
txtLogID.SetFocus
End If
End Sub

It works!!

Thanks guys.

 
I had the exact same question come up this morning. After reading how you worked around the problem, I'll probably do the same or validate the data during an On Click event for exiting the form. However, I'd like to know how others have dealt with this situation.

Any more ideas anyone?

Judie
 
I usually set up my forms with a 'Save' button. When the user clicks on Edit or Add, I disable all command buttons except Undo and Save.

I then do all my edit checks in the On Click event of the save button.

Hope that helps.
Larry De Laruelle
larry1de@yahoo.com

 
Larry,

I liked your idea of having a save button on the forms.

I tried the same thing but found that as soon as I move from one filed to another in the same record, access will save it.

How can I stop access from saving the fields as I move through them in a form?

Thanks!


 
The correct way to do this is:

Private Sub txtLogID_BeforeUpdate(Cancel As Integer)

If IsNull(txtLogID) = TrueThen
MsgBox "LogID cannot be empty", vbOKOnly, "LogID"
Cancel = True
End if

End Sub

The before update event fires when you try to shift focus (but before focus is lost). The Cancel = True indicates that the calling event is to be cancelled. Hence focus is never lost and the user is kept in that box.

Help?

Craig

 
I couldn't get this to work the way you wrote it. Then I realized that I wanted to test the data that was entered, not just avoid a Null. When I changed the IsNull (txtLogID)= True to a different test, len(txtLogID)<>6, and I entered data in the field, it worked fine. It still dosen't allow for avoiding a user entering a Null value, which I'd also like to do, but it's a step in the right direction.

Any other ideas?
 
if ((IsNull(txtLogID)=True) OR (len(txtLogID)<>6)) Then
... Jonathan
________________________________________
It is not fair to ask of others what you are unwilling to do yourself.
-Eleanor Roosevelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top