set focus help

M

mhmaid

I have two date fields. ExpenseStartDate, ExpenseEndDate.
when I enter the Start Date , the same date will be copied to the enddate.
and If for example I enter Start as 15nov06, and end is 14nov06 , a message
box will show "invalid Date" ,Cancel=true.

but actually in this case , I want the cursor to go back to the field
ExpenseStartDate.
I have triey me.Expensestartdate.setfocus but I got a msg you have to save
the field first.
what code I have to put for this , as i am new to VBA codes.

thanks
 
A

Allen Browne

For comparing the values between 2 fields, use the BeforeUpdate event of the
*Form*, not the events of the controls.

This kind of thing:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ExpenseEndDate < Me.ExpenseStartDate Then
Cancel = True
MsgBox "Expense cannot end before it starts."
Me.ExpenseStartDate.SetFocus
End If
End Sub
 
S

Stefan Hoffmann

hi,
I have two date fields. ExpenseStartDate, ExpenseEndDate.
when I enter the Start Date , the same date will be copied to the enddate.
and If for example I enter Start as 15nov06, and end is 14nov06 , a message
box will show "invalid Date" ,Cancel=true.
Which event are you using?

Instead of setting Cancel = True, you can use

Private Sub txtExpenseEndDate_AfterUpdate()

If [ExpenseEndDate] < [ExpenseStartDate] Then
txtExpenseEndDate.Value = txtExpenseEndDate.OldValue
txtExpenseStartDate.SetFocus
End If

End Sub


mfG
--> stefan <--
 
M

mhmaid

Thanks for all of you for help.
I have tried using the after update event and it is perfect.

and also I have tried to use the before update event of the "form" us
advised by mr allen brown , but so far I didnt succeed , may be because my
form is a subfom.

thanks .
 
A

Allen Browne

If the controls are in the subform, you need to use the BeforeUpdate event
of the subform.

If the form is unbound, its BeforeUpdate event won't do anything.
 
M

mhmaid

Allen Browne said:
If the controls are in the subform, you need to use the BeforeUpdate event
of the subform.

If the form is unbound, its BeforeUpdate event won't do anything.
thanks for your response. actully I have tried it in the beforeupdate of the
subform only. but unfortunatley I have made a stupid mistake ,I reversed the
position of the dates in the code , that why i was not getting correct
results.

thanks
 
Top