Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-25-2022, 09:05 AM
CellCharger CellCharger is offline Make a field required Windows 10 Make a field required Office 2019
Novice
Make a field required
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 07-25-2022, 01:36 PM
gmaxey gmaxey is offline Make a field required Windows 10 Make a field required Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-25-2022, 08:20 PM
CellCharger CellCharger is offline Make a field required Windows 10 Make a field required Office 2019
Novice
Make a field required
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 07-25-2022, 10:14 PM
Guessed's Avatar
Guessed Guessed is offline Make a field required Windows 10 Make a field required Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 07-26-2022, 12:30 PM
CellCharger CellCharger is offline Make a field required Windows 10 Make a field required Office 2019
Novice
Make a field required
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

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
I attached the form just in case.
Attached Images
File Type: jpg Error.jpg (28.5 KB, 27 views)
Attached Files
File Type: docm Report Due Date (1) fixed.docm (56.5 KB, 8 views)

Last edited by CellCharger; 07-27-2022 at 05:49 AM.
Reply With Quote
  #6  
Old 07-26-2022, 09:49 PM
Guessed's Avatar
Guessed Guessed is offline Make a field required Windows 10 Make a field required Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 07-27-2022, 06:05 AM
CellCharger CellCharger is offline Make a field required Windows 10 Make a field required Office 2019
Novice
Make a field required
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 07-27-2022, 04:55 PM
Guessed's Avatar
Guessed Guessed is offline Make a field required Windows 10 Make a field required Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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.
Reply With Quote
  #9  
Old 07-28-2022, 06:01 AM
CellCharger CellCharger is offline Make a field required Windows 10 Make a field required Office 2019
Novice
Make a field required
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

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.
Reply With Quote
  #10  
Old 07-28-2022, 06:49 AM
Guessed's Avatar
Guessed Guessed is offline Make a field required Windows 10 Make a field required Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Did you select the date from the dropdown calendar or type it in?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #11  
Old 07-28-2022, 07:00 AM
CellCharger CellCharger is offline Make a field required Windows 10 Make a field required Office 2019
Novice
Make a field required
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Did you select the date from the dropdown calendar or type it in?
I selected it from the dropdown calendar.

Regards,
Reply With Quote
  #12  
Old 07-28-2022, 04:22 PM
Guessed's Avatar
Guessed Guessed is offline Make a field required Windows 10 Make a field required Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #13  
Old 07-28-2022, 10:03 PM
CellCharger CellCharger is offline Make a field required Windows 10 Make a field required Office 2019
Novice
Make a field required
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

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.
Reply With Quote
  #14  
Old 08-03-2022, 09:39 PM
CellCharger CellCharger is offline Make a field required Windows 10 Make a field required Office 2019
Novice
Make a field required
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

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.
Reply With Quote
  #15  
Old 08-03-2022, 09:57 PM
Guessed's Avatar
Guessed Guessed is offline Make a field required Windows 10 Make a field required Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make a field required 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
Make a field required How to make a Check Box Form Field red if not checked? CarlCR Word Tables 3 07-12-2016 08:35 PM
Make a field required 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft