Required Fields

  • Thread starter Thread starter Ann
  • Start date Start date
A

Ann

I am having a really hard time with required fields. First I made them
required in the table but if i tried to make a form that didn't use all the
fields from the table nothing worked because I was always told I had required
fields that needed to be entered even though they weren't on the form. OK, I
get that since they are part of the record in the table. Then I tried the
code listed below on as many of the events that made sense and it never
worked.

If IsNull(Me.txtContainer) Then
MsgBox "Required Field!"
Cancel = True
txtContainer.SetFocus
End If

Then I tried putting ([txtContainer] is not null) on the Validation Rule
property. That works if I bring up an existing record and remove what is
there but if I try to bypass the field on a new record it lets me. Can
someone help me out with how this is supposed to work. I really thought all
I had to do was make the field required in the table and that was it. Thanks
for the help.
 
Put your code into the BeforeUpate event of the *form* (i.e. not the events
of the controls.)
 
Thank you that did work. I had it on the control because I wanted the
message to pop up right after they left the field. If I put it on the form
it is coming up but it happens before it moves to the subform. Is the code
written wrong to be placed on the BeforeUpdate of the control? I'm just
learning to code so I'm not really sure.
Thanks.

Allen Browne said:
Put your code into the BeforeUpate event of the *form* (i.e. not the events
of the controls.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.


Ann said:
I am having a really hard time with required fields. First I made them
required in the table but if i tried to make a form that didn't use all
the
fields from the table nothing worked because I was always told I had
required
fields that needed to be entered even though they weren't on the form.
OK, I
get that since they are part of the record in the table. Then I tried the
code listed below on as many of the events that made sense and it never
worked.

If IsNull(Me.txtContainer) Then
MsgBox "Required Field!"
Cancel = True
txtContainer.SetFocus
End If

Then I tried putting ([txtContainer] is not null) on the Validation Rule
property. That works if I bring up an existing record and remove what is
there but if I try to bypass the field on a new record it lets me. Can
someone help me out with how this is supposed to work. I really thought
all
I had to do was make the field required in the table and that was it.
Thanks
for the help.
 
Depending on what you want to do, it can be in either the control or the form
before update events.

The reason, in this case, to use the form event is because if you don't set
the focus to the control or don't enter anything in it, the code will never
execute and the error will be raised. When it is in the Form event, you
can't move off the record until it is correct.
--
Dave Hargis, Microsoft Access MVP


Ann said:
Thank you that did work. I had it on the control because I wanted the
message to pop up right after they left the field. If I put it on the form
it is coming up but it happens before it moves to the subform. Is the code
written wrong to be placed on the BeforeUpdate of the control? I'm just
learning to code so I'm not really sure.
Thanks.

Allen Browne said:
Put your code into the BeforeUpate event of the *form* (i.e. not the events
of the controls.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.


Ann said:
I am having a really hard time with required fields. First I made them
required in the table but if i tried to make a form that didn't use all
the
fields from the table nothing worked because I was always told I had
required
fields that needed to be entered even though they weren't on the form.
OK, I
get that since they are part of the record in the table. Then I tried the
code listed below on as many of the events that made sense and it never
worked.

If IsNull(Me.txtContainer) Then
MsgBox "Required Field!"
Cancel = True
txtContainer.SetFocus
End If

Then I tried putting ([txtContainer] is not null) on the Validation Rule
property. That works if I bring up an existing record and remove what is
there but if I try to bypass the field on a new record it lets me. Can
someone help me out with how this is supposed to work. I really thought
all
I had to do was make the field required in the table and that was it.
Thanks
for the help.
 
Back
Top