Required Fields

B

Bill

I have a form with tabs. In the header of the form I have a check box
[chkWorkComp], that if checked I want certain fields [Job Title] in the tab
[IR_Sub_WC_Employment] to be required fields.

Right now the tab is visible only when the box is checked by doing an
afterupdate sub which changes the property "Visible" to True or False.

How can I affect the fields in a totally different form based on the check
box?

Thank you for your help,

Bill
 
D

dymondjack

Control properties in a completely different form can be fairly easily
modified (assuming the form is open). See the following link for the syntax.

http://www.mvps.org/access/forms/frm0031.htm


For the record... controls on separate tabs of the same form need no special
syntax (Me.ctlname works regardless of the tab).

--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery
 
B

Bill

Well I've bookmarked your site as it offers a great deal of information. I
am still confused by how to refer this correctly. The main form is called
[IR_Main] then in the detail I've inserted other forms via tabs. The second
form is called [IR_Sub_WC_Employment]. In [IR_Main] I have a check box
called [chkWorkComp]. When this is checked the tab [IR_Sub_WC_Employment] is
revealed. On this tab (form) I want require certain fields to be completed.
So based on your website I've created the following:

Private Sub Sched_Start_Time_BeforeUpdate(Cancel As Integer)
If Forms!IR_Main.chkWorkComp = True And (IsNull(Me.Sched_Start_Time) Or
Me.Sched_Start_Time = "") Then
MsgBox "Must fill Sched_Start_Time"
Me.Sched_Start_Time.SetFocus
Cancel = True
End If
End Sub

I can't get it to validate the box. If I tab through the form I can tab
right past it without issue. When I enter data it return an error. So what
am I doing wrong?

Thanks for all the help.
dymondjack said:
Control properties in a completely different form can be fairly easily
modified (assuming the form is open). See the following link for the syntax.

http://www.mvps.org/access/forms/frm0031.htm


For the record... controls on separate tabs of the same form need no special
syntax (Me.ctlname works regardless of the tab).

--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery


Bill said:
I have a form with tabs. In the header of the form I have a check box
[chkWorkComp], that if checked I want certain fields [Job Title] in the tab
[IR_Sub_WC_Employment] to be required fields.

Right now the tab is visible only when the box is checked by doing an
afterupdate sub which changes the property "Visible" to True or False.

How can I affect the fields in a totally different form based on the check
box?

Thank you for your help,

Bill
 
D

dymondjack

When tabbing through controls on a form, the beforeupdate event will not
fire. However, if you enter any data to a control and it loses focus, the
BeforeUpdate event of the control will fire.

Keep in mind that when you refer to the BeforeUpdate property of a control,
typing Me.ctlName (which is, for your purposes the same as Me.ctlName.Value)
will return whatever was in the control before you entered information (the
control value has not yet updated). To get around this, use Me.ctlName.Text
(which will return the text currently in the control, regardless of wether it
is saved or not). Remember also that the control needs to have the focus to
get the .Text value (Me.ctlName.SetFocus).

Also, I believe there is an error with this line:
If Forms!IR_Main.chkWorkComp

Try:

Me.Parent!chkWorkComp

(I'm assuming the control Sched_Start_Time is on the subform, and
chkWorkComp is on the mainform).


hth



--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery


Bill said:
Well I've bookmarked your site as it offers a great deal of information. I
am still confused by how to refer this correctly. The main form is called
[IR_Main] then in the detail I've inserted other forms via tabs. The second
form is called [IR_Sub_WC_Employment]. In [IR_Main] I have a check box
called [chkWorkComp]. When this is checked the tab [IR_Sub_WC_Employment] is
revealed. On this tab (form) I want require certain fields to be completed.
So based on your website I've created the following:

Private Sub Sched_Start_Time_BeforeUpdate(Cancel As Integer)
If Forms!IR_Main.chkWorkComp = True And (IsNull(Me.Sched_Start_Time) Or
Me.Sched_Start_Time = "") Then
MsgBox "Must fill Sched_Start_Time"
Me.Sched_Start_Time.SetFocus
Cancel = True
End If
End Sub

I can't get it to validate the box. If I tab through the form I can tab
right past it without issue. When I enter data it return an error. So what
am I doing wrong?

Thanks for all the help.
dymondjack said:
Control properties in a completely different form can be fairly easily
modified (assuming the form is open). See the following link for the syntax.

http://www.mvps.org/access/forms/frm0031.htm


For the record... controls on separate tabs of the same form need no special
syntax (Me.ctlname works regardless of the tab).

--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery


Bill said:
I have a form with tabs. In the header of the form I have a check box
[chkWorkComp], that if checked I want certain fields [Job Title] in the tab
[IR_Sub_WC_Employment] to be required fields.

Right now the tab is visible only when the box is checked by doing an
afterupdate sub which changes the property "Visible" to True or False.

How can I affect the fields in a totally different form based on the check
box?

Thank you for your help,

Bill
 
D

dymondjack

You should probably change your logical operator setup as well (And/Or's).
These can be pretty tricky, so I always make it a point to parenthesize each
condition for clarification. The way access interprets them without being in
parentheses usually is not what we would expect.

Try this:

If (Me.Parent!chkWorkComp = True) And _
(Nz(Me.Sched_Start_Time, "") = "") Then
...
...
End If

The Nz function gets rid of the requirement for checking both IsNull and for
a zero-length string. If, by chance, you really did want to check both
IsNull and ZLS, you would need:

If ((Me.Parent!chkWorkComp = True) And (IsNull(Me.Sched_Start_Time))) Or _
((Me.Parent!chkWorkComp = True) And (Me.Sched_Start_Time = "")) Then
...
...
...
End If




--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery


dymondjack said:
When tabbing through controls on a form, the beforeupdate event will not
fire. However, if you enter any data to a control and it loses focus, the
BeforeUpdate event of the control will fire.

Keep in mind that when you refer to the BeforeUpdate property of a control,
typing Me.ctlName (which is, for your purposes the same as Me.ctlName.Value)
will return whatever was in the control before you entered information (the
control value has not yet updated). To get around this, use Me.ctlName.Text
(which will return the text currently in the control, regardless of wether it
is saved or not). Remember also that the control needs to have the focus to
get the .Text value (Me.ctlName.SetFocus).

Also, I believe there is an error with this line:
If Forms!IR_Main.chkWorkComp

Try:

Me.Parent!chkWorkComp

(I'm assuming the control Sched_Start_Time is on the subform, and
chkWorkComp is on the mainform).


hth



--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery


Bill said:
Well I've bookmarked your site as it offers a great deal of information. I
am still confused by how to refer this correctly. The main form is called
[IR_Main] then in the detail I've inserted other forms via tabs. The second
form is called [IR_Sub_WC_Employment]. In [IR_Main] I have a check box
called [chkWorkComp]. When this is checked the tab [IR_Sub_WC_Employment] is
revealed. On this tab (form) I want require certain fields to be completed.
So based on your website I've created the following:

Private Sub Sched_Start_Time_BeforeUpdate(Cancel As Integer)
If Forms!IR_Main.chkWorkComp = True And (IsNull(Me.Sched_Start_Time) Or
Me.Sched_Start_Time = "") Then
MsgBox "Must fill Sched_Start_Time"
Me.Sched_Start_Time.SetFocus
Cancel = True
End If
End Sub

I can't get it to validate the box. If I tab through the form I can tab
right past it without issue. When I enter data it return an error. So what
am I doing wrong?

Thanks for all the help.
dymondjack said:
Control properties in a completely different form can be fairly easily
modified (assuming the form is open). See the following link for the syntax.

http://www.mvps.org/access/forms/frm0031.htm


For the record... controls on separate tabs of the same form need no special
syntax (Me.ctlname works regardless of the tab).

--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery


:

I have a form with tabs. In the header of the form I have a check box
[chkWorkComp], that if checked I want certain fields [Job Title] in the tab
[IR_Sub_WC_Employment] to be required fields.

Right now the tab is visible only when the box is checked by doing an
afterupdate sub which changes the property "Visible" to True or False.

How can I affect the fields in a totally different form based on the check
box?

Thank you for your help,

Bill
 
D

dymondjack

Correction:

The Nz function gets rid of the requirement for checking both IsNull and for
a zero-length string.

This statement is incorrect. In order to use it this way you would need to
check the length of the string:

(Len(Nz(Me.Sched_Start_Time, "")) = 0)

That will return correctly regardless of Null or ZLS.

Sorry for any confusion

--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery
 
B

Bill

Ok. So this is what I came up with and your right, nothing seems to work.
The field is skipped without any question.

Private Sub EmployeeID_BeforeUpdate(Cancel As Integer)
If (Me.Parent!chkWorkComp = True) And _
(Len(Nz(Me.EmployeeID, "")) = 0) Then
MsgBox "Must fill EmployeeID"
Me.EmployeeID.SetFocus
Cancel = True
End If
End Sub
 
D

dymondjack

I think maybe there's a better way to go about this whole thing. If I
understand correctly, you want to make sure that, if chkWorkComp is true,
various other fields have information before the record is saved.

Generally this is much easier and more elegant through the form's
BeforeUpdate event, rather than trying to catch it in control events. The
form's BeforeUpdate event will always trigger if there has been a change
made... regardless of the record is saved (moving on to the next record,
closing the form, hitting a save button, closing access, everything except a
complete system crash). Furthermore, the form's BeforeUpdate event also
gives you the option to cancel.

So, if there's anything at all you want to make sure of before a record is
saved, the form's BeforeUpdate event is the idea place to check for it.
Assuming you are familiar with error handling and exit points in code, here's
the skeleton of how to effectively use this procedure:


'CODE START
Public Sub Before_Update(Cancel As Integer)
On Error Goto Err_Handler

Dim iCancel As Integer
iCancel = 0

'Put all of your validation code here
'Example:
If Me.chkWorkComp = False Then
MsgBox "Please check Work Comp before saving"
Goto Exit_Code
End If
'...
'...
'...

iCancel = 1

Exit_Code:
If iCancel = 0 Then
Cancel = True
End If
Exit Sub
Err_Handler:
'Typical Error handling code here...
Resume Exit_Code
Resume
End Sub
'CODE END

Here's how it works:

You'll have this iCancel variable, which you set to 0 before doing anything
else (It should default to that as soon as it is dimensioned, but we're not
going to count on it). When we get to the exit point of the sub, if it see's
that iCancel is still 0, its not going to let the save happen.

The first part of the trick here is to NOT set iCancel to 1 (or anything
besides 0) everything else in the sub is confirmed. This line should always
be the last line before before the exit point. The second part of the trick
is to use the Goto Exit_Code line any time a validation fails. That way, it
jumps right past the iCancel = 1 line, and the update is cancelled. (In case
you weren't aware, you should never have more than one exit point in a
procedure).

If you do these two things, the ONLY possible way that this record will
update is if every single validation expression you enter returns the way you
want it to. Even if your code runs into an unexpected error, the record will
not update.


The next problem you are going to run into deals with switching focus
between the main form and subform. I am *almost* positive that whenever you
change the focus from one to the other, whichever form you leave will save
(the BeforeUpdate for that form will run). I say this because it would stand
to reason that when you start a new record in the main form, that record
needs to be saved before you can start entering a related record in the
subform.

Based on this, you will need to make a decision on which form (main or sub)
that you would like to validate information on. *If* this is true, you will
not be able to validate controls on a subform when you are working with a new
record on the main form (the main form BeforeUpdate will fire when you switch
to the subform, and subform control validations will fail because the user
hasn't had a chance to get to the subform to edit those records yet). So,
you may want to use the subform's BeforeUpdate event rather than the
Mainforms (although, this will allow you to save a record in the mainform
without validation on subform data). As far as I know, there is no real way
to get around that.


If, by chance, you do want to validate this on a control-by-control basis,
consider using the control's OnExit event, rather than BeforeUpdate.



--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top