RE: Required Fields in main form with subform
- From: Klatuu <Klatuu@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 24 Jul 2009 11:01:01 -0700
Your assumption on the array is correct. You use the names of the controls.
Assuming all three are text boxes, I don't see a problem.
When you get the message box, is the name of the control the first thing in
the message?
--
Dave Hargis, Microsoft Access MVP
"Tina Hudson" wrote:
I'm getting error message: "method or data member not found" on the line:.
.ctl.SetFocus
I wasn't sure about the Array("txtBox1","txtBox5", "tstBox11"), but I
substituted with the 3 fields that are required - First, Last and Pin.
Here is my complete code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim aryCtl As Variant
Dim lngCtr As Long
Dim ctl As Control
aryCtl = Array("First", "Last", "PIN")
With Me
For lngCtr = 0 To 2
Set ctl = .Controls(aryCtl(lngCtr))
If IsNull(ctl) Then
Cancel = True
If MsgBox(ctl & "Is Required" & "Click Yes to Correct or No
to Cancel Update", vbQuestion + vbYesNo) = vbYes Then
.ctl.SetFocus
Else
Me.Undo
End If
Exit For
End If
End With
End Sub
--
Thanks,
Tina Hudson
"Klatuu" wrote:
When you move from a main form to its subform, the main form's data will be
updated. That is why you are getting the error. You can't just hide the
error, because you will not be able to move off the main form until the
fields have data.
I would suggest you use the form's Before Update event to check the required
fields for valid data before you allow the update. Typically, you will show
the user a message box telling them what the problem is so they can correct
the error before proceeding. If there are errors, cancel the update. This
example (untested air code) will allow the user to correct the error or
cancel any changes made:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim aryCtl As Variant
Dim lngCtr As Long
Dim ctl As Control
aryCtl = Array("txtBox1","txtBox5", "tstBox11")
With Me
For lngCtr = 0 To 2
Set ctl = .Controls(aryCtl(lngCtr))
If IsNull(ctl) Then
Cancel = True
If MsgBox ctl & " Is Required " & vbNewLine & _
"Click Yes to Correct Or No to Cancel Update", _
vbQuestion + vbYesNo) = vbYes Then
.ctl.SetFocus
Else
Me.Undo
End If
Exit For
End If
End With
End Sub
--
Dave Hargis, Microsoft Access MVP
"Tina Hudson" wrote:
Good morning,
I have a data entry form with 2 sub forms. 3 fields on the main form are
set to required in the table design. If a user clicks on a field in the
subform before entering data in any of the 3 required fields, I get a 3314
error:
"The field 'fieldname' cannot contain a null value because the required
property is set to True. Enter a value for this field."
where 'fieldname' is one of the 3 required fields on the main form.
Is there anyway around this? I don't want the user to see the error message
as it will confuse them.
--
Thanks,
Tina Hudson
- Follow-Ups:
- RE: Required Fields in main form with subform
- From: Tina Hudson
- RE: Required Fields in main form with subform
- References:
- Required Fields in main form with subform
- From: Tina Hudson
- RE: Required Fields in main form with subform
- From: Klatuu
- RE: Required Fields in main form with subform
- From: Tina Hudson
- Required Fields in main form with subform
- Prev by Date: Re: Invalid identifier following null constant
- Next by Date: Re: Through VBA code open objects stored in table and run these objects
- Previous by thread: RE: Required Fields in main form with subform
- Next by thread: RE: Required Fields in main form with subform
- Index(es):
Relevant Pages
|