vbYesNo

renenger

Registered User.
Local time
Today, 12:00
Joined
Oct 25, 2002
Messages
117
I am trying to notify users if they schedule a delivery on a weekend. I have the code below. However, it doesn't set focus back on NewDate. It skips ahead to Comments. What am I missing?

Private Sub NewDate_AfterUpdate()
If Format(Me.NewDate, "ddd") = "Sat" Then
If MsgBox("You have a chosen to deliver on a Saturday! Are you sure?", vbYesNo, "Scheduling New Date") = vbNo Then
Me![NewDate].SetFocus
Else
End If
End If
End Sub
 
You need to assign the return vaule to a string.

DIM VBAnsw as string

VBAnsw = MsgBox("You have a chosen to deliver on a Saturday! Are you sure?", vbYesNo, "Scheduling New Date")

If VBAnsw = vbYes then
yada yada yada
else
nada nada nada
 
I tried your code. However, I still can't get it to reset the focus back to the NewDate field.

Private Sub NewDate_AfterUpdate()
Dim VBAnsw As String
If Format(Me.NewDate, "ddd") = "Sat" Then
VBAnsw = MsgBox("You have a chosen to deliver on a Saturday! Are you sure?", vbYesNo, "Scheduling Date")
If VBAnsw = vbYes Then
Me![NewDate].SetFocus
Else
End If
End If

End Sub
 
Try changing the

me.[newdate].setfocus

to

frm1.newdate.setfocus

where frm1 is the actual form name the field resides on.
 
I still can't get it to reset back to NewDate. I can't figure out why.

I have tried

Me.NewDate.SetFocus
frmSpecificLotHist.NewDate.SetFocus
Me.[NewDate].SetFocus
 
Another one to try. No sure it will work, cause it sounds like something else is going on, but you can try.

Forms![frmSpecificLotHist]![NewDate].SetFocus

HTH,
Shane
 
The reason it doesn’t work is because, at the time of the AfterUpdate event, the control still has the focus (the lost focus event hasn’t fired) and setting focus to itself does nothing.

However, setting focus to another control and then back to itself does work.

Hope that helps.

Regards,
Chris.
 
Better yet, put your code in the BeforeUpdate event (where it really belongs) and just set Cancel = True to keep the focus on NewDate. Forget the SetFocus stuff all together.
 
Well that depends on if the control is bound or not.

If the control is unbound then it will require an Undo to prevent an Access error, so: -

Me.NewDate.Undo
Cancel = True

would be required before the Cancel.

As far as I can tell the If bound or not has not been specified as yet and so is still an open argument.

Secondly, the main question seems to be why we can’t set focus to a control that has already got the focus.

To construct a workaround, that avoids the question, does not answer the question but simply bypasses the question…that in itself reduces knowledge.

In hindsight we shall learn if the control is bound or not but hindsight is purely wishing on the past being better defined at the time.

Let’s try to remove absolutes from our statements and see what hindsight brings. ;)

Regards,
Chris.
 
i have been trying all methods in the posts i have searched on this, to get the focus back to a control which initiated a msgbox,
the control is unbound
when i click ok to close the msgbox, i can see the focus go back to the control (the one i want it at) and move to the next control as per the tab order.
i want to keep the tab order for entry,
here is the code i am using on the control BeforeUpdate,

Dim strMsg As String
Dim Answer As String
Msg = strMsg
Answer = MsgBox(strMsg, vbInformation + vbOKOnly, "Turbine: " & turbineIdent & " Entry Error!")
If Answer = vbOKOnly Then
Me.turbineHourRun.Undo
Cancel = True
End If

would appreciate some help on this
 
Last edited:
If the control is unbound then it will require an Undo to prevent an Access error, so: -

Me.NewDate.Undo
Cancel = True
Not necessarily. In some cases, the undo is fine because the user is going to enter a different choice but in the case where the user just has to change what he typed like this case, it would be a real annoyance to keep erasing his entry rather than allowing him to fix it. Plus, if you undo the value, you can't stop the tab from moving on. So, I almost NEVER use an undo in this situation.
Code:
Private Sub NewDate_BeforeUpdate()
If Format(Me.NewDate, "ddd") = "Sat" Then
    If MsgBox("You have a chosen to deliver on a Saturday! Are you sure?", vbYesNo, "Scheduling New Date") = vbNo Then
        Cancel = True
    Else
    End If
End If
End Sub
Using this code, focus will never leave the NewDate event until the entry is corrected.
 

Users who are viewing this thread

Back
Top Bottom