set focus help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
mhmaid said:
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

You cannot cancel in BeforeUpdate and move focus to another control. Either
cancel and force the user to change the end date value or display a message
without cancelling and set focus to the start date control in which case you
want to use AfterUpdate rather than BeforeUpdate.
 
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
 
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 <--
 
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 .
 
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.
 
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
 

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

Back
Top