Form Setfocus

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

Guest

I have a Form with 2 date fields. I need to test if one date1 field is older
or newer date2. And if date2 is older that date1 then mark date2 Backcolor a
different color and set the focus to date2 field. Easy I here you say but not
for me! If I enter Me.Form.SetFocus followed by Me.txtEffectiveDate.SetFocus
in the After_Update event I get get nowhere.

Any Suggession??

TIA
johnb
 
The issue here is that before you can make the comparison, both fields need
to have a date in them. In addition, you have to decide what to do if one or
both date fields are Null. Not knowing the rule for this, I can only offer
this as a suggestion. Because you can't be sure which date the user will
enter first, I would suggest you create a Sub in your form module to test the
dates and call it from the After Update event of both fields:

Private Sub CheckDates()
If Me.Date2 < Me.Date1 Then
Me.Date2.BackColor = vbRed
Me.Date2.SetFocus
End If
End Sub

A note about setting focus to a form. You can't set the focus to a form if
there are any controls on the form capable of accepting the focus.
 
Hi Dave
Thanks for the reply. I'm happy with testing the 2 dates bit. I am using 1
of the date fields to do the testing, in the After_update event. The only bit
of code that fails to work is the Me.Date2.Setfocus. I can Setfocus to any
other control on the form but not the Date2 control. I guess I need a another
control to do the setfocus bit.

regards
johnb
 
I don't know why you can't set focus to that control. Is it enabled and not
locked? Is it in the Tab Order? Is is a normal bound control or is there
something special about it?
 
David,
Properties.Enabled = Yes and locked = No; The Form is bound to a table. The
Control in question is a Medium Date field. I've messed about with Setfocus
all morning and I can get it to focus on all other controls on the form
witout a any problem. It seems that if a control triggers an event then you
can not setfocus back to that control that triggered the event. I've
compacted etc. and compiled.....but no change!

Kind regards
johnb
 
Try adding this before you set the focus:

Private Sub CheckDates()
Dim ctl As Control

If Me.Date2 < Me.Date1 Then
Me.Date2.BackColor = vbRed
ctl = Screen.ActiveControl
If ctl.Name <> "Date2" Then
Me.Date2.SetFocus
End If
End If
End Sub

If Date2 has the focus, it will ignore setting the focus and raising the
error; otherwise, it will set the focus to Date2.
 
Hi David

I've got to work. Well its crap but its consistent. I've justed added
"Me.AnotherControlName.Setfocus" one line above the
Me.txtEffectiveDate.Setfocus and it WORKS!!!!

I've no idea why. I shall now try your solution.

Regards

johnb
 

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