Access 2010: Requiring an entry in a field IF a certain selection is made from another cbobox.

mbmartin0409
mbmartin0409 used Ask the Experts™
on
In my db I have a combo box with several choices.  If user selects "Current HS" , then I want to REQUIRE the field [HSGradYr] to be entered.  

I have the following before update code that PARTIALLY works - but stops.

Private Sub StatusReason_BeforeUpdate(Cancel As Integer)
If Me.StatusReason = "Current HS" Then
  If Len(Me.HSGradYr & vbNullString) = 0 Then
    MsgBox "If you use this reason, you must enter a HS Grad Yr"
    Cancel = True
    Me.HSGradYr.SetFocus
  End If
End If
End Sub

When I make that selection in StatusReason, two things happen.  First I get the proper message box I asked for and then this:

"Run-time error '2108'
You must save the field before you execute the GoToControl action, the GoToControl method, or the SetFocus method."

Clearly I've left something out of my code -

HELP?!?!?!?!  

thanks everyone!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
You cannot set Focus to a control within the Before Update of a control


Private Sub StatusReason_BeforeUpdate(Cancel As Integer)
If Me.StatusReason = "Current HS" Then
  If Len(Me.HSGradYr & vbNullString) = 0 Then
    MsgBox "If you use this reason, you must enter a HS Grad Yr"
    Cancel = True
      ' Me.HSGradYr.SetFocus  ' Comment this out
  End If
End If
End Sub

mx

Author

Commented:
Thanks for that ---
but then --- how DO i get the user's focus on the field that needs to be completed?

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
Well ... you can move this code to the Form Before Update event instead, because there you *can* set Focus to another control.  That's just how it works ...

mx
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Database Architect / Application Developer
Top Expert 2007
Commented:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.StatusReason = "Current HS" Then
  If Len(Me.HSGradYr & vbNullString) = 0 Then
    MsgBox "If you use this reason, you must enter a HS Grad Yr"
    Cancel = True
      Me.HSGradYr.SetFocus  ' This should work now .
  End If
End If
End Sub

Author

Commented:
also -  Since I have commented that line out,  The run-time error is gone, of course - BUT when I click "ok" on the message box to go enter the required year in the field the same requirement message keeps popping up.  as soon as I leave the field with the beforeupdate code the message pops

any ideas?

Author

Commented:
Excellent - It seems to work fine putting this code in the form before update event.  I'll keep trying to screw it up but it seems to be just perfect!
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
You should find that the Form BU is the best place for  validation over all.  Other than maybe for example someone is entering a Date in a text box, and you want to be sure it's a valid date ... or whatever.

mx

Author

Commented:
thanks for the help and enjoy the points! :-)
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
You are welcome ... and thanks, you put me on the board for this Month, lol.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial