Link to home
Start Free TrialLog in
Avatar of mbmartin0409
mbmartin0409Flag for United States of America

asked on

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

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!
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

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
Avatar of mbmartin0409

ASKER

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

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
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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!
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
thanks for the help and enjoy the points! :-)
You are welcome ... and thanks, you put me on the board for this Month, lol.