Link to home
Start Free TrialLog in
Avatar of daintysally
daintysallyFlag for United States of America

asked on

How do I make a field on an access 2007 form required?

Hi Experts,

I am trying to make a field on my form a required field.  I have created the a form for the user to update.  I want to force the user to select a value from a combo box field on my form.  I have made the field a required field on the table's design view and I have put the code below in the BeforeUpdate event of the field.  When I test it, the message box appears, however, the focus is not placed back on the field that is required.  See the code below.  
******************************************************************************************************************
Private Sub ComboParty_BeforeUpdate(Cancel As Integer)
If IsNull(Me.ComboParty) = True Then
    MsgBox "This is a REQUIRED field.  Please make a selection", vbCritical + vbOKOnly + vbDefaultButton2, "REQUIRED DATA"
        Me.ComboParty.SetFocus
End If
End Sub
******************************************************************************************************************

Can anyone tell me why the focus will not return to the the ComboParty box?  I don't want the user to be able to tab down to the next field on the form if ComboParty is null.  Any help would be greatly appreciated as I am up against a tight deadline.
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

" I don't want the user to be able to tab down to the next field on the form if ComboParty is null."
Well ... first off, if user has not entered anything in the comb box, then the BU event will not even trigger ... so, I'm not seeing how the msgbox is going to fire at all.  This would only happen if ... there was an existing value, and the user backed it out.  And it that case, the focus *should* ... already be in the combo box.

?

mx
Avatar of daintysally

ASKER

Well ... first off, if user has not entered anything in the comb box, then the BU event will not even trigger ... so, I'm not seeing how the msgbox is going to fire at all.  This would only happen if ... there was an existing value, and the user backed it out.  And it that case, the focus *should* ... already be in the combo box.

mx, you are right....when I backed the value out of the field, I get the following error message:
Run-time error '2108'

You must save the field before you execute the GoToControl action, the GoToControl method, or the SetFocus method.

However, it allows the user to 'tab' over that field on the form, and I don't want it to do that.  Is there anyway to prevent the user from doing that?
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
SOLUTION
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
" Sometimes, say do to importing data into a table, setting a field(s) as Required is not practical.  There are other cases that come up also"
More on this.  We get reports daily from IT. That data gets imported into a table in a db. But for example, the analyst that ultimately works the account ... is not part of the imported data.  However, business rules dictate that .... the analyst name (as well as other fields) IS required once the record is worked (edited), and ultimately saved.  

So, you can see that I cannot make the Analyst Name field Required at the table level, but it is at the Form level.  In this case, the Form BU event would be used for final validation.

mx
Sorry ... there is a typo in the Form Error code above:

This
        Case 3314   'Required Field
            mgsbox "One or more required fields are missing. Please enter ALL required fields"

should be

        Case 3314   'Required Field
            MsgBox "One or more required fields are missing. Please enter ALL required fields"

mx
Thanks mx!!!  I am trying your suggestion now.
Avatar of AkAlan
AkAlan

I would code the pages Before Update event and then look at the combobox.

If IsNull(Me.ComboParty) = True Then
    MsgBox "This is a REQUIRED field.  Please make a selection", vbCritical + vbOKOnly + vbDefaultButton2, "REQUIRED DATA"
        Me.ComboParty.SetFocus
     Cancel = true   ' this stops the update
End If
AkAlan:
You should probably review what is been discussed already, because what you posted will not work.

mx
I reviewed the post. I use the method I described all the time and iir does work. Im not at my pc now but I'll post an example tomorrow.
Sorry, but you cannot SetFocus to any control when you are in the BU event of a Control.  If you do, you will get runtime error 2108 ... as mentioned @ http:#a35173389

And you posted the same thing the OP has - which does not work - except you added the Cancel event.

mx
I put the code in the forms bu event. As I said, I'll post an example db in the morning.
"I put the code in the forms bu event"
Sorry, I thought you were in the Control BU event. Anyway, I already posted the Form BU solution @ http:#a35173430

mx
No problem, I work with both Access and asp.net so I shouldn't have said page, I should have said Form so it would have been more obvious. I clicked your link but it just takes me up this page a bit and I don't see a soulution.
Thank you MX....this worked wonderfully!!!