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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetFocus, not working, validate field 1

Status
Not open for further replies.

Blondie96

Programmer
Aug 12, 2004
119
US

I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5


I am trying to validate data & force re-entry of the field if it does not pass. I have only been able to use the setfocus from within the onfocus of the next field, resetting it to the previous field.

Is there a way to check before leaving the field & stop the focus from moving to the next field.

Reason:

I can enter 1 to 5 requests each with a start & end Date & time. I need to check that end time in each row before it tries to go to the next field because the user may not go to the next field. If a time does not pass the criteria I want to force re-entry of that field.

Can someone help me with this please.

Thanks,
Tamra
 
place your validation code at the OnExit event of each field. for instance, if you wanted to make sure the field was not null - at the OnExit event:

if Nz(me.BegDate1,0) = 0 then
docmd.cancelevent 'cancels the exit event
docmd.setfocus me.begdate1 'returns focus to field
msgbox "You must enter a date.",0+64,"Validation Error"
end if

there are other validations you can check for, but i do not know what they may be (begdate<enddate, beghour<endhour etc). use the onexit event to run your validation per field, or place it on the last field of the record to validate the whole record.

hope that helps

lee
 
I tried:

Private Sub cboEndHour1_Exit(Cancel As Integer)
If Nz(Me.cboEndHour1, 0) = 0 Then
cboEndHour1 = ""
DoCmd.CancelEvent 'cancels the exit event
Forms!MakeSchedule!cboEndHour1.SetFocus
MsgBox "End Hour Required."
' exit sub
end if
end sub

I also have:

Private Sub cboEndHour1_LostFocus()

If cboEndHour1 < cboStartHour1 Then
If EndDate1 = StartDate1 Then
cboEndHour1 = ""
MsgBox "To Create this Time span, End Date Must be greater than Start " & _
"Date."
EndDate1 = ""
Forms!MakeSchedule!EndDate1.SetFocus
End If
End If
End Sub

I even tried the two 'Subs' combined as one:
If Nz(Me.cboEndHour1, 0) = 0 Then
...
' exit sub
end if
If cboEndHour1 < cboStartHour1 Then
..
MsgBox "To Create this Time span, End Date Must be greater than Start " & _
"Date."
...
End If
End If


with the "exit sub"

no matter what I do, the error defaults to
"To Create this time span...."

If the cboEndTime1 field is blank, I want it to re-set with the msgbox "End Time Required." and not check other errors.
 
Well, the simplest things are the hardest to find. The code to check the endDate worked the first time. Then when I checked for endtime<starttime (valid if endDate > startDate) if it was invalid, I moved "" to EndHour & EndDate, then set focus to endDate. When it got to start time however it no longer worked.

The stupid (or easy) part... I was checking
"IsNull(EndTime)". It turns out, much to my frustration, the two are not the same. I set the fields to null, put the code in the OnExit sub & everything works fine.

Thanks everyone for the help.

Tamra
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top