SetFocus not working (1 Viewer)

dhunter

Registered User.
Local time
Today, 02:39
Joined
Jul 1, 2009
Messages
26
If a user chooses no on a msg box prompt I want the focus to go back to the control that the user was updating. I don't get any errors but the focus doesn't get set back to the control. Both controls are on a subform and i have tried the DoCmd.GotoControl to setfocus on the subform before setfocus to the completed date control but that does the same thing.

Here is my code:

Private Sub Completed_Date_AfterUpdate()

'if the fbm is earlier than the comp date entered, ask user if they want to set the first billable month to new date specified by completed date.

If Me.First_Billable_Month.Value > Me.Completed_Date.Value Then

Dim Msg, Style, Title, Help, Ctxt, response, MyString

Msg = "The date you have specified is earlier than the First Billable Month. Do you want to overwrite the First Billable Month?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "First Billable Month"

response = MsgBox(Msg, Style, Title)

If response = vbYes Then ' User chose Yes.
MyString = "Yes"
Me.First_Billable_Month.Value = Me.Completed_Date.Value
Else
MyString = "No" ' User chose No.
Me.Completed_Date.Value = ""
Me.Completed_Date.SetFocus
End If

End If

End Sub

Maybe I should put this code in the event proc for On Dirty instead?
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:39
Joined
May 13, 2009
Messages
93
I learned to call a random control for SetFocus and then the true control for SetFocus.
Code:
Me.First_Billable_Month.SetFocus 'random control
Me.Completed_Date.SetFocus 'true control
It happens so fast, you wont see it.
 

missinglinq

AWF VIP
Local time
Today, 03:39
Joined
Jun 20, 2003
Messages
6,423
Rick's suggestion is correct if you want to set the focus on a control from that control's AfterUpdate event, but in point of fact this is not the best event to use for this type of code.

What will happen if the user has not yet entered a date into the First_Billable_Month textbox before entering the Completed_Date date? And what if they later enter a date in First_Billable_Month that comes after Completed_Date? Your code will be useless.

You need to do this kind of validation in the Form_BeforeUpdate when you know that the user is has entered all the data they plan to enter and are ready to save the record. Also, when using this in Form_BeforeUpdate event, you'll want to change

Me.Completed_Date.Value = ""
Me.Completed_Date.SetFocus

to

Me.Completed_Date.Value = ""
Cancel = True

Me.Completed_Date.SetFocus

so that the record will bot be saved until the correction is made.
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:39
Joined
May 13, 2009
Messages
93
Thanks for expanding on this, Im pretty sure it was one of your posts I read about SetFocus.
 

Users who are viewing this thread

Top Bottom