Changing required property

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi! I have an order form and items subform in which I'd like 3 subform fields
to be required if a particular customer is selected in the main form.
Otherwise, they are not required. How do I change that in code?
Thanks!
 
I would just use the BeforeUpdate event of the subform to verify that the
fields are entered. The quick way would be to do something like this:

If me.custid=1234 then
if isnull(me.field1) then
msgbox "Field1 is required"
me.field1.setfocus
cancel=true
elseif isnull(me.field2) then
msgbox "Field2 is required"
me.field2.setfocus
cancel=true
elseif isnull(me.field3) then
msgbox "Field3 is required"
me.field3.setfocus
cancel=true
endif
endif

In reality, I would avoid hardcoding the custid. Instead I would add a
yes/no field to the customer table to indicate whether to take this step
then check that value instead of the Custid.
 
Ah, thank you - it was the cancel = true I was missing, but putting the code
in the beforeupdate event of the subform won't trigger it until they've
already tabbed through the fields - I was hoping to do it on each field as
they try to tab without entering data - so I put it in the lost focus event
of each field, but couldn't get it to set the focus back on the required
field.

Sandra Daigle said:
I would just use the BeforeUpdate event of the subform to verify that the
fields are entered. The quick way would be to do something like this:

If me.custid=1234 then
if isnull(me.field1) then
msgbox "Field1 is required"
me.field1.setfocus
cancel=true
elseif isnull(me.field2) then
msgbox "Field2 is required"
me.field2.setfocus
cancel=true
elseif isnull(me.field3) then
msgbox "Field3 is required"
me.field3.setfocus
cancel=true
endif
endif

In reality, I would avoid hardcoding the custid. Instead I would add a
yes/no field to the customer table to indicate whether to take this step
then check that value instead of the Custid.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Mary said:
Hi! I have an order form and items subform in which I'd like 3
subform fields to be required if a particular customer is selected in
the main form. Otherwise, they are not required. How do I change
that in code?
Thanks!
 
You can use the Exit event of the control - this is also a cancellable event
(use Cancel=True). When you cancel this event the focus remains in the
control. The problem with using this event is that it is a little
frustrating for the user - because it won't allow the user to move out of
the control to do anything else.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Mary said:
Ah, thank you - it was the cancel = true I was missing, but putting the
code in the beforeupdate event of the subform won't trigger it until
they've already tabbed through the fields - I was hoping to do it on each
field as they try to tab without entering data - so I put it in the lost
focus event of each field, but couldn't get it to set the focus back on
the required field.

Sandra Daigle said:
I would just use the BeforeUpdate event of the subform to verify that the
fields are entered. The quick way would be to do something like this:

If me.custid=1234 then
if isnull(me.field1) then
msgbox "Field1 is required"
me.field1.setfocus
cancel=true
elseif isnull(me.field2) then
msgbox "Field2 is required"
me.field2.setfocus
cancel=true
elseif isnull(me.field3) then
msgbox "Field3 is required"
me.field3.setfocus
cancel=true
endif
endif

In reality, I would avoid hardcoding the custid. Instead I would add a
yes/no field to the customer table to indicate whether to take this step
then check that value instead of the Custid.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Mary said:
Hi! I have an order form and items subform in which I'd like 3
subform fields to be required if a particular customer is selected in
the main form. Otherwise, they are not required. How do I change
that in code?
Thanks!
 
You could also use the Exit event of the control which can be cancelled.
When you set cancel=true the focus remains in the control. I personally
don't like this method as much because it can be a little frustrating for
the user.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Mary said:
Ah, thank you - it was the cancel = true I was missing, but putting
the code in the beforeupdate event of the subform won't trigger it
until they've already tabbed through the fields - I was hoping to do
it on each field as they try to tab without entering data - so I put
it in the lost focus event of each field, but couldn't get it to set
the focus back on the required field.

Sandra Daigle said:
I would just use the BeforeUpdate event of the subform to verify
that the fields are entered. The quick way would be to do something
like this:

If me.custid=1234 then
if isnull(me.field1) then
msgbox "Field1 is required"
me.field1.setfocus
cancel=true
elseif isnull(me.field2) then
msgbox "Field2 is required"
me.field2.setfocus
cancel=true
elseif isnull(me.field3) then
msgbox "Field3 is required"
me.field3.setfocus
cancel=true
endif
endif

In reality, I would avoid hardcoding the custid. Instead I would add
a yes/no field to the customer table to indicate whether to take
this step then check that value instead of the Custid.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Mary said:
Hi! I have an order form and items subform in which I'd like 3
subform fields to be required if a particular customer is selected
in the main form. Otherwise, they are not required. How do I change
that in code?
Thanks!
 
Note that even if you use the Exit event of the form you will probably need
to duplicate the code (or put it into a validation function that you call
from both places) in the BeforeUpdate event since it is conceivable that a
user will use the mouse to skip around to different fields.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Sandra said:
You could also use the Exit event of the control which can be
cancelled. When you set cancel=true the focus remains in the control.
I personally don't like this method as much because it can be a
little frustrating for the user.


Mary said:
Ah, thank you - it was the cancel = true I was missing, but putting
the code in the beforeupdate event of the subform won't trigger it
until they've already tabbed through the fields - I was hoping to do
it on each field as they try to tab without entering data - so I put
it in the lost focus event of each field, but couldn't get it to set
the focus back on the required field.

Sandra Daigle said:
I would just use the BeforeUpdate event of the subform to verify
that the fields are entered. The quick way would be to do something
like this:

If me.custid=1234 then
if isnull(me.field1) then
msgbox "Field1 is required"
me.field1.setfocus
cancel=true
elseif isnull(me.field2) then
msgbox "Field2 is required"
me.field2.setfocus
cancel=true
elseif isnull(me.field3) then
msgbox "Field3 is required"
me.field3.setfocus
cancel=true
endif
endif

In reality, I would avoid hardcoding the custid. Instead I would add
a yes/no field to the customer table to indicate whether to take
this step then check that value instead of the Custid.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Mary Fran wrote:
Hi! I have an order form and items subform in which I'd like 3
subform fields to be required if a particular customer is selected
in the main form. Otherwise, they are not required. How do I
change that in code?
Thanks!
 
Back
Top