A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Form Setfocus



 
 
Thread Tools Display Modes
  #1  
Old February 5th, 2007, 05:01 PM posted to microsoft.public.access.forms
JohnB
external usenet poster
 
Posts: 185
Default Form Setfocus

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
  #2  
Old February 5th, 2007, 07:40 PM posted to microsoft.public.access.forms
Klatuu
external usenet poster
 
Posts: 7,074
Default Form Setfocus

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.
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

  #3  
Old February 6th, 2007, 09:28 AM posted to microsoft.public.access.forms
JohnB
external usenet poster
 
Posts: 185
Default Form Setfocus

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

"Klatuu" wrote:

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.
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

  #4  
Old February 6th, 2007, 02:08 PM posted to microsoft.public.access.forms
Klatuu
external usenet poster
 
Posts: 7,074
Default Form Setfocus

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?
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

"Klatuu" wrote:

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.
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

  #5  
Old February 6th, 2007, 02:22 PM posted to microsoft.public.access.forms
JohnB
external usenet poster
 
Posts: 185
Default Form Setfocus

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

"Klatuu" wrote:

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?
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

"Klatuu" wrote:

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.
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

  #6  
Old February 6th, 2007, 02:34 PM posted to microsoft.public.access.forms
Klatuu
external usenet poster
 
Posts: 7,074
Default Form Setfocus

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.

--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

"Klatuu" wrote:

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?
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

"Klatuu" wrote:

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.
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

  #7  
Old February 6th, 2007, 02:50 PM posted to microsoft.public.access.forms
JohnB
external usenet poster
 
Posts: 185
Default Form Setfocus

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
"Klatuu" wrote:

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.

--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

"Klatuu" wrote:

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?
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

"Klatuu" wrote:

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.
--
Dave Hargis, Microsoft Access MVP


"johnb" wrote:

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

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 11:25 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.