Hi All,
I have a form with various tabs and a number of fields. If I could I would just select all fields to "required=Yes" in the table design mode. But some fields should only have data entered if another field has a certain value. So, I think I just want all visible fields for this data requirement (I set some fields to visible=false if I don't want them to have a value). Does anyone know the easiest way to do this? Perhaps, a few lines of VBA code?
So far I tried to do this on click of the Submit Record button which I created using the "docmd.gotorecord, , acnewrec" statement, but I can't seem to make it work when combining it with if then msgbox statements. Plus I typed an if then statement for every required field. It's definitely a little cumbersome but don't know any other way to do this.
Here's my code below:
Private Sub AddNewRecord_Click()
On Error GoTo Err1
DoCmd.GoToRecord , , acNewRec
Me.Label216.Visible = False
Me.CM_2A.Visible = False
Me.Label221.Visible = False
Me.CM_2B.Visible = False
Me.Label222.Visible = False
Me.CM_3B.Visible = False
Me.Label223.Visible = False
Me.CM_4B.Visible = False
Me.Label443.Visible = False
Me.CM_5B.Visible = False
Me.PCI_Date1.Visible = False
Me.PCI_Date2.Visible = False
Me.PCI_Date3.Visible = False
Me.PCI_Date4.Visible = False
Me.CABG_Date1.Visible = False
Me.CABG_Date2.Visible = False
Me.Box349.Visible = False
Me.Label560.Visible = False
Me.PriorTestCount.Visible = False
Me.Label238.Visible = False
Me.Label239.Visible = False
Me.Label240.Visible = False
Me.Label241.Visible = False
Me.Label243.Visible = False
Me.Label247.Visible = False
Me.Label251.Visible = False
Me.Label255.Visible = False
Me.Label259.Visible = False
Me.Label510.Visible = False
Me.Label555.Visible = False
Me.Label557.Visible = False
Me.Label558.Visible = False
Me.Label559.Visible = False
Me.Label525.Visible = False
Me.Test1.Visible = False
Me.Test1_Date.Visible = False
Me.Test1_Prior.Visible = False
Me.Test1Result1.Visible = False
Me.Test1Result2.Visible = False
Me.Test1Result3.Visible = False
Me.Test1Result4.Visible = False
Me.Test1Result5.Visible = False
Me.Test1Change.Visible = False
Me.Test2.Visible = False
Me.Test2_Date.Visible = False
Me.Test2_Prior.Visible = False
Me.Test2Result1.Visible = False
Me.Test2Result2.Visible = False
Me.Test2Result3.Visible = False
Me.Test2Result4.Visible = False
Me.Test2Result5.Visible = False
Me.Test2Change.Visible = False
Me.Test3.Visible = False
Me.Test3_Date.Visible = False
Me.Test3_Prior.Visible = False
Me.Test3Result1.Visible = False
Me.Test3Result2.Visible = False
Me.Test3Result3.Visible = False
Me.Test3Result4.Visible = False
Me.Test3Result5.Visible = False
Me.Test3Change.Visible = False
Me.Test4.Visible = False
Me.Test4_Date.Visible = False
Me.Test4_Prior.Visible = False
Me.Test4Result1.Visible = False
Me.Test4Result2.Visible = False
Me.Test4Result3.Visible = False
Me.Test4Result4.Visible = False
Me.Test4Result5.Visible = False
Me.Test4Change.Visible = False
Me.Test5.Visible = False
Me.Test5_Date.Visible = False
Me.Test5_Prior.Visible = False
Me.Test5Result1.Visible = False
Me.Test5Result2.Visible = False
Me.Test5Result3.Visible = False
Me.Test5Result4.Visible = False
Me.Test5Result5.Visible = False
Me.Test5Change.Visible = False
Me.Label505.Visible = False
Me.Label506.Visible = False
Me.Label507.Visible = False
Me.Label508.Visible = False
Me.Label509.Visible = False
Me.CTALocation.Visible = False
Me.CTAstenosis.Visible = False
Me.TestLocation1.Visible = False
Me.TestLocation2.Visible = False
Me.TestFinding.Visible = False
Exit Sub
Err1:
If Me.Date_of_Abstract = "" Then
MsgBox "Date of Abstract is required."
Cancel = True
End If
If Me.Index_Imaging_Test = "CTA" And Me.ImagingTestResult = "Abnormal" And (IsNull(Me.CTALocation) Or IsNull(Me.CTAstenosis)) Then
MsgBox "One or more required fields in the Imaging Test tab is missing."
Cancel = True
ElseIf Me.Index_Imaging_Test <> "CTA" And Me.ImagingTestResult = "Abnormal" And (IsNull(Me.TestLocation1) Or IsNull(Me.TestLocation2) _
Or IsNull(Me.TestFinding)) Then
MsgBox "One or more required fields in the Imaging Test tab is missing."
Cancel = True
End If
If Me.CM_1A <> "N/A" And IsNull(Me.CM_2A) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.CM_1B <> "N/A" And IsNull(Me.CM_2B) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.CM_2B <> "N/A" And IsNull(Me.CM_3B) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.CM_3B <> "N/A" And IsNull(Me.CM_4B) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.CM_4B <> "N/A" And IsNull(Me.CM_5B) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.PCI_Count = 1 And (IsNull(Me.PCI_Date1) Or IsNull(Me.PCI_Location1) Or IsNull(Me.PCI_Type1) _
Or IsNull(Me.Size1) Or IsNull(Me.PTCAorStent1)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.PCI_Count = 2 And (IsNull(Me.PCI_Date1) Or IsNull(Me.PCI_Location1) Or IsNull(Me.PCI_Type1) _
Or IsNull(Me.Size1) Or IsNull(Me.PTCAorStent1) Or IsNull(Me.PCI_Date2) Or IsNull(Me.PCI_Location2) Or IsNull(Me.PCI_Type2) _
Or IsNull(Me.Size2) Or IsNull(Me.PTCAorStent2)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.PCI_Count = 3 And (IsNull(Me.PCI_Date1) Or IsNull(Me.PCI_Location1) Or IsNull(Me.PCI_Type1) _
Or IsNull(Me.Size1) Or IsNull(Me.PTCAorStent1) Or IsNull(Me.PCI_Date2) Or IsNull(Me.PCI_Location2) Or IsNull(Me.PCI_Type2) _
Or IsNull(Me.Size2) Or IsNull(Me.PTCAorStent2) Or IsNull(Me.PCI_Date3) Or IsNull(Me.PCI_Location3) Or IsNull(Me.PCI_Type3) _
Or IsNull(Me.Size3) Or IsNull(Me.PTCAorStent3)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.PCI_Count = 4 And (IsNull(Me.PCI_Date1) Or IsNull(Me.PCI_Location1) Or IsNull(Me.PCI_Type1) _
Or IsNull(Me.Size1) Or IsNull(Me.PTCAorStent1) Or IsNull(Me.PCI_Date2) Or IsNull(Me.PCI_Location2) Or IsNull(Me.PCI_Type2) _
Or IsNull(Me.Size2) Or IsNull(Me.PTCAorStent2) Or IsNull(Me.PCI_Date3) Or IsNull(Me.PCI_Location3) Or IsNull(Me.PCI_Type3) _
Or IsNull(Me.Size3) Or IsNull(Me.PTCAorStent3) Or IsNull(Me.PCI_Date4) Or IsNull(Me.PCI_Location4) Or IsNull(Me.PCI_Type4) _
Or IsNull(Me.Size4) Or IsNull(Me.PTCAorStent4)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.CABG_Count = 1 And (IsNull(Me.CABG_Date1) Or IsNull(Me.CABG_Location1) Or IsNull(Me.CABG_Type1) _
Or IsNull(Me.Complete_Revascularization1)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.CABG_Count = 2 And (IsNull(Me.CABG_Date1) Or IsNull(Me.CABG_Location1) Or IsNull(Me.CABG_Type1) _
Or IsNull(Me.Complete_Revascularization1) Or IsNull(Me.CABG_Date2) Or IsNull(Me.CABG_Location2) Or IsNull(Me.CABG_Type2) _
Or IsNull(Me.Complete_Revascularization2)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Prior_Noninvasive_Stress_Test = "Yes" And IsNull(Me.PriorTestCount) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 1 And IsNull(Me.Test1) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 2 And (IsNull(Me.Test1) Or IsNull(Me.Test2)) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 3 And (IsNull(Me.Test1) Or IsNull(Me.Test2) Or IsNull(Me.Test3)) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 4 And (IsNull(Me.Test1) Or IsNull(Me.Test2) Or IsNull(Me.Test3) _
Or IsNull(Me.Test4)) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 5 And (IsNull(Me.Test1) Or IsNull(Me.Test2) Or IsNull(Me.Test3) _
Or IsNull(Me.Test4) Or IsNull(Me.Test5)) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.Chest_Pain = "Yes" And IsNull(Me.Character_of_Chest_Pain) Then
MsgBox "One required value is missing for Symptoms."
Cancel = True
End If
End Sub
Any help is greatly appreciated.
Thx,
Jim
I have a form with various tabs and a number of fields. If I could I would just select all fields to "required=Yes" in the table design mode. But some fields should only have data entered if another field has a certain value. So, I think I just want all visible fields for this data requirement (I set some fields to visible=false if I don't want them to have a value). Does anyone know the easiest way to do this? Perhaps, a few lines of VBA code?
So far I tried to do this on click of the Submit Record button which I created using the "docmd.gotorecord, , acnewrec" statement, but I can't seem to make it work when combining it with if then msgbox statements. Plus I typed an if then statement for every required field. It's definitely a little cumbersome but don't know any other way to do this.
Here's my code below:
Private Sub AddNewRecord_Click()
On Error GoTo Err1
DoCmd.GoToRecord , , acNewRec
Me.Label216.Visible = False
Me.CM_2A.Visible = False
Me.Label221.Visible = False
Me.CM_2B.Visible = False
Me.Label222.Visible = False
Me.CM_3B.Visible = False
Me.Label223.Visible = False
Me.CM_4B.Visible = False
Me.Label443.Visible = False
Me.CM_5B.Visible = False
Me.PCI_Date1.Visible = False
Me.PCI_Date2.Visible = False
Me.PCI_Date3.Visible = False
Me.PCI_Date4.Visible = False
Me.CABG_Date1.Visible = False
Me.CABG_Date2.Visible = False
Me.Box349.Visible = False
Me.Label560.Visible = False
Me.PriorTestCount.Visible = False
Me.Label238.Visible = False
Me.Label239.Visible = False
Me.Label240.Visible = False
Me.Label241.Visible = False
Me.Label243.Visible = False
Me.Label247.Visible = False
Me.Label251.Visible = False
Me.Label255.Visible = False
Me.Label259.Visible = False
Me.Label510.Visible = False
Me.Label555.Visible = False
Me.Label557.Visible = False
Me.Label558.Visible = False
Me.Label559.Visible = False
Me.Label525.Visible = False
Me.Test1.Visible = False
Me.Test1_Date.Visible = False
Me.Test1_Prior.Visible = False
Me.Test1Result1.Visible = False
Me.Test1Result2.Visible = False
Me.Test1Result3.Visible = False
Me.Test1Result4.Visible = False
Me.Test1Result5.Visible = False
Me.Test1Change.Visible = False
Me.Test2.Visible = False
Me.Test2_Date.Visible = False
Me.Test2_Prior.Visible = False
Me.Test2Result1.Visible = False
Me.Test2Result2.Visible = False
Me.Test2Result3.Visible = False
Me.Test2Result4.Visible = False
Me.Test2Result5.Visible = False
Me.Test2Change.Visible = False
Me.Test3.Visible = False
Me.Test3_Date.Visible = False
Me.Test3_Prior.Visible = False
Me.Test3Result1.Visible = False
Me.Test3Result2.Visible = False
Me.Test3Result3.Visible = False
Me.Test3Result4.Visible = False
Me.Test3Result5.Visible = False
Me.Test3Change.Visible = False
Me.Test4.Visible = False
Me.Test4_Date.Visible = False
Me.Test4_Prior.Visible = False
Me.Test4Result1.Visible = False
Me.Test4Result2.Visible = False
Me.Test4Result3.Visible = False
Me.Test4Result4.Visible = False
Me.Test4Result5.Visible = False
Me.Test4Change.Visible = False
Me.Test5.Visible = False
Me.Test5_Date.Visible = False
Me.Test5_Prior.Visible = False
Me.Test5Result1.Visible = False
Me.Test5Result2.Visible = False
Me.Test5Result3.Visible = False
Me.Test5Result4.Visible = False
Me.Test5Result5.Visible = False
Me.Test5Change.Visible = False
Me.Label505.Visible = False
Me.Label506.Visible = False
Me.Label507.Visible = False
Me.Label508.Visible = False
Me.Label509.Visible = False
Me.CTALocation.Visible = False
Me.CTAstenosis.Visible = False
Me.TestLocation1.Visible = False
Me.TestLocation2.Visible = False
Me.TestFinding.Visible = False
Exit Sub
Err1:
If Me.Date_of_Abstract = "" Then
MsgBox "Date of Abstract is required."
Cancel = True
End If
If Me.Index_Imaging_Test = "CTA" And Me.ImagingTestResult = "Abnormal" And (IsNull(Me.CTALocation) Or IsNull(Me.CTAstenosis)) Then
MsgBox "One or more required fields in the Imaging Test tab is missing."
Cancel = True
ElseIf Me.Index_Imaging_Test <> "CTA" And Me.ImagingTestResult = "Abnormal" And (IsNull(Me.TestLocation1) Or IsNull(Me.TestLocation2) _
Or IsNull(Me.TestFinding)) Then
MsgBox "One or more required fields in the Imaging Test tab is missing."
Cancel = True
End If
If Me.CM_1A <> "N/A" And IsNull(Me.CM_2A) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.CM_1B <> "N/A" And IsNull(Me.CM_2B) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.CM_2B <> "N/A" And IsNull(Me.CM_3B) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.CM_3B <> "N/A" And IsNull(Me.CM_4B) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.CM_4B <> "N/A" And IsNull(Me.CM_5B) Then
MsgBox "One or more required fields in the Co-Morbidities tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.PCI_Count = 1 And (IsNull(Me.PCI_Date1) Or IsNull(Me.PCI_Location1) Or IsNull(Me.PCI_Type1) _
Or IsNull(Me.Size1) Or IsNull(Me.PTCAorStent1)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.PCI_Count = 2 And (IsNull(Me.PCI_Date1) Or IsNull(Me.PCI_Location1) Or IsNull(Me.PCI_Type1) _
Or IsNull(Me.Size1) Or IsNull(Me.PTCAorStent1) Or IsNull(Me.PCI_Date2) Or IsNull(Me.PCI_Location2) Or IsNull(Me.PCI_Type2) _
Or IsNull(Me.Size2) Or IsNull(Me.PTCAorStent2)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.PCI_Count = 3 And (IsNull(Me.PCI_Date1) Or IsNull(Me.PCI_Location1) Or IsNull(Me.PCI_Type1) _
Or IsNull(Me.Size1) Or IsNull(Me.PTCAorStent1) Or IsNull(Me.PCI_Date2) Or IsNull(Me.PCI_Location2) Or IsNull(Me.PCI_Type2) _
Or IsNull(Me.Size2) Or IsNull(Me.PTCAorStent2) Or IsNull(Me.PCI_Date3) Or IsNull(Me.PCI_Location3) Or IsNull(Me.PCI_Type3) _
Or IsNull(Me.Size3) Or IsNull(Me.PTCAorStent3)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.PCI_Count = 4 And (IsNull(Me.PCI_Date1) Or IsNull(Me.PCI_Location1) Or IsNull(Me.PCI_Type1) _
Or IsNull(Me.Size1) Or IsNull(Me.PTCAorStent1) Or IsNull(Me.PCI_Date2) Or IsNull(Me.PCI_Location2) Or IsNull(Me.PCI_Type2) _
Or IsNull(Me.Size2) Or IsNull(Me.PTCAorStent2) Or IsNull(Me.PCI_Date3) Or IsNull(Me.PCI_Location3) Or IsNull(Me.PCI_Type3) _
Or IsNull(Me.Size3) Or IsNull(Me.PTCAorStent3) Or IsNull(Me.PCI_Date4) Or IsNull(Me.PCI_Location4) Or IsNull(Me.PCI_Type4) _
Or IsNull(Me.Size4) Or IsNull(Me.PTCAorStent4)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.CABG_Count = 1 And (IsNull(Me.CABG_Date1) Or IsNull(Me.CABG_Location1) Or IsNull(Me.CABG_Type1) _
Or IsNull(Me.Complete_Revascularization1)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Revascularization = "Yes" And Me.CABG_Count = 2 And (IsNull(Me.CABG_Date1) Or IsNull(Me.CABG_Location1) Or IsNull(Me.CABG_Type1) _
Or IsNull(Me.Complete_Revascularization1) Or IsNull(Me.CABG_Date2) Or IsNull(Me.CABG_Location2) Or IsNull(Me.CABG_Type2) _
Or IsNull(Me.Complete_Revascularization2)) Then
MsgBox "One or more required fields in the PCI or CABG tab is missing."
Cancel = True
End If
If Me.Prior_Noninvasive_Stress_Test = "Yes" And IsNull(Me.PriorTestCount) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 1 And IsNull(Me.Test1) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 2 And (IsNull(Me.Test1) Or IsNull(Me.Test2)) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 3 And (IsNull(Me.Test1) Or IsNull(Me.Test2) Or IsNull(Me.Test3)) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 4 And (IsNull(Me.Test1) Or IsNull(Me.Test2) Or IsNull(Me.Test3) _
Or IsNull(Me.Test4)) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.PriorTestCount = 5 And (IsNull(Me.Test1) Or IsNull(Me.Test2) Or IsNull(Me.Test3) _
Or IsNull(Me.Test4) Or IsNull(Me.Test5)) Then
MsgBox "One or more required fields is missing in the Prior Tests tab."
Cancel = True
End If
If Me.Chest_Pain = "Yes" And IsNull(Me.Character_of_Chest_Pain) Then
MsgBox "One required value is missing for Symptoms."
Cancel = True
End If
End Sub
Any help is greatly appreciated.
Thx,
Jim