#  Other Applications & Softwares  > Word Formatting & General >  >  Form Design in Word

## lyla22

Hi,

I need to create a form in Word 2003 that will be used to gather data. However, I need to build the following functionality into the form:

1.	Checkboxes  only allow a user to tick one Checkbox in a group (I think I got this one sorted)
2.	Mandatory Fields - need certain fields to always be populated
3.	If a Checkbox is ticked, need to set a default value in a Text Field.
4.	Make a selection of fields in an area of the form mandatory if a checkbox is selected.
5.	Hide certain fields if a checkbox is ticked

I have done some playing around with the Forms toolbar and so far, only got No. 1 above working. The rest I cant seem to make any headway on.

I've attached a sample form containing the kinds of things I'd like to be able to do.

Can anyone help?

Many Thanks in Advance...

----------


## macropod

For code re mandatory fields, see: http://www.excelforum.com/word-progr...ed-fields.html

Do note that it is not possible to _hide_ formfields of any kind. Accordingly, you would need to add/delete the formfields programatically, for which see: http://www.msofficeforums.com/word/1...html#post30792

----------


## James Donovan

> For code re mandatory fields, see: http://www.excelforum.com/word-progr...ed-fields.html
> 
> Do note that it is not possible to _hide_ formfields of any kind. Accordingly, you would need to add/delete the formfields programatically, for which see: http://www.msofficeforums.com/word/1...html#post30792



Thanks for share valuable information....... :Smilie:

----------


## lyla22

Paul,

Thank you so much, those links are a great help.

 :Smilie:

----------


## lyla22

Paul,

A couple of additional questions...

As a follow-on from the query you answered above RE programatically, is it only possible to do this on a field by field basis, i.e. could you have a set of fields stored in an AutoText entry or something and then have VBA insert this AutoText entry if a checkbox field is selected? Its just that I may have 20-30 fields that need to be hidden or displayed which would mean a lot of code.

And as a follow-on from mandatory fields. The sample above is great but the form I'm designing can have varying mandatory fields, depending on selections made along the way. So if tick checkbox A, I might want to make TextField 1, TextField5 and TextField8 mandatory whereas if I tick Checkbox B, I may want TextField2,TextField3 and TextField11 to be mandatory. Is this in any way possible?

----------


## macropod

Adding fields could _not_ be done via an Autotext entry. The only practical workaround I can think of for Office 2003 would be to have another document with all the fields in-situ and copy those into the current document on demand.

As for the second question, yes, that's possible. What would be required is the addition of a series of IF tests (or perhaps Select Case tests) to determine when a particular field is mandatory.

----------


## lyla22

Ive started working on the form and so far am getting on ok. Ive been able to use code to hide/show bookmarked areas which contain the fields required based on other selections. Ive also used code to enable/disable particular fields based on other selections made. 

There is one thing I just cant seem to figure out. Paul, you alluded to it in your last post but Im unsure of how I would do it. I basically need certain fields to be made mandatory based on selections made in other fields. I would like if say, I choose A in a dropdown field, that 4 other fields will automatically become mandatory and must be filled in before the form can be saved. So depending on whatever selections are made by the user, the mandatory fields will vary each time a user populates the form. So the code provided earlier works, but only when the mandatory fields are fixed. How could I amend it to cater for varying mandatory fields, or am I asking too much? Or is there another approach I could use?

Thanks again in advance

----------


## macropod

Hi Lyla,

basically, it's a matter of having a series of If tests or a Select Case statements to determine which fields get added to the 'required' collection when the document is saved. So, for example, if a particular dropdown has 'Yes' selected, you might have one group of formfields added to the collection but, if it's 'no', another set might be added. And so on for other 'trigger' formfields.

----------


## lyla22

Thanks for your quick reply! I can understand the logic of what you are suggesting. Its my limited coding abilities that are the problem! I understand I can use the IF to test what value a field contains but could you give me a start on what code would add the desired fields to the required collection?

So to test the dropdown field, I know I would start with this:

If ActiveDocument.FormFields("DropDownX").DropDown.Value = 1 Then

???????

----------


## macropod

Hi Lyla,

Yes, it'd be something like:



```
Please Login or Register  to view this content.
```

----------


## lyla22

Ah! So I can just code the IF statements into the BeforeSave code. So obvious when you think about it but I had totally overcomplicated it in my head! So my code might look something like:




```
Please Login or Register  to view this content.
```



I will try this and see how I get on.

Thank you so much, am learning a lot!!

----------


## macropod

For consistency with the rest of the code you're using, you'd need to change 'ActiveDocument' to 'Doc', but that's about it.

----------


## lyla22

Me Again!

I've had to revise my approach on the validation of the mandatory fields. When this form is filled, it will be populated by a few different users meaning it may be saved many times before all the required fields are filled by the different users. So the checking of the mandatory fields on the Save action is useless in this case.

As an alternative, there is a field at the end of the form that must be filled which is a vital field that cannot be left blank. So could I assign an 'On Entry' macro to this field that would check that all mandatory fields have been filled before it will permit the user to fill in this vital field? I tried to implement the code above and use it in this context but its giving a Runtime error 424 - Object Required error and highlighting the line ReqFields.Add Doc.FormFields("ReasonChange") as the problem.

Here's the code I'm assigning as the On Entry macro for the field:




```
Please Login or Register  to view this content.
```


I'd really appreciate your guidance on this...

Thanks

----------


## macropod

Hi lyla,

A simpler approach would be to test whether the last formfield has been filled in at the time of saving. If not, just exit normally. You could do with with:



```
Please Login or Register  to view this content.
```


where 'LastField' is the bookmark name assigned to the "_field at the end of the form that must be filled which is a vital field that cannot be left blank_"

----------


## lyla22

Perfect, Thanks so much Paul! That should work great!

----------


## lyla22

My form is still in development and Im having trouble with the latest requirement. Paul, you have been so helpful thus far, Im hoping you may have a way for me to do the following.

I have successfully implemented the code in earlier posts which checks if various mandatory fields have been populated at the point of Saving and it works well. The only issue has been the vast amounts of criteria Ive had to code (LOTS of IF Statements!). But its working.

I now have a new table to be added to the form. I have a macro which sets the correct colour on the fields in the first row (Yellow = mandatory, Green = Optional) and then I have a macro to automatically give the user a new row if required and create form fields on the fly based on the previous rows fields. Because I cant foresee how many rows will be used, I cant set fixed mandatory fields. 

So could I code into the DocumentBeforeSave module something that would check in the table for any field that has a yellow background and set it as a mandatory field? Or maybe the field contents of certain columns in the table could be flagged as mandatory?

Ive attached a sample document containing a similar table to what Ill be using.

Any help would be greatly appreciated.

----------


## macropod

Given that the fields are in a regular table, you could simply test all cells in the specified columns for completion. That way you don't need to know anything about the field names, colours, etc.

The following function returns a string containing outstanding cell references:



```
Please Login or Register  to view this content.
```


You call the function by passing it a table number and a series of columns to test (eg TableTest(1, "1,2,4") to test columns 1, 2 & 4 of table 1).

Here's an example of how you might use it:



```
Please Login or Register  to view this content.
```

----------


## lyla22

Brilliant as always!

I think that should work perfectly. 

Thank you SO much.

----------

