#1
|
|||
|
|||
Make a field required
Hi everyone,
I have a restricted editing Word form with multiple fields. The fields are a combination of Rich Text Content Control, Drop Down List Content Control, Checkbox Content Control and Date Picker Content Control. I am looking for a code makes certain fields required with a warning messaging indicating this field is required. Also, when the user leaves a required field empty, will they know which field was left empty? In other words, will the warning message be generic or field-specific? I am not sure if I need to attach a sample form. Thank you! |
#2
|
|||
|
|||
In every Word document VB Project "ThisDocument" module there is an event:
Here, I have applied the tag "ManNum" to CCs requiring and numerical input and "ManText" to those requiring a text input. Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Tag Case "ManText" If ContentControl.ShowingPlaceholderText Then MsgBox "This field requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If Case "ManNum" If Not IsNumeric(ContentControl.Range.Text) Then MsgBox "This field requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If End Select lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Thank you, Greg!
I have 2 questions. 1. I have an existing code in "ThisDocument". How do I add another code? 2. Can your code be field-specific? As I mentioned, my form has multiple fields, but I only need a few fields to be required fields. Thanks again for your help. |
#4
|
||||
|
||||
You copy and paste the code into that module - assuming there isn't already code there with exactly the same name.
The code Greg posted is field specific already - based on the value you type into the Tag property of the Content Control.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Thank you Andrew,
Please excuse my lack of knowledge. I fixed the field tags but when I pasted the code below the existing code, I get the error message (see the attachment). And just for the record, the the first code was from another post that I created. this is the code Code:
Option Explicit Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) Application.ScreenUpdating = False Dim Dt As Date, StrDt As String With CCtrl If .Title <> "Date of Initiation" Then Exit Sub If .ShowingPlaceholderText = True Then ActiveDocument.SelectContentControlsByTitle("Due Date")(1).Range.Text = "" Else StrDt = .Range.Text If IsDate(StrDt) Then Dt = CDate(StrDt) Else Dt = CDate(Split(StrDt, (Split(StrDt, " ")(0)))(1)) End If ActiveDocument.SelectContentControlsByTitle("Due Date")(1).Range.Text = Format(Dt + 30, .DateDisplayFormat) End If End With Application.ScreenUpdating = True End Sub Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Tag Case "Rev. #" If ContentControl.ShowingPlaceholderText Then MsgBox "Rev. # requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If Case "PDR #" If ContentControl.ShowingPlaceholderText Then MsgBox "PDR # requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If Case "Date of Discovery" If ContentControl.ShowingPlaceholderText Then MsgBox "Date of Discovery requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If Case "Type" If ContentControl.ShowingPlaceholderText Then MsgBox "Type requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If Case "ManNum" If Not IsNumeric(ContentControl.Range.Text) Then MsgBox "This field requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If End Select lbl_Exit: Exit Sub End Sub Last edited by CellCharger; 07-27-2022 at 05:49 AM. |
#6
|
||||
|
||||
As alluded to in my assumption, you can't have two instances of the same macro names in the same module. Try replacing the code you have with this single macro which merges the intent of both macros with a bit more flexibility. Note that there is a difference between the Tag and Title properties on a Content Control. Either can be used in the code.
Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) If aCC.ShowingPlaceholderText Then Select Case aCC.Tag Case "Rev. #", "PDR #", "Date of Discovery", "Type" 'these CCs require an answer MsgBox "This Content Control requires a response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End Select Else 'if CC has content, check its type matches the content If aCC.Tag Like "*[#]" Then 'if the CCs tag ends with '#' If Not IsNumeric(aCC.Range.Text) Then MsgBox "This field requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If ElseIf aCC.Tag Like "*Date*" Then 'if CCs tag includes 'Date' If Not IsDate(aCC.Range.Text) Then MsgBox "This field requires a date response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If End If End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
I am sorry again, but are you saying that I can't have 2 codes in "ThisDocument"? Please keep in mind that my knowledge in coding is very limited
Essentially what the form needs to is automatically adds 30 days to based on date of initiation field (hats the first code) and also requires input in certain fields (that's the second code) Regards, Last edited by CellCharger; 07-27-2022 at 01:26 PM. |
#8
|
||||
|
||||
I'm saying you can't have the same macro name twice in the same module. In this case the macro name is critical because the name and location means it automatically runs whenever the user moves out the selection out of a Content Control.
But you can combine the functionality in both macros into a single macro. The following code should replace everything you had in the earlier posts Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) Dim aDate As Date If aCC.ShowingPlaceholderText Then Select Case aCC.Tag Case "Rev. #", "PDR #", "Date of Discovery", "Type" 'these CCs require an answer MsgBox "This Content Control requires a response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End Select Else 'if CC has content, check its type matches the content If aCC.Tag Like "*[#]" Then 'if the CCs tag ends with '#' If Not IsNumeric(aCC.Range.Text) Then MsgBox "This field requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If ElseIf aCC.Tag Like "*Date*" Then 'if CCs tag includes 'Date' If Not IsDate(aCC.Range.Text) Then MsgBox "This field requires a date response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True ElseIf aCC.Title = "Date of Initiation" Then 'note tag doesn't match on this CC in your doc aDate = CDate(aCC.Range.Text) + 30 ActiveDocument.SelectContentControlsByTitle("Due Date")(1).Range.Text = aDate End If End If End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia Last edited by Charles Kenyon; 07-28-2022 at 02:04 PM. |
#9
|
|||
|
|||
Thank you Andrew,
The form is stuck at the date of discovery field even after I enter a date. it keeps asking for an input although there is date entered. |
#10
|
||||
|
||||
Did you select the date from the dropdown calendar or type it in?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
Quote:
Regards, |
#12
|
||||
|
||||
The formatting of that particular CC shows as "26May2022" and it doesn't include spaces so the VBA is not recognising it as a valid date.
Is there a reason you can't change the CC properties to format it with spaces? That would be the easy fix
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#13
|
|||
|
|||
Oh I see. This is the company’s date format but dd-mmm-yyyy works as well. I changed the date format in the date of initiation and date of discovery fields to dd-mmm-yyyy but the due date format is m/dd/yyyy. How do I change it to dd-mmm-yyyy?
By the way, I matched the date of discovery title and tag. That was a mistake. Last edited by CellCharger; 07-29-2022 at 09:14 AM. |
#14
|
|||
|
|||
Thank you, Andrew, for your help. I fixed the date format by changing the text CC to date picker CC. Is there a way to make the "Due Date" field un-editable? I know I change the field property to make to prevent editing but If I do that, the macro will not work since the form the protected.
|
#15
|
||||
|
||||
Your code can temporarily change the CC to make it editable and then change it back after it is edited.
Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) Dim aDate As Date, ccDue As ContentControl If aCC.ShowingPlaceholderText Then Select Case aCC.Tag Case "Rev. #", "PDR #", "Date of Discovery", "Type" 'these CCs require an answer MsgBox "This Content Control requires a response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End Select Else 'if CC has content, check its type matches the content If aCC.Tag Like "*[#]" Then 'if the CCs tag ends with '#' If Not IsNumeric(aCC.Range.Text) Then MsgBox "This field requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True End If ElseIf aCC.Tag Like "*Date*" Then 'if CCs tag includes 'Date' If Not IsDate(aCC.Range.Text) Then MsgBox "This field requires a date response.", vbInformation + vbOKOnly, "INPUT REQUIRED" Cancel = True ElseIf aCC.Title = "Date of Initiation" Then 'note tag doesn't match on this CC in your doc aDate = CDate(aCC.Range.Text) + 30 Set ccDue = ActiveDocument.SelectContentControlsByTitle("Due Date")(1) ccDue.LockContents = False ccDue.Range.Text = Format(aDate, "d mmm yyyy") 'if date picker, format may be as per CC property ccDue.LockContents = True End If End If End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
how to make a template with field and mailmerge to work together in protected mode | pjkon | Mail Merge | 1 | 06-10-2019 04:35 PM |
How To Make 2013 Populate To Field | abraxis | Outlook | 2 | 08-10-2018 06:41 AM |
How to make a Check Box Form Field red if not checked? | CarlCR | Word Tables | 3 | 07-12-2016 08:35 PM |
How to make a SEQ field show up in cross references? | Roscoe | Word | 5 | 06-01-2016 01:39 PM |
make text form field active dependent on dropdown | Glenn0004 | Word VBA | 1 | 06-23-2015 06:13 PM |