PDA

View Full Version : How to grey out form field depedant on text in another?



TheMongoose
08-28-2012, 05:44 AM
Good afternoon,

not the snappiest title I know, I'm looking for how to have say two form fields - Form 1 & Form 2

Form 1 will contain something like

Employed
Unemployed
Retired

is there a way of making Form 2 inactive (ie greyed out) unless "unemployed" is selected in Form 1?

I'm guessing this isn't too difficult really (I may be wrong!), I'm new to VBA but it's actually quite interesting really... I did a bit of pascal at school; uses wincrt :rotlaugh:

Thanks!

gmaxey
08-28-2012, 06:26 PM
Set the following macro to run on exit from your ffMaster (the dropdown). Assumes "Unemployed" is the second dropdown value.

Sub MasterDDOnExit()
If ActiveDocument.FormFields("ffMaster").DropDown.Value = 2 Then
ActiveDocument.FormFields("ffDependent").Enabled = True
Else
ActiveDocument.FormFields("ffDependent").Enabled = False
End If
End Sub

TheMongoose
08-29-2012, 05:20 AM
You, Sir, are a scholar and a gent! Thank you

At the risk of abusing your good nature slightly,Is there a way of "reversing" the macro so to speak? Ie if someone has chosen Unemployed but then goes back to change it the other field is now not active, or when I'm working on test my sheet - so I have to unprotect and manually select "dropdown enabled" from the form field, then reprotect and begin again.

I did try to write another macro so instead of (I've added in some more options)


Sub MasterDDOnExit()
If ActiveDocument.FormFields("ffMaster").DropDown.Value = 4 Or 5 Or 6 Then
ActiveDocument.FormFields("ffDependent").Enabled = False
Else
ActiveDocument.FormFields("ffDependent").Enabled = True
End If

End Sub



it was


Sub MasterDD1OnExit()
If ActiveDocument.FormFields("ffMaster").DropDown.Value = 1 Or 2 Or 3 Then
ActiveDocument.FormFields("ffDependent").Enabled = True
Else
ActiveDocument.FormFields("ffDependent").Enabled = False
End If

End Sub




but it didn't seem to work.

Frosty
08-29-2012, 10:49 AM
I dislike complicated OR statements, as sometimes they will accept the value as you did it, and sometimes you will need to write out the whole statement.

I would look at use of a Select Case statement instead.


Sub MasterDD1OnExit()
Select Case ActiveDocument.FormFields("ffMaster").DropDown.Value
Case 1, 2, 3
ActiveDocument.FormFields("ffDependent").Enabled = True
Case Else
ActiveDocument.FormFields("ffDependent").Enabled = False
End Select
End Sub

You should look into the options of how to step through your code (using F5, F8 and F9 in various combinations) to help you troubleshoot your own code.

TheMongoose
08-29-2012, 03:02 PM
Greetings,

I was going to actually post if "or" was right because it seems to break the macro randomly, sometimes it worked sometimes it didn't..and yeah I had going up to like or 12 too heh. I suppose I should have made it clear in the original post I have lots of options.

WHat I'm really looking for is for it work something like this: (and no it's not meant to be in code)

ffMaster – if Unemployed (option 4) is selected then:

ffDependent – is Not available
ffDependent1 - is Available
ffDependent2 – Not available
ffText1 – Not available
ffText2 – Not available

but;

ffMaster – if Employed (option 3) is selected then:

ffDependent – Available
ffDependent1 - Not Available
ffDependent2 – Available
ffText1 – Available
ffText2 – Available

ffMaster is Employment Status, ffDependent is job security, dependent1 is unemployment length, dep2 is employment hours, text1 is employment start date and text2 is employment finish date if that makes it clear, so if someone is unemployed you dont need job security field for instance

I shall give Case a try and will look into stepping through the code, I just need to work out what these mean first! But no-one leart anything by being spoon fed,

thanks :]

Frosty
08-29-2012, 03:26 PM
The select case will also allow ranges of numbers, as well as being nested.

So you can do...

Select Case SomeVariable
Case 1-3
Case 4-7
Case 8
Case 9-10
Case Else
End Select

However, you can only use ranges if you're actually using numbers... you can't use them if you're using strings which *look* like numbers. Even though VBA will sometimes do work behind the scenes to do what is called "coercing" data types, there is a significant difference between...
1 (an integer or a long)
and
"1" (a string)

Where possible, rather than coming up with codes (unless these are meaningful to you) which represent something, just use the "something" directly.

Select Case myFormField("ffMaster").Dropdown.Value
Case "Unemployed"
'do stuff
Case "Employed"
'do different stuff?
End Select

etc etc.

But otherwise, as you said-- you'll learn better if we don't spoonfeed you. But you're going to want to work out your exact logic map (sounds like you're pretty close) and go from there.

But I would start simple (as you appear to have done), and then work your way out to the more complex. The logic is the same whether you're adjusting 1 or 50 form fields based on the value of 1 form field, so don't write a bunch of code adjusting 50 form fields until you get a structure you like.

Then, once you've copied and pasted a bunch of code, we can talk about how you modularize the code to make it more simple to re-use code.

Good luck!