Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-03
9
Medium Priority
?
493 Views
Last Modified: 2012-05-12
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!
0
Comment
Question by:mbmartin0409
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 75
ID: 36478680
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
0
 

Author Comment

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

0
 
LVL 75
ID: 36478765
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
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 36478767

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
0
 

Author Comment

by:mbmartin0409
ID: 36478775
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?
0
 

Author Comment

by:mbmartin0409
ID: 36478788
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!
0
 
LVL 75
ID: 36478800
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
0
 

Author Comment

by:mbmartin0409
ID: 36478852
thanks for the help and enjoy the points! :-)
0
 
LVL 75
ID: 36478891
You are welcome ... and thanks, you put me on the board for this Month, lol.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question